Skip to content

Commit 4c4c349

Browse files
test(enhanced): add aliasConsumption + filters ConfigCase and unit tests
Add share-with-aliases-filters config case to exercise alias-aware consumption with include/exclude; add unit tests that simulate deep path/absolute requests; fix ConsumeSharedPlugin version filter guard.
1 parent 05c3dbf commit 4c4c349

File tree

11 files changed

+244
-2
lines changed

11 files changed

+244
-2
lines changed

packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,14 @@ class ConsumeSharedPlugin {
321321
return resolveFilter(consumedModule);
322322
}
323323
const { data } = result || {};
324-
if (!data || !data['version'] || data['name'] !== request) {
324+
// If pkg data is missing or lacks version, keep module
325+
if (!data || !data['version']) {
325326
return resolveFilter(consumedModule);
326327
}
328+
// For deep-path keys (alias consumption), the request may be a path like
329+
// "next/dist/compiled/react" or an absolute resource path. In that case,
330+
// data['name'] will be the package name (e.g., "next"). Do not require
331+
// strict equality with the request string; rely solely on semver check.
327332

328333
if (
329334
config.include &&
@@ -407,7 +412,8 @@ class ConsumeSharedPlugin {
407412
return resolveFilter(consumedModule);
408413
}
409414
const { data } = result || {};
410-
if (!data || !data['version'] || data['name'] !== request) {
415+
// If pkg data is missing or lacks version, keep module
416+
if (!data || !data['version']) {
411417
return resolveFilter(consumedModule);
412418
}
413419

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// No build errors expected for this case
2+
module.exports = [];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
it('should load direct compiled stub for aliased react when excluded by version filter', async () => {
2+
const mod = await import('react');
3+
// Validate we loaded the direct compiled stub (not the shared instance)
4+
expect(mod.name).toBe('compiled-react');
5+
expect(mod.source).toBe('node_modules/next/dist/compiled/react');
6+
expect(mod.createElement()).toBe('DIRECT-compiled-react-element');
7+
});
8+
9+
it('should share aliased react-allowed when included by version filter', async () => {
10+
const viaAlias = await import('react-allowed');
11+
const direct = await import('next/dist/compiled/react-allowed');
12+
13+
// Identity and behavior checks
14+
expect(viaAlias.name).toBe('compiled-react-allowed');
15+
expect(viaAlias.source).toBe('node_modules/next/dist/compiled/react-allowed');
16+
expect(viaAlias.createElement()).toBe(
17+
'SHARED-compiled-react-allowed-element',
18+
);
19+
20+
// Identity should match direct import as well
21+
expect(viaAlias.name).toBe(direct.name);
22+
expect(viaAlias.source).toBe(direct.source);
23+
});
24+
25+
module.exports = {
26+
testName: 'share-with-aliases-filters',
27+
};

packages/enhanced/test/configCases/sharing/share-with-aliases-filters/node_modules/next/dist/compiled/react-allowed.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/enhanced/test/configCases/sharing/share-with-aliases-filters/node_modules/next/dist/compiled/react.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/enhanced/test/configCases/sharing/share-with-aliases-filters/node_modules/next/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/enhanced/test/configCases/sharing/share-with-aliases-filters/node_modules/react/package.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test-share-with-aliases-filters",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"react": "18.2.0"
6+
}
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Expected warnings for aliasConsumption + include/exclude filters scenario
2+
module.exports = [];
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { ModuleFederationPlugin } = require('../../../../dist/src');
2+
const path = require('path');
3+
4+
module.exports = {
5+
mode: 'development',
6+
devtool: false,
7+
resolve: {
8+
alias: {
9+
// Alias bare imports to compiled targets (simulating Next.js-style aliases)
10+
react: path.resolve(
11+
__dirname,
12+
'node_modules/next/dist/compiled/react.js',
13+
),
14+
'react-allowed': path.resolve(
15+
__dirname,
16+
'node_modules/next/dist/compiled/react-allowed.js',
17+
),
18+
},
19+
},
20+
plugins: [
21+
new ModuleFederationPlugin({
22+
name: 'share-with-aliases-filters',
23+
experiments: { asyncStartup: false, aliasConsumption: true },
24+
shared: {
25+
// Exclude 18.x: alias 'react' -> should load fallback (direct compiled stub) via import
26+
'next/dist/compiled/react': {
27+
import: 'next/dist/compiled/react',
28+
requiredVersion: false,
29+
exclude: { version: '^18.0.0' },
30+
},
31+
// Include 18.x: alias 'react-allowed' -> should be shared
32+
'next/dist/compiled/react-allowed': {
33+
import: 'next/dist/compiled/react-allowed',
34+
requiredVersion: false,
35+
include: { version: '^18.0.0' },
36+
},
37+
},
38+
}),
39+
],
40+
};

0 commit comments

Comments
 (0)