fix: esm files should use .mjs extention#3159
Conversation
🦋 Changeset detectedLatest commit: de79650 The changes in this PR will be included in the next version bump. This PR includes changesets to release 25 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for module-federation-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
e60e427 to
f9ed585
Compare
ed48cd5 to
680fc9e
Compare
There was a problem hiding this comment.
Summary
The core changes in this pull request focus on updating the file extensions used for ESM (ECMAScript Module) files in the module federation codebase. The changes ensure that ESM files use the correct .mjs extension, which addresses an issue where the incorrect .cjs extension was being used.
The changes include:
- Updating the file extension for ESM files in the
FederationRuntimePlugin.tsfile from.cjsto.mjs. - Changing the import path for the
@module-federation/runtimepackage to use the.mjsfile extension instead of.esm.jsin theModuleFederationPlugin.tsfile.
These changes help to improve the integration of the module federation functionality with the existing codebase and ensure that ESM files are properly recognized and used in the module federation runtime.
File Summaries
| File | Summary |
|---|---|
| packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts | The code changes update the file extension for ESM (ECMAScript Module) files from .cjs to .mjs. This ensures that the ESM files are properly identified and used in the module federation runtime, addressing an issue where the incorrect file extension was being used. |
| packages/rspack/src/ModuleFederationPlugin.ts | The changes in this file update the import path for the @module-federation/runtime package to use the .mjs file extension instead of .esm.js. This ensures that the ESM (ECMAScript Module) files are properly recognized and used in the module federation setup. |
There was a problem hiding this comment.
Incremental Review
Comments posted: 2
Configuration
Squadron Mode: essential
Commits Reviewed
680fc9e06e962ea988525a2ce4a5c9bff92ed08b...7715b994791ebb4b4cd2f9ed70781f9deeb4a02b
Files Reviewed
- packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts
- packages/rspack/src/ModuleFederationPlugin.ts
Files Ignored
These files were ignored due to the filter in the squadron.yaml file.
- .changeset/popular-pillows-draw.md
- packages/data-prefetch/package.json
- packages/data-prefetch/rollup.config.js
- packages/data-prefetch/src/cli/index.ts
- packages/managers/rollup.config.js
- packages/manifest/rollup.config.js
- packages/runtime-tools/package.json
- packages/runtime-tools/rollup.config.js
- packages/runtime/package.json
- packages/runtime/rollup.config.js
- packages/sdk/package.json
- packages/sdk/rollup.config.js
- packages/webpack-bundler-runtime/package.json
- packages/webpack-bundler-runtime/rollup.config.js
- pnpm-lock.yaml
7715b99 to
9c0f334
Compare
# Conflicts: # packages/data-prefetch/rollup.config.js # packages/runtime-tools/rollup.config.js # packages/runtime/rollup.config.js # packages/sdk/rollup.config.js # packages/webpack-bundler-runtime/rollup.config.js # pnpm-lock.yaml
There was a problem hiding this comment.
Incremental Review
Comments posted: 4
Configuration
Squadron Mode: essential
Commits Reviewed
083af4ba6aa3c563267fe0a17f846a86ccde4476...c56143fc51bd3c2cec1a37f6d6ea2a13036a2783
Files Reviewed
- packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts
- packages/rspack/src/ModuleFederationPlugin.ts
Files Ignored
These files were ignored due to the filter in the squadron.yaml file.
- .changeset/popular-pillows-draw.md
- packages/data-prefetch/package.json
- packages/data-prefetch/rollup.config.js
- packages/data-prefetch/src/cli/index.ts
- packages/runtime-tools/package.json
- packages/runtime-tools/rollup.config.js
- packages/runtime/package.json
- packages/runtime/rollup.config.js
- packages/sdk/package.json
- packages/sdk/rollup.config.js
- packages/webpack-bundler-runtime/package.json
- packages/webpack-bundler-runtime/rollup.config.js
There was a problem hiding this comment.
Incremental Review
Comments posted: 4
Configuration
Squadron Mode: essential
Commits Reviewed
0b44ffae88fd261128629a015d63c65f3699da5a...de79650bd174c22b3245b02186461a799a72b772
Files Reviewed
- packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts
- packages/rspack/src/ModuleFederationPlugin.ts
Files Ignored
These files were ignored due to the filter in the squadron.yaml file.
- .changeset/popular-pillows-draw.md
- packages/data-prefetch/package.json
- packages/data-prefetch/rollup.config.js
- packages/data-prefetch/src/cli/index.ts
- packages/runtime-tools/package.json
- packages/runtime-tools/rollup.config.js
- packages/runtime/package.json
- packages/runtime/rollup.config.js
- packages/sdk/package.json
- packages/sdk/rollup.config.js
- packages/webpack-bundler-runtime/package.json
- packages/webpack-bundler-runtime/rollup.config.js
| if (isHoisted) { | ||
| runtimePath = runtimePath.replace('.cjs', '.esm'); | ||
| runtimePath = runtimePath.replace('.cjs.js', '.esm.mjs'); |
There was a problem hiding this comment.
The string replacement for file extension is brittle and could fail if the path doesn't contain '.cjs.js'. A more robust approach would be:
| if (isHoisted) { | |
| runtimePath = runtimePath.replace('.cjs', '.esm'); | |
| runtimePath = runtimePath.replace('.cjs.js', '.esm.mjs'); | |
| runtimePath = runtimePath.replace(/\.cjs\.js$/, '.esm.mjs'); |
This ensures we only replace at the end of the string and handles cases where '.cjs.js' might appear elsewhere in the path.
| if (this.options?.experiments?.federationRuntime === 'hoisted') { | ||
| this.bundlerRuntimePath = this.bundlerRuntimePath.replace( | ||
| '.cjs.js', | ||
| '.esm.js', | ||
| '.esm.mjs', | ||
| ); |
There was a problem hiding this comment.
Similar to the previous suggestion, the extension replacement here should be more precise:
| if (this.options?.experiments?.federationRuntime === 'hoisted') { | |
| this.bundlerRuntimePath = this.bundlerRuntimePath.replace( | |
| '.cjs.js', | |
| '.esm.js', | |
| '.esm.mjs', | |
| ); | |
| this.bundlerRuntimePath = this.bundlerRuntimePath.replace( | |
| /\.cjs\.js$/, | |
| '.esm.mjs', | |
| ); |
| if (/webpack-bundler-runtime/.test(resolveData.contextInfo.issuer)) { | ||
| resolveData.request = RuntimePath.replace('cjs', 'esm'); | ||
| resolveData.request = RuntimePath.replace('cjs.js', 'esm.mjs'); |
There was a problem hiding this comment.
The extension replacement here is also brittle. Consider using a more robust approach:
| if (/webpack-bundler-runtime/.test(resolveData.contextInfo.issuer)) { | |
| resolveData.request = RuntimePath.replace('cjs', 'esm'); | |
| resolveData.request = RuntimePath.replace('cjs.js', 'esm.mjs'); | |
| resolveData.request = RuntimePath.replace(/\.cjs\.js$/, '.esm.mjs'); |
| const runtimeESMPath = require.resolve( | ||
| '@module-federation/runtime/dist/index.esm.js', | ||
| '@module-federation/runtime/dist/index.esm.mjs', | ||
| { paths: [options.implementation] }, | ||
| ); |
There was a problem hiding this comment.
The path resolution for the ESM runtime file correctly uses the .mjs extension, which aligns with the Node.js ESM specification. However, to make the code more resilient to potential path resolution issues, consider adding error handling:
| const runtimeESMPath = require.resolve( | |
| '@module-federation/runtime/dist/index.esm.js', | |
| '@module-federation/runtime/dist/index.esm.mjs', | |
| { paths: [options.implementation] }, | |
| ); | |
| const runtimeESMPath = (() => { | |
| try { | |
| return require.resolve( | |
| '@module-federation/runtime/dist/index.esm.mjs', | |
| { paths: [options.implementation] }, | |
| ); | |
| } catch (error) { | |
| throw new Error(`Failed to resolve ESM runtime path: ${error.message}`); | |
| } | |
| })(); |
This change adds proper error handling and provides a more descriptive error message if the runtime file cannot be resolved, which can help with debugging deployment issues.
Description
make esm files emit with .mjs extention
Related Issue
module-federation/vite#163 (comment)
Types of changes
Checklist