Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(enhanced): improve schema validation maintainability #3335

Draft
wants to merge 5 commits into
base: consume-share-layers
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"commit": "cz",
"docs": "typedoc",
"f": "nx format:write",
"enhanced:jest": "pnpm build && cd packages/enhanced && NODE_OPTIONS=--experimental-vm-modules npx jest test/ConfigTestCases.basictest.js",
"enhanced:jest": "pnpm build && cd packages/enhanced && NODE_OPTIONS=--experimental-vm-modules npx jest test/schema.basictest.js",
"lint": "nx run-many --target=lint",
"test": "nx run-many --target=test",
"build": "nx run-many --target=build --parallel=5 --projects=tag:type:pkg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ export interface ProvidesConfig {
* Do not accept shared module if version is not valid (defaults to yes, if local fallback module is available and shared module is not a singleton, otherwise no, has no effect if there is no required version specified).
*/
strictVersion?: boolean;
/**
* Layer for the shared module.
*/
layer?: string;
/**
* The actual request to use for importing the module. If not specified, the property name/key will be used.
*/
request?: string;
}
7 changes: 0 additions & 7 deletions packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class ConsumeSharedModule extends Module {
* @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
* @returns {void}
*/
// @ts-ignore
override needBuild(
context: NeedBuildContext,
callback: (error?: WebpackError | null, needsRebuild?: boolean) => void,
Expand All @@ -149,7 +148,6 @@ class ConsumeSharedModule extends Module {
* @param {function(WebpackError=): void} callback callback function
* @returns {void}
*/
// @ts-ignore
override build(
options: WebpackOptions,
compilation: Compilation,
Expand Down Expand Up @@ -192,18 +190,15 @@ class ConsumeSharedModule extends Module {
* @param {UpdateHashContext} context context
* @returns {void}
*/
// @ts-ignore
override updateHash(hash: Hash, context: UpdateHashContext): void {
hash.update(JSON.stringify(this.options));
// @ts-ignore
super.updateHash(hash, context);
}

/**
* @param {CodeGenerationContext} context context for code generation
* @returns {CodeGenerationResult} result
*/
// @ts-ignore
override codeGeneration({
chunkGraph,
moduleGraph,
Expand All @@ -224,7 +219,6 @@ class ConsumeSharedModule extends Module {
if (eager) {
const dep = this.dependencies[0];
fallbackCode = runtimeTemplate.syncModuleFactory({
// @ts-ignore
dependency: dep,
chunkGraph,
runtimeRequirements,
Expand All @@ -233,7 +227,6 @@ class ConsumeSharedModule extends Module {
} else {
const block = this.blocks[0];
fallbackCode = runtimeTemplate.asyncModuleFactory({
// @ts-ignore
block,
chunkGraph,
runtimeRequirements,
Expand Down
2 changes: 1 addition & 1 deletion packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ class ConsumeSharedPlugin {
async (resolveData: ResolveData): Promise<Module | undefined> => {
const { context, request, dependencies, contextInfo } = resolveData;
// wait for resolving to be complete
//@ts-ignore
return promise.then(() => {
if (
dependencies[0] instanceof ConsumeSharedFallbackDependency ||
Expand Down Expand Up @@ -345,6 +344,7 @@ class ConsumeSharedPlugin {
});
}
}
return;
});
},
);
Expand Down
12 changes: 9 additions & 3 deletions packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ProvideSharedDependency extends Dependency {
requiredVersion: string | false;
strictVersion: boolean;
singleton: boolean;
layer?: string;

/**
* @param {string} shareScope share scope
Expand All @@ -36,6 +37,7 @@ class ProvideSharedDependency extends Dependency {
* @param {boolean} requiredVersion version requirement
* @param {boolean} strictVersion don't use shared version even if version isn't valid
* @param {boolean} singleton use single global version
* @param {string} [layer] layer information
*/
constructor(
shareScope: string,
Expand All @@ -46,6 +48,7 @@ class ProvideSharedDependency extends Dependency {
requiredVersion: string | false,
strictVersion: boolean,
singleton: boolean,
layer?: string,
) {
super();
this.shareScope = shareScope;
Expand All @@ -56,6 +59,7 @@ class ProvideSharedDependency extends Dependency {
this.requiredVersion = requiredVersion;
this.strictVersion = strictVersion;
this.singleton = singleton;
this.layer = layer;
}

override get type(): string {
Expand All @@ -66,7 +70,7 @@ class ProvideSharedDependency extends Dependency {
* @returns {string | null} an identifier to merge equal requests
*/
override getResourceIdentifier(): string | null {
return `provide module (${this.shareScope}) ${this.request} as ${
return `provide module (${this.shareScope})${this.layer ? ` (${this.layer})` : ''} ${this.request} as ${
this.name
} @ ${this.version}${this.eager ? ' (eager)' : ''}`;
}
Expand All @@ -77,12 +81,13 @@ class ProvideSharedDependency extends Dependency {
override serialize(context: ObjectSerializerContext): void {
context.write(this.shareScope);
context.write(this.name);
context.write(this.request);
context.write(this.version);
context.write(this.request);
context.write(this.eager);
context.write(this.requiredVersion);
context.write(this.strictVersion);
context.write(this.singleton);
context.write(this.layer);
super.serialize(context);
}

Expand All @@ -103,8 +108,9 @@ class ProvideSharedDependency extends Dependency {
read(),
read(),
read(),
read(),
);
//@ts-ignore
// @ts-expect-error - webpack serializer pattern requires static property
this.shareScope = context.read();
obj.deserialize(context);
return obj;
Expand Down
21 changes: 11 additions & 10 deletions packages/enhanced/src/lib/sharing/ProvideSharedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ProvideSharedModule extends Module {
* @param {boolean} requiredVersion version requirement
* @param {boolean} strictVersion don't use shared version even if version isn't valid
* @param {boolean} singleton use single global version
* @param {string} [layer] layer information
*/
constructor(
shareScope: string,
Expand All @@ -63,8 +64,9 @@ class ProvideSharedModule extends Module {
requiredVersion: string | false,
strictVersion: boolean,
singleton: boolean,
layer?: string,
) {
super(WEBPACK_MODULE_TYPE_PROVIDE);
super(WEBPACK_MODULE_TYPE_PROVIDE, undefined, layer);
this._shareScope = shareScope;
this._name = name;
this._version = version;
Expand All @@ -79,17 +81,19 @@ class ProvideSharedModule extends Module {
* @returns {string} a unique identifier of the module
*/
override identifier(): string {
return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
return `provide module (${this._shareScope})${
this.layer ? ` (${this.layer})` : ''
} ${this._name}@${this._version} = ${this._request}`;
}

/**
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
override readableIdentifier(requestShortener: RequestShortener): string {
return `provide shared module (${this._shareScope}) ${this._name}@${
this._version
} = ${requestShortener.shorten(this._request)}`;
return `provide shared module (${this._shareScope})${
this.layer ? ` (${this.layer})` : ''
} ${this._name}@${this._version} = ${requestShortener.shorten(this._request)}`;
}

/**
Expand All @@ -107,7 +111,6 @@ class ProvideSharedModule extends Module {
* @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
* @returns {void}
*/
// @ts-ignore
override needBuild(
context: NeedBuildContext,
callback: (error?: WebpackError | null, needsRebuild?: boolean) => void,
Expand All @@ -123,7 +126,6 @@ class ProvideSharedModule extends Module {
* @param {function(WebpackError=): void} callback callback function
* @returns {void}
*/
// @ts-ignore
override build(
options: WebpackOptions,
compilation: Compilation,
Expand Down Expand Up @@ -168,7 +170,6 @@ class ProvideSharedModule extends Module {
* @param {CodeGenerationContext} context context for code generation
* @returns {CodeGenerationResult} result
*/
// @ts-ignore
override codeGeneration({
runtimeTemplate,
moduleGraph,
Expand All @@ -177,14 +178,12 @@ class ProvideSharedModule extends Module {
const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
const moduleGetter = this._eager
? runtimeTemplate.syncModuleFactory({
//@ts-ignore
dependency: this.dependencies[0],
chunkGraph,
request: this._request,
runtimeRequirements,
})
: runtimeTemplate.asyncModuleFactory({
//@ts-ignore
block: this.blocks[0],
chunkGraph,
request: this._request,
Expand Down Expand Up @@ -231,6 +230,7 @@ class ProvideSharedModule extends Module {
write(this._requiredVersion);
write(this._strictVersion);
write(this._singleton);
write(this.layer);
super.serialize(context);
}

Expand All @@ -249,6 +249,7 @@ class ProvideSharedModule extends Module {
read(),
read(),
read(),
read(),
);
obj.deserialize(context);
return obj;
Expand Down
Loading
Loading