From 6d7a50b285649e0baf908bd21f19142863c5b5f4 Mon Sep 17 00:00:00 2001 From: Robert Donnelly Date: Fri, 28 Jun 2024 10:12:13 +0100 Subject: [PATCH] chore: add tests for share plugin recursive search --- .changeset/lucky-fans-exist.md | 5 ++ .../share-plugin-monorepo/app1/index.js | 60 +++++++++++++++++++ .../app1/node_modules/lib4/index.js | 1 + .../app1/node_modules/lib4/package.json | 3 + .../app1/node_modules/lib5/index.js | 1 + .../app1/node_modules/store/index.js | 1 + .../app1/node_modules/store/package.json | 3 + .../share-plugin-monorepo/app1/package.json | 5 ++ .../share-plugin-monorepo/app1/relative1.js | 1 + .../share-plugin-monorepo/app1/relative2.js | 1 + .../node_modules/lib1/index.js | 1 + .../node_modules/lib1/package.json | 3 + .../node_modules/lib2/index.js | 1 + .../node_modules/lib3/index.js | 1 + .../node_modules/lib3/package.json | 3 + .../share-plugin-monorepo/package.json | 5 ++ .../share-plugin-monorepo/webpack.config.js | 44 ++++++++++++++ 17 files changed, 139 insertions(+) create mode 100644 .changeset/lucky-fans-exist.md create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib5/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative1.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative2.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib2/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/index.js create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/package.json create mode 100644 packages/enhanced/test/configCases/sharing/share-plugin-monorepo/webpack.config.js diff --git a/.changeset/lucky-fans-exist.md b/.changeset/lucky-fans-exist.md new file mode 100644 index 00000000000..de7f706ec30 --- /dev/null +++ b/.changeset/lucky-fans-exist.md @@ -0,0 +1,5 @@ +--- +'@module-federation/enhanced': minor +--- + +Enable recursive search for shared dependency version diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/index.js new file mode 100644 index 00000000000..a4fa7fc3d8d --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/index.js @@ -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', + }), + ); +}); diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/index.js new file mode 100644 index 00000000000..21a25a46fa5 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/index.js @@ -0,0 +1 @@ +export default "lib4"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/package.json new file mode 100644 index 00000000000..2a38ae1d1f4 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib4/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.1.1" +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib5/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib5/index.js new file mode 100644 index 00000000000..c845cd97c3c --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/lib5/index.js @@ -0,0 +1 @@ +export default "lib5"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/index.js new file mode 100644 index 00000000000..225383e5cc2 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/index.js @@ -0,0 +1 @@ +export default "store"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/package.json new file mode 100644 index 00000000000..ce04135d2cd --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/node_modules/store/package.json @@ -0,0 +1,3 @@ +{ + "version": "0" +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/package.json new file mode 100644 index 00000000000..759724dfd56 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "lib3": "^1.1.0" + } +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative1.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative1.js new file mode 100644 index 00000000000..6480b770466 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative1.js @@ -0,0 +1 @@ +export default 'rel1'; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative2.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative2.js new file mode 100644 index 00000000000..b70ff92e003 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/app1/relative2.js @@ -0,0 +1 @@ +export default 'rel2'; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/index.js new file mode 100644 index 00000000000..461d2376f4c --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/index.js @@ -0,0 +1 @@ +export default "lib1"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/package.json new file mode 100644 index 00000000000..2a38ae1d1f4 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib1/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.1.1" +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib2/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib2/index.js new file mode 100644 index 00000000000..c2a6f9581ff --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib2/index.js @@ -0,0 +1 @@ +export default "lib2"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/index.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/index.js new file mode 100644 index 00000000000..62fde4a705d --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/index.js @@ -0,0 +1 @@ +export default "lib3"; diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/package.json new file mode 100644 index 00000000000..2a38ae1d1f4 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/node_modules/lib3/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.1.1" +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/package.json b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/package.json new file mode 100644 index 00000000000..759724dfd56 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "lib3": "^1.1.0" + } +} diff --git a/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/webpack.config.js b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/webpack.config.js new file mode 100644 index 00000000000..d72ac4ea842 --- /dev/null +++ b/packages/enhanced/test/configCases/sharing/share-plugin-monorepo/webpack.config.js @@ -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', + }, + }), + ], +};