Skip to content

Commit

Permalink
feat(enhanced): add issuerLayer support to consume shared
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Nov 22, 2024
1 parent 536b110 commit aed08e9
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ export interface ConsumesConfig {
* Share a specific layer of the module, if the module supports layers.
*/
layer?: string;
/**
* Issuer layer for the shared module.
*/
issuerLayer?: string;
}
14 changes: 12 additions & 2 deletions packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export type ConsumeOptions = {
* Share a specific layer of the module, if the module supports layers.
*/
layer?: string | null;
/**
* Issuer layer in which the module should be resolved
*/
issuerLayer?: string | null;
};

/**
Expand All @@ -95,6 +99,8 @@ export type ConsumeOptions = {
* @property {boolean} strictVersion don't use shared version even if version isn't valid
* @property {boolean} singleton use single global version
* @property {boolean} eager include the fallback module in a sync way
* @property {string | null=} layer Share a specific layer of the module, if the module supports layers
* @property {string | null=} issuerLayer Issuer layer in which the module should be resolved
*/

const TYPES = new Set(['consume-shared']);
Expand Down Expand Up @@ -128,10 +134,11 @@ class ConsumeSharedModule extends Module {
singleton,
eager,
layer,
issuerLayer,
} = this.options;
return `${WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE}|${shareScope}|${shareKey}|${
requiredVersion && rangeToString(requiredVersion)
}|${strictVersion}|${importResolved}|${singleton}|${eager}|${layer}`;
}|${strictVersion}|${importResolved}|${singleton}|${eager}|${layer}|${issuerLayer}`;
}

/**
Expand All @@ -148,14 +155,17 @@ class ConsumeSharedModule extends Module {
singleton,
eager,
layer,
issuerLayer,
} = this.options;
return `consume shared module (${shareScope}) ${shareKey}@${
requiredVersion ? rangeToString(requiredVersion) : '*'
}${strictVersion ? ' (strict)' : ''}${singleton ? ' (singleton)' : ''}${
importResolved
? ` (fallback: ${requestShortener.shorten(importResolved)})`
: ''
}${eager ? ' (eager)' : ''}${layer ? ` (${layer})` : ''}`;
}${eager ? ' (eager)' : ''}${layer ? ` (${layer})` : ''}${
issuerLayer ? ` (issuer: ${issuerLayer})` : ''
}`;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ConsumeSharedPlugin {
singleton: false,
eager: false,
layer: undefined,
issuerLayer: undefined,
}
: // key is a request/key
// item is a version
Expand All @@ -101,6 +102,7 @@ class ConsumeSharedPlugin {
singleton: false,
eager: false,
layer: undefined,
issuerLayer: undefined,
};
return result;
},
Expand All @@ -118,7 +120,7 @@ class ConsumeSharedPlugin {
packageName: item.packageName,
singleton: !!item.singleton,
eager: !!item.eager,
layer: item.layer ? item.layer : undefined,
issuerLayer: item.issuerLayer ? item.issuerLayer : undefined,
}),
);
}
Expand Down Expand Up @@ -292,7 +294,7 @@ class ConsumeSharedPlugin {
normalModuleFactory.hooks.factorize.tapPromise(
PLUGIN_NAME,
async (resolveData: ResolveData): Promise<Module | undefined> => {
const { context, request, dependencies } = resolveData;
const { context, request, dependencies, contextInfo } = resolveData;
// wait for resolving to be complete
//@ts-ignore
return promise.then(() => {
Expand All @@ -302,8 +304,15 @@ class ConsumeSharedPlugin {
) {
return;
}
const match = unresolvedConsumes.get(request);

const match = unresolvedConsumes.get(
contextInfo.issuerLayer
? contextInfo.issuerLayer + request
: request,
);

if (match !== undefined) {
debugger;
return createConsumeSharedModule(context, request, match);
}
for (const [prefix, options] of prefixedConsumes) {
Expand Down
3 changes: 0 additions & 3 deletions packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ class ProvideSharedPlugin {
normalModuleFactory.hooks.module.tap(
'ProvideSharedPlugin',
(module, { resource, resourceResolveData }, resolveData) => {
if (resource && resource.includes('/react')) {
debugger;
}
if (resource && resolvedProvideMap.has(resource)) {
return module;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/enhanced/src/lib/sharing/resolveMatchedConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,18 @@ export async function resolveMatchedConfigs<T>(
// module request prefix
prefixed.set(request, config);
} else {
let req = request;
//@ts-ignore
if ('import' in config && config.import && request !== config.import) {
req = config.import as string;
}
//@ts-ignore
if ('issuerLayer' in config && config.issuerLayer) {
//@ts-ignore
req = config.issuerLayer + req;
}
// module request
unresolved.set(request, config);
unresolved.set(req, config);
}
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const validateConsume: ValidationFunction = function validate(
key !== 'shareScope' &&
key !== 'singleton' &&
key !== 'strictVersion' &&
key !== 'issuerLayer' &&
key !== 'layer'
) {
validate.errors = [{ params: { additionalProperty: key } }];
Expand Down
13 changes: 13 additions & 0 deletions packages/enhanced/src/schemas/sharing/ConsumeSharedPlugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
}
]
},
"issuerLayer": {
"description": "Layer in which the issuer should be.",
"type": "string",
"minLength": 1
},
"packageName": {
"description": "Package name to determine required version from description file. This is only needed when package name can't be automatically determined from request.",
"type": "string",
Expand Down Expand Up @@ -114,6 +119,14 @@
"description": "Share scope name used for all consumed modules (defaults to 'default').",
"type": "string",
"minLength": 1
},
"layer": {
"description": "Layer in which modules should be placed in.",
"type": "string"
},
"issuerLayer": {
"description": "Layer in which the issuer should be.",
"type": "string"
}
},
"required": ["consumes"]
Expand Down
6 changes: 4 additions & 2 deletions packages/enhanced/test/configCases/sharing/layers/otherApp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as React from 'react';
// import * as React from 'react';
//
// export default React;

export default React;
export default { version: 123 };
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ConsumeSharedPlugin } = require('../../../../dist/src');
const WConsumeSharedPlugin = require('webpack/lib/sharing/ConsumeSharedPlugin');

module.exports = {
mode: 'development',
Expand Down Expand Up @@ -27,7 +28,7 @@ module.exports = {
},
{
test: /\.js$/,
issuerLayer: 'consume-share-layer',
issuerLayer: 'react-layer',
layer: 'other-layer',
use: './uppercase-loader.js',
},
Expand All @@ -36,10 +37,10 @@ module.exports = {
plugins: [
new ConsumeSharedPlugin({
consumes: {
// react: {
// singleton: true,
// layer: 'react-layer',
// },
react: {
singleton: true,
issuerLayer: 'loader-layer',
},
otherReact: {
import: 'react',
shareKey: 'react',
Expand Down
Loading

0 comments on commit aed08e9

Please sign in to comment.