Skip to content

Commit

Permalink
fix: esm files should use .mjs extention (#3159)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy authored Nov 5, 2024
1 parent 54bfd1c commit 206b56d
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 28 deletions.
11 changes: 11 additions & 0 deletions .changeset/popular-pillows-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@module-federation/webpack-bundler-runtime': patch
'@module-federation/data-prefetch': patch
'@module-federation/runtime-tools': patch
'@module-federation/managers': patch
'@module-federation/manifest': patch
'@module-federation/runtime': patch
'@module-federation/sdk': patch
---

disable hoistTransitiveImports for better tree shake
12 changes: 6 additions & 6 deletions packages/data-prefetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
},
"exports": {
".": {
"import": "./dist/index.esm.js",
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.cjs.d.ts"
},
"./react": {
"import": "./dist/react.esm.js",
"import": "./dist/react.esm.mjs",
"require": "./dist/react.cjs.js",
"types": "./dist/react.cjs.d.ts"
},
"./cli": {
"import": "./dist/cli.esm.js",
"import": "./dist/cli.esm.mjs",
"require": "./dist/cli.cjs.js",
"types": "./dist/cli.cjs.d.ts"
},
"./babel-plugin": {
"import": "./dist/babel.esm.js",
"import": "./dist/babel.esm.mjs",
"require": "./dist/babel.cjs.js",
"types": "./dist/babel.cjs.d.ts"
},
"./universal": {
"import": "./dist/universal.esm.js",
"import": "./dist/universal.esm.mjs",
"require": "./dist/universal.cjs.js",
"types": "./dist/universal.cjs.d.ts"
}
Expand All @@ -58,7 +58,7 @@
}
},
"main": "dist/index.cjs",
"module": "dist/index.esm.js",
"module": "dist/index.esm.mjs",
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
Expand Down
16 changes: 16 additions & 0 deletions packages/data-prefetch/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ module.exports = (rollupConfig, _projectOptions) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
c.format === 'esm'
? c.entryFileNames.replace('.js', '.mjs')
: c.entryFileNames,
chunkFileNames:
c.format === 'esm'
? c.chunkFileNames.replace('.js', '.mjs')
: c.chunkFileNames,
}));
} else {
rollupConfig.output = {
Expand All @@ -39,6 +47,14 @@ module.exports = (rollupConfig, _projectOptions) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.entryFileNames.replace('.js', '.mjs')
: rollupConfig.output.entryFileNames,
chunkFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.chunkFileNames.replace('.js', '.mjs')
: rollupConfig.output.chunkFileNames,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/data-prefetch/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class PrefetchPlugin implements WebpackPluginInstance {
this.options.runtimePlugins = [];
}

const runtimePath = path.resolve(__dirname, './plugin.esm.js');
const runtimePath = path.resolve(__dirname, './plugin.esm.mjs');
if (!this.options.runtimePlugins?.includes(runtimePath)) {
this.options.runtimePlugins!.push(runtimePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class FederationRuntimePlugin {
}

if (isHoisted) {
runtimePath = runtimePath.replace('.cjs', '.esm');
runtimePath = runtimePath.replace('.cjs.js', '.esm.mjs');
}

const alias: any = compiler.options.resolve.alias || {};
Expand Down Expand Up @@ -439,7 +439,7 @@ class FederationRuntimePlugin {
if (this.options?.experiments?.federationRuntime === 'hoisted') {
this.bundlerRuntimePath = this.bundlerRuntimePath.replace(
'.cjs.js',
'.esm.js',
'.esm.mjs',
);

new EmbedFederationRuntimePlugin().apply(compiler);
Expand All @@ -450,7 +450,7 @@ class FederationRuntimePlugin {
/@module-federation\/runtime/,
(resolveData) => {
if (/webpack-bundler-runtime/.test(resolveData.contextInfo.issuer)) {
resolveData.request = RuntimePath.replace('cjs', 'esm');
resolveData.request = RuntimePath.replace('cjs.js', 'esm.mjs');

if (resolveData.createData) {
resolveData.createData.request = resolveData.request;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ModuleFederationPlugin implements RspackPluginInstance {
).apply(compiler);

const runtimeESMPath = require.resolve(
'@module-federation/runtime/dist/index.esm.js',
'@module-federation/runtime/dist/index.esm.mjs',
{ paths: [options.implementation] },
);

Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.6.16",
"author": "zhanghang <[email protected]>",
"main": "./dist/index.cjs",
"module": "./dist/index.esm.js",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.cjs.d.ts",
"license": "MIT",
"publishConfig": {
Expand All @@ -12,17 +12,17 @@
"exports": {
".": {
"types": "./dist/index.cjs.d.ts",
"import": "./dist/index.esm.js",
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs.js"
},
"./runtime": {
"types": "./dist/runtime.cjs.d.ts",
"import": "./dist/runtime.esm.js",
"import": "./dist/runtime.esm.mjs",
"require": "./dist/runtime.cjs.js"
},
"./webpack-bundler-runtime": {
"types": "./dist/webpack-bundler-runtime.cjs.d.ts",
"import": "./dist/webpack-bundler-runtime.esm.js",
"import": "./dist/webpack-bundler-runtime.esm.mjs",
"require": "./dist/webpack-bundler-runtime.cjs.js"
},
"./*": "./*"
Expand Down
16 changes: 16 additions & 0 deletions packages/runtime-tools/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ module.exports = (rollupConfig) => {
rollupConfig.output = rollupConfig.output.map((c) => ({
...c,
hoistTransitiveImports: false,
entryFileNames:
c.format === 'esm'
? c.entryFileNames.replace('.js', '.mjs')
: c.entryFileNames,
chunkFileNames:
c.format === 'esm'
? c.chunkFileNames.replace('.js', '.mjs')
: c.chunkFileNames,
}));
} else {
rollupConfig.output = {
...rollupConfig.output,
hoistTransitiveImports: false,
entryFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.entryFileNames.replace('.js', '.mjs')
: rollupConfig.output.entryFileNames,
chunkFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.chunkFileNames.replace('.js', '.mjs')
: rollupConfig.output.chunkFileNames,
};
}

Expand Down
10 changes: 5 additions & 5 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.6.16",
"author": "zhouxiao <[email protected]>",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.cjs.d.ts",
"license": "MIT",
"publishConfig": {
Expand All @@ -16,22 +16,22 @@
"exports": {
".": {
"types": "./dist/index.cjs.d.ts",
"import": "./dist/index.esm.js",
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs.js"
},
"./helpers": {
"types": "./dist/helpers.cjs.d.ts",
"import": "./dist/helpers.esm.js",
"import": "./dist/helpers.esm.mjs",
"require": "./dist/helpers.cjs.js"
},
"./types": {
"types": "./dist/types.cjs.d.ts",
"import": "./dist/types.esm.js",
"import": "./dist/types.esm.mjs",
"require": "./dist/types.cjs.js"
},
"./embedded": {
"types": "./dist/embedded.cjs.d.ts",
"import": "./dist/embedded.esm.js",
"import": "./dist/embedded.esm.mjs",
"require": "./dist/embedded.cjs.js"
},
"./*": "./*"
Expand Down
18 changes: 17 additions & 1 deletion packages/runtime/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const copy = require('rollup-plugin-copy');

const FEDERATION_DEBUG = process.env.FEDERATION_DEBUG || '';

module.exports = (rollupConfig) => {
module.exports = (rollupConfig, projectOptions) => {
rollupConfig.input = {
index: 'packages/runtime/src/index.ts',
types: 'packages/runtime/src/types.ts',
Expand All @@ -30,6 +30,14 @@ module.exports = (rollupConfig) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
c.format === 'esm'
? c.entryFileNames.replace('.js', '.mjs')
: c.entryFileNames,
chunkFileNames:
c.format === 'esm'
? c.chunkFileNames.replace('.js', '.mjs')
: c.chunkFileNames,
}));
} else {
rollupConfig.output = {
Expand All @@ -40,6 +48,14 @@ module.exports = (rollupConfig) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.entryFileNames.replace('.js', '.mjs')
: rollupConfig.output.entryFileNames,
chunkFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.chunkFileNames.replace('.js', '.mjs')
: rollupConfig.output.chunkFileNames,
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
"author": "zhanghang <[email protected]>",
"sideEffects": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.cjs.d.ts",
"exports": {
".": {
"types": "./dist/index.cjs.d.ts",
"import": "./dist/index.esm.js",
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs.js"
},
"./normalize-webpack-path": {
"types": "./dist/normalize-webpack-path.cjs.d.ts",
"import": "./dist/normalize-webpack-path.esm.js",
"import": "./dist/normalize-webpack-path.esm.mjs",
"require": "./dist/normalize-webpack-path.cjs.js"
}
},
Expand Down
16 changes: 16 additions & 0 deletions packages/sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ module.exports = (rollupConfig, _projectOptions) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
c.format === 'esm'
? c.entryFileNames.replace('.js', '.mjs')
: c.entryFileNames,
chunkFileNames:
c.format === 'esm'
? c.chunkFileNames.replace('.js', '.mjs')
: c.chunkFileNames,
}));
} else {
rollupConfig.output = {
Expand All @@ -28,6 +36,14 @@ module.exports = (rollupConfig, _projectOptions) => {
}
},
hoistTransitiveImports: false,
entryFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.entryFileNames.replace('.js', '.mjs')
: rollupConfig.output.entryFileNames,
chunkFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.chunkFileNames.replace('.js', '.mjs')
: rollupConfig.output.chunkFileNames,
};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/webpack-bundler-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
},
"author": "zhanghang <[email protected]>",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.cjs.d.ts",
"dependencies": {
"@module-federation/runtime": "workspace:*",
"@module-federation/sdk": "workspace:*"
},
"exports": {
".": {
"import": "./dist/index.esm.js",
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs.js"
},
"./constant": {
"import": "./dist/constant.esm.js",
"import": "./dist/constant.esm.mjs",
"require": "./dist/constant.cjs.js"
},
"./container": {
"import": "./dist/container.esm.js",
"import": "./dist/container.esm.mjs",
"require": "./dist/container.cjs.js"
},
"./*": "./*"
Expand Down
16 changes: 16 additions & 0 deletions packages/webpack-bundler-runtime/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,28 @@ module.exports = (rollupConfig, projectOptions) => {
...c,
manualChunks: (id) => manualChunks(id, projectOptions),
hoistTransitiveImports: false,
entryFileNames:
c.format === 'esm'
? c.entryFileNames.replace('.js', '.mjs')
: c.entryFileNames,
chunkFileNames:
c.format === 'esm'
? c.chunkFileNames.replace('.js', '.mjs')
: c.chunkFileNames,
}));
} else {
rollupConfig.output = {
...rollupConfig.output,
manualChunks: (id) => manualChunks(id, projectOptions),
hoistTransitiveImports: false,
entryFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.entryFileNames.replace('.js', '.mjs')
: rollupConfig.output.entryFileNames,
chunkFileNames:
rollupConfig.output.format === 'esm'
? rollupConfig.output.chunkFileNames.replace('.js', '.mjs')
: rollupConfig.output.chunkFileNames,
};
}

Expand Down

0 comments on commit 206b56d

Please sign in to comment.