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: recursive search for shared version #2681

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/lucky-fans-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/enhanced': minor
---

Enable recursive search for shared dependency version
85 changes: 59 additions & 26 deletions packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const WebpackError = require(
const createSchemaValidation = require(
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
) as typeof import('webpack/lib/util/create-schema-validation');
const { join, dirname } = require(
normalizeWebpackPath('webpack/lib/util/fs'),
) as typeof import('webpack/lib/util/fs');

const validate = createSchemaValidation(
//eslint-disable-next-line
Expand Down Expand Up @@ -234,43 +237,73 @@ class ConsumeSharedPlugin {
packageName = match[0];
}

getDescriptionFile(
compilation.inputFileSystem,
context,
['package.json'],
(err, result) => {
if (err) {
requiredVersionWarning(
`Unable to read description file: ${err}`,
);
return resolve(undefined);
}
//@ts-ignore
const { data, path: descriptionPath } = result;
if (!data) {
const getDescriptionFileCallback: Parameters<
typeof getDescriptionFile
>[3] = (err, result) => {
if (err) {
requiredVersionWarning(
`Unable to read description file: ${err}`,
);
return resolve(undefined);
}
//@ts-ignore
const { data, path: descriptionPath } = result;
const currentDirectory = dirname(
compilation.inputFileSystem,
descriptionPath,
);
const parentDirectory = dirname(
compilation.inputFileSystem,
currentDirectory,
);
if (!data) {
if (currentDirectory === parentDirectory) {
requiredVersionWarning(
`Unable to find description file in ${context}.`,
);
return resolve(undefined);
}
//@ts-ignore
if (data.name === packageName) {
// Package self-referencing
return resolve(undefined);
}
const requiredVersion = getRequiredVersionFromDescriptionFile(
data,
packageName,

return getDescriptionFile(
compilation.inputFileSystem,
parentDirectory,
['package.json'],
getDescriptionFileCallback,
);
if (typeof requiredVersion !== 'string') {
}
//@ts-ignore
if (data.name === packageName) {
// Package self-referencing
return resolve(undefined);
}
const requiredVersion = getRequiredVersionFromDescriptionFile(
data,
packageName,
);
if (typeof requiredVersion !== 'string') {
if (currentDirectory === parentDirectory) {
requiredVersionWarning(
`Unable to find required version for "${packageName}" in description file (${descriptionPath}). It need to be in dependencies, devDependencies or peerDependencies.`,
);
return resolve(undefined);
}
// @ts-ignore webpack internal semver has some issue, use runtime semver , related issue: https://github.com/webpack/webpack/issues/17756
resolve(requiredVersion);
},

return getDescriptionFile(
compilation.inputFileSystem,
parentDirectory,
['package.json'],
getDescriptionFileCallback,
);
}
// @ts-ignore webpack internal semver has some issue, use runtime semver , related issue: https://github.com/webpack/webpack/issues/17756
resolve(requiredVersion);
};

getDescriptionFile(
compilation.inputFileSystem,
context,
['package.json'],
getDescriptionFileCallback,
);
}),
]).then(([importResolved, requiredVersion]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
it('should provide and consume a normal nested library async', async () => {
expect(await import('lib1')).toEqual(
expect.objectContaining({
default: 'lib1',
}),
);
});

it('should provide and consume a normal library async', async () => {
expect(await import('lib4')).toEqual(
expect.objectContaining({
default: 'lib4',
}),
);
});

it('should provide and consume a renamed nested library sync', () => {
expect(require('lib-two')).toEqual(
expect.objectContaining({
default: 'lib2',
}),
);
});

it('should provide and consume a renamed library sync', () => {
expect(require('lib-five')).toEqual(
expect.objectContaining({
default: 'lib5',
}),
);
});

it('should provide and consume a normal nested library async in a separate shareScope', async () => {
expect(await import('lib3')).toEqual(
expect.objectContaining({
default: 'lib3',
}),
);
expect(
__webpack_share_scopes__.default && __webpack_share_scopes__.default.lib3,
).toBe(undefined);
expect(typeof __webpack_share_scopes__.other.lib3).toBe('object');
});

it('should provide and consume a relative request async', async () => {
expect(await import('./relative1')).toEqual(
expect.objectContaining({
default: 'rel1',
}),
);
});

it('should consume a remapped relative request async', async () => {
if (Math.random() < 0) require('store');
expect(await import('./relative2')).toEqual(
expect.objectContaining({
default: 'store',
}),
);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"lib3": "^1.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'rel1';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'rel2';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"lib3": "^1.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { SharePlugin } = require('../../../../dist/src');

module.exports = {
mode: 'development',
devtool: false,
context: `${__dirname}/app1`,
plugins: [
new SharePlugin({
shared: {
lib1: '^1.0.0',
'lib-two': {
import: 'lib2',
requiredVersion: '^1.0.0',
version: '1.3.4',
strictVersion: true,
eager: true,
},
lib3: {
shareScope: 'other',
},
lib4: '^1.0.0',
'lib-five': {
import: 'lib5',
requiredVersion: '^1.0.0',
version: '1.3.4',
strictVersion: true,
eager: true,
},
'./relative1': {
import: './relative1',
version: false,
},
'./relative2': {
import: false,
shareKey: 'store',
version: '0',
requiredVersion: false,
strictVersion: true,
},
store: '0',
},
}),
],
};
Loading