Repository navigation
fix(vite): give HMR deps-bundle shims the named exports of CommonJS and minified ESM - #11543
Merged
Merged
Conversation
…nd minified ESM
Under HMR, node_modules packages in the deps bundle reach the device as thin
shims that re-export each name from the bundled namespace. The names come
from a static scan, which missed two common shapes, so the shim exported
only `default` and the app failed to start with "The requested module
'react' does not provide an export named 'Fragment'":
- CommonJS entries that re-export another file, like react's
`module.exports = require('./cjs/react.development.js')`, have no
`exports.NAME =` assignments to find.
- Minified ESM such as clsx's `dist/clsx.mjs` keeps its `export`s mid-line,
where the ESM check (which looks at line starts) doesn't see them, so it
was scanned as CommonJS.
A `.mjs` file is now always read as ESM. For CommonJS whose names can't be
found statically, the shim exports the names `module.exports` has when the
file is loaded on the dev server, the same lookup per-module serving uses.
esbuild's `import *` copies those properties onto the bundled namespace, so
the shim still reads from the single bundle evaluation.
|
View your CI Pipeline Execution ↗ for commit 17c4a3f
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
commit: |
…names Side-effect-only scripts such as zone.js's fesm2015/zone.js classify as opaque CJS. Loading them on the dev server to enumerate names installs their patches (global Promise, timers) in the Vite process, so skip the runtime lookup when the file never references `exports`. Also filter strict-mode reserved words (package, public, ...) from shim export names, which runtime enumeration can now surface, and update doc comments that still described default-only opaque CJS shims.
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.
PR Checklist
What is the current behavior?
Under HMR,
/ns/m/node_modules/reactand/ns/m/node_modules/clsxare deps-bundle shims that export onlydefault, soimport { Fragment } from 'react'fails with "does not provide an export named 'Fragment'" (React apps have to run with--no-hmr). The shim's names come from a static scan:module.exports = require('./cjs/…')has noexports.X =to find, so it is treated as opaque and given a default-only shim.dist/clsx.mjsfails the ESM check, which only looks at line starts, so it is scanned as CommonJS.What is the new behavior?
.mjsfiles are always treated as ESM.getCjsNamedExportsfinds by loading the file on the dev server, the lookup per-module serving already uses. esbuild'simport *copies those properties onto the bundled namespace, so the package is still evaluated once (which patch-style packages such as moment-timezone rely on).On a real dev server the react shim now has 44 named exports, including
Fragment, and clsx's hasclsx. One existing test asserted a default-only shim formodule.exports = { b: 1 }; it now expectsexport const b.Tests: in
deps-bundle.spec.ts, a minified.mjscase and react- and clsx-shaped service tests.Each new test fails without the change. The
@nativescript/vitesuite (163 files) andtsc -p tsconfig.lib.jsonpass on the branch.