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

feat(enhanced): support layers in consume share plugin #3276

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5b107fa
feat(enhanced): support layers
ScriptedAlchemy Nov 17, 2024
062706f
feat(enhanced): support layers
ScriptedAlchemy Nov 17, 2024
cfd9d01
feat(enhanced): layers for consume shared module
ScriptedAlchemy Nov 21, 2024
3e59c55
Merge branch 'main' into layers-support
ScriptedAlchemy Nov 21, 2024
536b110
Merge branch 'main' into layers-support
ScriptedAlchemy Nov 21, 2024
aed08e9
feat(enhanced): add issuerLayer support to consume shared
ScriptedAlchemy Nov 22, 2024
9fb164b
chore(enhanced): update test
ScriptedAlchemy Nov 25, 2024
a9d899c
chore(enhanced): update test
ScriptedAlchemy Nov 25, 2024
33a36d1
feat(enhanced): ConsumeSharedPlugin issuerLayer support
ScriptedAlchemy Nov 25, 2024
f0c5c50
chore: update tests for layer combos
ScriptedAlchemy Nov 25, 2024
578adbb
chore: update tests for layer combos
ScriptedAlchemy Nov 25, 2024
31f08d0
chore: update tests for layer combos
ScriptedAlchemy Nov 25, 2024
066ffa6
feat(enhanced): support direct layer
ScriptedAlchemy Nov 25, 2024
b70eb8d
Merge branch 'main' into layers-support
ScriptedAlchemy Nov 25, 2024
358ba00
fix(enhanced): update share options of share plugin
ScriptedAlchemy Nov 25, 2024
94d72ed
fix(enhanced): update share options of share plugin
ScriptedAlchemy Nov 25, 2024
91dbb12
chore(enhanced): refactor layers tests
ScriptedAlchemy Nov 25, 2024
514cf03
chore(enhanced): remove layer options from provider
ScriptedAlchemy Nov 25, 2024
359b0ca
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Nov 25, 2024
24ef6d0
Delete .cursorrules
ScriptedAlchemy Nov 25, 2024
9d9bff2
feat(enhanced): support layers in consume share
ScriptedAlchemy Nov 26, 2024
40ae817
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Nov 26, 2024
d1c68b3
fix(enhanced): rename requiredLayer to layer
ScriptedAlchemy Dec 2, 2024
22964f8
Merge remote-tracking branch 'origin/consume-share-layers' into consu…
ScriptedAlchemy Dec 2, 2024
9884030
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Dec 2, 2024
3d8fa21
refactor(enhanced): pr review of consume share layering
ScriptedAlchemy Dec 3, 2024
69a2a52
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Dec 3, 2024
04b8ffd
chore: locks
ScriptedAlchemy Dec 3, 2024
7852878
feat(enhanced): add request to consume share (#3307)
ScriptedAlchemy Dec 5, 2024
f35b1b1
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Dec 5, 2024
b8df73e
chore: remove tt
ScriptedAlchemy Dec 9, 2024
b4b5a7b
chore: remove unused share
ScriptedAlchemy Dec 9, 2024
6f8f45e
refactor(enhanced): refactor type locations
ScriptedAlchemy Dec 10, 2024
875f516
feat(enhanced): ProvideSharedPlugin loader layer support (#3334)
ScriptedAlchemy Dec 12, 2024
673f6ca
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Dec 12, 2024
20bae5b
chore: locks
ScriptedAlchemy Dec 12, 2024
a5774c8
chore(enhanced): add share plugin test
ScriptedAlchemy Dec 13, 2024
d44acbd
chore(enhanced): add share plugin test
ScriptedAlchemy Dec 13, 2024
1a33ac7
chore(enhanced): add share plugin test
ScriptedAlchemy Dec 13, 2024
1fc2b05
thing
ScriptedAlchemy Dec 13, 2024
29f6d73
thing
ScriptedAlchemy Dec 13, 2024
22127ee
chore(node): lint
ScriptedAlchemy Dec 13, 2024
8f9fb99
chore(node): lint
ScriptedAlchemy Dec 13, 2024
243f1bc
Merge branch 'main' into consume-share-layers
ScriptedAlchemy Dec 13, 2024
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
5 changes: 5 additions & 0 deletions .changeset/brown-badgers-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/enhanced': minor
---

support request option on ConsumeSharePlugin. Allows matching requests like the object key of shared does
5 changes: 5 additions & 0 deletions .changeset/shy-snails-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/enhanced': minor
---

Layer support for Provide Share Plugin
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ packages/enhanced/test/js
# storybook cases
!apps/rslib-module/@mf-types/**

**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*

# Federation
**/.federation
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +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",
"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 Expand Up @@ -228,6 +229,7 @@
"vue-tsc": "^2.0.26",
"wait-on": "^7.2.0",
"webpack": "5.93.0",
"webpack-cli": "^5.1.4",
"webpack-virtual-modules": "0.6.2",
"whatwg-fetch": "^3.6.20",
"yargs": "^17.7.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export type ConsumeOptions = {
/**
* fallback request
*/
import?: string | undefined;
/**
* resolved fallback request
*/
importResolved?: string | undefined;
/**
* The actual request to use for importing the module. If not specified, the property name/key will be used.
*/
request?: string;
/**
* global share key
*/
shareKey: string;
/**
* share scope
*/
shareScope: string;
/**
* version requirement
*/
requiredVersion:
| import('webpack/lib/util/semver').SemVerRange
| false
| undefined;
/**
* package name to determine required version automatically
*/
packageName: string;
/**
* don't use shared version even if version isn't valid
*/
strictVersion: boolean;
/**
* use single global version
*/
singleton: boolean;
/**
* include the fallback module in a sync way
*/
eager: boolean;
/**
* 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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ export interface ConsumesConfig {
* 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;
/**
* Issuer layer in which the module should be resolved.
*/
issuerLayer?: string;
/**
* 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;
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ export interface SharedConfig {
* Version of the provided module. Will replace lower matching versions, but not higher.
*/
version?: false | string;
/**
* Issuer layer in which the module should be resolved.
*/
issuerLayer?: string;
/**
* Layer for the shared module.
*/
layer?: string;
/**
* The actual request to use for importing the module. Defaults to the property name.
*/
request?: string;
}
64 changes: 12 additions & 52 deletions packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
import ConsumeSharedFallbackDependency from './ConsumeSharedFallbackDependency';
import { normalizeConsumeShareOptions } from './utils';
import { WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE } from '../Constants';
import type { ConsumeOptions } from '../../declarations/plugins/sharing/ConsumeSharedModule';

const { rangeToString, stringifyHoley } = require(
normalizeWebpackPath('webpack/lib/util/semver'),
Expand All @@ -38,48 +39,6 @@ const makeSerializable = require(
normalizeWebpackPath('webpack/lib/util/makeSerializable'),
) as typeof import('webpack/lib/util/makeSerializable');

export type ConsumeOptions = {
/**
* fallback request
*/
import?: string | undefined;
/**
* resolved fallback request
*/
importResolved?: string | undefined;
/**
* global share key
*/
shareKey: string;
/**
* share scope
*/
shareScope: string;
/**
* version requirement
*/
requiredVersion:
| import('webpack/lib/util/semver').SemVerRange
| false
| undefined;
/**
* package name to determine required version automatically
*/
packageName: string;
/**
* don't use shared version even if version isn't valid
*/
strictVersion: boolean;
/**
* use single global version
*/
singleton: boolean;
/**
* include the fallback module in a sync way
*/
eager: boolean;
};

/**
* @typedef {Object} ConsumeOptions
* @property {string=} import fallback request
Expand All @@ -91,6 +50,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 All @@ -103,7 +64,11 @@ class ConsumeSharedModule extends Module {
* @param {ConsumeOptions} options consume options
*/
constructor(context: string, options: ConsumeOptions) {
super(WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE, context);
super(
WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE,
context,
options.layer ?? undefined,
);
this.options = options;
}

Expand All @@ -119,10 +84,11 @@ class ConsumeSharedModule extends Module {
strictVersion,
singleton,
eager,
layer,
} = this.options;
return `${WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE}|${shareScope}|${shareKey}|${
requiredVersion && rangeToString(requiredVersion)
}|${strictVersion}|${importResolved}|${singleton}|${eager}`;
}|${strictVersion}|${importResolved}|${singleton}|${eager}|${layer}`;
}

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

/**
Expand All @@ -166,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 @@ -182,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 @@ -225,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 @@ -257,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 @@ -266,7 +227,6 @@ class ConsumeSharedModule extends Module {
} else {
const block = this.blocks[0];
fallbackCode = runtimeTemplate.asyncModuleFactory({
// @ts-ignore
block,
chunkGraph,
runtimeRequirements,
Expand Down
Loading
Loading