fix: resolve named CommonJS imports in dev-server bundling#89
Open
Aukevanoost wants to merge 1 commit into
Open
fix: resolve named CommonJS imports in dev-server bundling#89Aukevanoost wants to merge 1 commit into
Aukevanoost wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #83
This pull request introduces comprehensive improvements to the handling of CommonJS (CJS) and ECMAScript Module (ESM) interop in the
node-modules-bundlerfor esbuild. It adds robust detection and synthetic export generation for named exports from CJS modules, ensuring that named imports work correctly across module boundaries, especially when using module federation or import maps. The update also includes extensive unit tests for the new logic.CommonJS/ESM Interop Enhancements
createCjsNamedExportsPluginesbuild plugin that, for CommonJS entry points, generates a synthetic ESM module re-exporting both the default and all discovered named exports at build time, fixing issues with named imports from CJS modules (e.g.,import { isDayjs } from 'dayjs') [1]], [2]]).planCjsWrapto decide if a module needs wrapping and which keys to re-export ([src/tools/esbuild/node-modules-bundler.tsR31-R289]).buildSyntheticCjsEntryto generate the synthetic ESM entry ([src/tools/esbuild/node-modules-bundler.tsR31-R289]).isCommonjsCandidateto determine if a file should be treated as CommonJS ([src/tools/esbuild/node-modules-bundler.tsR31-R289]).isEsmInteropErrorto distinguish benign ESM interop errors from real runtime failures ([src/tools/esbuild/node-modules-bundler.tsR31-R289]).isIdentifierNameto validate exportable identifier names ([src/tools/esbuild/node-modules-bundler.tsR31-R289]).Build Pipeline and Transform Logic
canSkipTransformfunction, which only skips.mjsfiles that don't need linking and when advanced optimizations are disabled [1]], [2]]).Testing
node-modules-bundler.spec.ts, covering edge cases and ensuring correctness for CJS/ESM detection, export generation, and error handling [1]], [2]]).These changes significantly improve the reliability and correctness of named import handling from CJS modules in esbuild-based builds, especially in federated or import map scenarios.