Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ export interface ProvidesConfig {
* Version of the provided module. Will replace lower matching versions, but not higher.
*/
version?: false | string;
/**
* Version requirement from module in share scope.
*/
requiredVersion?: false | string;
/**
* Allow only a single version of the shared module in share scope (disabled by default).
*/
singleton?: boolean;
/**
* 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;
}
18 changes: 18 additions & 0 deletions packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,39 @@ class ProvideSharedDependency extends Dependency {
version: string | false;
request: string;
eager: boolean;
requiredVersion: string | false;
strictVersion: boolean;
singleton: boolean;

/**
* @param {string} shareScope share scope
* @param {string} name module name
* @param {string | false} version version
* @param {string} request request
* @param {boolean} eager true, if this is an eager 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
*/
constructor(
shareScope: string,
name: string,
version: string | false,
request: string,
eager: boolean,
requiredVersion: string | false,
strictVersion: boolean,
singleton: boolean,
) {
super();
this.shareScope = shareScope;
this.name = name;
this.version = version;
this.request = request;
this.eager = eager;
this.requiredVersion = requiredVersion;
this.strictVersion = strictVersion;
this.singleton = singleton;
}

override get type(): string {
Expand All @@ -68,6 +80,9 @@ class ProvideSharedDependency extends Dependency {
context.write(this.request);
context.write(this.version);
context.write(this.eager);
context.write(this.requiredVersion);
context.write(this.strictVersion);
context.write(this.singleton);
super.serialize(context);
}

Expand All @@ -85,6 +100,9 @@ class ProvideSharedDependency extends Dependency {
read(),
read(),
read(),
read(),
read(),
read(),
);
//@ts-ignore
this.shareScope = context.read();
Expand Down
32 changes: 31 additions & 1 deletion packages/enhanced/src/lib/sharing/ProvideSharedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class ProvideSharedModule extends Module {
private _version: string | false;
private _request: string;
private _eager: boolean;
private _requiredVersion: string | false;
private _strictVersion: boolean;
private _singleton: boolean;

/**
* @constructor
Expand All @@ -47,20 +50,29 @@ class ProvideSharedModule extends Module {
* @param {string | false} version version
* @param {string} request request to the provided module
* @param {boolean} eager include the module in sync way
* @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
*/
constructor(
shareScope: string,
name: string,
version: string | false,
request: string,
eager: boolean,
requiredVersion: string | false,
strictVersion: boolean,
singleton: boolean,
) {
super(WEBPACK_MODULE_TYPE_PROVIDE);
this._shareScope = shareScope;
this._name = name;
this._version = version;
this._request = request;
this._eager = eager;
this._requiredVersion = requiredVersion;
this._strictVersion = strictVersion;
this._singleton = singleton;
}

/**
Expand Down Expand Up @@ -196,6 +208,12 @@ class ProvideSharedModule extends Module {
request: this._request,
getter: moduleGetter,
shareScope: [this._shareScope],
shareConfig: {
eager: this._eager,
requiredVersion: this._requiredVersion,
strictVersion: this._strictVersion,
singleton: this._singleton,
},
});
return { sources, data, runtimeRequirements };
}
Expand All @@ -210,6 +228,9 @@ class ProvideSharedModule extends Module {
write(this._version);
write(this._request);
write(this._eager);
write(this._requiredVersion);
write(this._strictVersion);
write(this._singleton);
super.serialize(context);
}

Expand All @@ -219,7 +240,16 @@ class ProvideSharedModule extends Module {
*/
static deserialize(context: ObjectDeserializerContext): ProvideSharedModule {
const { read } = context;
const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
const obj = new ProvideSharedModule(
read(),
read(),
read(),
read(),
read(),
read(),
read(),
read(),
);
obj.deserialize(context);
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ProvideSharedModuleFactory extends ModuleFactory {
dep.version,
dep.request,
dep.eager,
dep.requiredVersion,
dep.strictVersion,
dep.singleton,
),
});
}
Expand Down
35 changes: 22 additions & 13 deletions packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import { parseOptions } from '../container/options';
import ProvideForSharedDependency from './ProvideForSharedDependency';
import ProvideSharedDependency from './ProvideSharedDependency';
import ProvideSharedModuleFactory from './ProvideSharedModuleFactory';
import type { ProvideSharedPluginOptions } from '../../declarations/plugins/sharing/ProvideSharedPlugin';
import type {
ProvideSharedPluginOptions,
ProvidesConfig,
} from '../../declarations/plugins/sharing/ProvideSharedPlugin';
import FederationRuntimePlugin from '../container/runtime/FederationRuntimePlugin';
import checkOptions from '../../schemas/sharing/ProviderSharedPlugin.check';
import schema from '../../schemas/sharing/ProviderSharedPlugin';

const createSchemaValidation = require(
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
Expand All @@ -23,12 +28,7 @@ const WebpackError = require(
normalizeWebpackPath('webpack/lib/WebpackError'),
) as typeof import('webpack/lib/WebpackError');

export type ProvideOptions = {
shareKey: string;
shareScope: string;
version: string | undefined | false;
eager: boolean;
};
export type ProvideOptions = ProvidesConfig;
export type ResolvedProvideMap = Map<
string,
{
Expand All @@ -39,8 +39,8 @@ export type ResolvedProvideMap = Map<

const validate = createSchemaValidation(
//eslint-disable-next-line
require('webpack/schemas/plugins/sharing/ProvideSharedPlugin.check.js'),
() => require('webpack/schemas/plugins/sharing/ProvideSharedPlugin.json'),
checkOptions,
() => schema,
{
name: 'Provide Shared Plugin',
baseDataPath: 'options',
Expand Down Expand Up @@ -72,11 +72,14 @@ class ProvideSharedPlugin {
if (Array.isArray(item))
throw new Error('Unexpected array of provides');
/** @type {ProvideOptions} */
const result = {
const result: ProvideOptions = {
shareKey: item,
version: undefined,
shareScope: options.shareScope || 'default',
eager: false,
requiredVersion: false,
strictVersion: false,
singleton: false,
};
return result;
},
Expand All @@ -85,6 +88,9 @@ class ProvideSharedPlugin {
version: item.version,
shareScope: item.shareScope || options.shareScope || 'default',
eager: !!item.eager,
requiredVersion: item.requiredVersion || false,
strictVersion: item.strictVersion || false,
singleton: item.singleton || false,
}),
);
this._provides.sort(([a], [b]) => {
Expand Down Expand Up @@ -225,11 +231,14 @@ class ProvideSharedPlugin {
compiler.context,
//@ts-ignore
new ProvideSharedDependency(
config.shareScope,
config.shareKey,
config.shareScope!,
config.shareKey!,
version || false,
resource,
config.eager,
config.eager!,
config.requiredVersion!,
config.strictVersion!,
config.singleton!,
),
{
name: undefined,
Expand Down
3 changes: 3 additions & 0 deletions packages/enhanced/src/lib/sharing/SharePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class SharePlugin {
shareScope: options.shareScope,
version: options.version,
eager: options.eager,
requiredVersion: options.requiredVersion,
strictVersion: options.strictVersion,
singleton: options.singleton,
},
}));
//@ts-ignore
Expand Down
3 changes: 2 additions & 1 deletion packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class ShareRuntimeModule extends RuntimeModule {
"${sharedOption.name}" : {
version: ${sharedOption.version},
get: ${sharedOption.getter},
scope: ${JSON.stringify(sharedOption.shareScope)}
scope: ${JSON.stringify(sharedOption.shareScope)},
shareConfig: ${JSON.stringify(sharedOption.shareConfig)}
},
`;
}
Expand Down
Loading