← 输入框|github.com|刷新
Skip to content

fix(vite): give HMR deps-bundle shims the named exports of CommonJS and minified ESM - #11543

Merged
NathanWalker merged 4 commits into
mainfrom
fix/vite-deps-bundle-named-exports
Oct 21, 2026
Merged

NathanWalker merged 4 commits into
mainfrom
fix/vite-deps-bundle-named-exports

Conversation

@triniwiz

Copy link
Copy Markdown
Member

PR Checklist

What is the current behavior?

Under HMR, /ns/m/node_modules/react and /ns/m/node_modules/clsx are deps-bundle shims that export only default, so import { 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:

  • react's module.exports = require('./cjs/…') has no exports.X = to find, so it is treated as opaque and given a default-only shim.
  • clsx's minified one-line dist/clsx.mjs fails the ESM check, which only looks at line starts, so it is scanned as CommonJS.

What is the new behavior?

  • .mjs files are always treated as ESM.
  • An opaque CommonJS shim exports the names getCjsNamedExports finds by loading the file on the dev server, the lookup per-module serving already uses. esbuild's import * 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 has clsx. One existing test asserted a default-only shim for module.exports = { b: 1 }; it now expects export const b.

Tests: in deps-bundle.spec.ts, a minified .mjs case and react- and clsx-shaped service tests.

Each new test fails without the change. The @nativescript/vite suite (163 files) and tsc -p tsconfig.lib.json pass on the branch.

…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.
@nx-cloud

nx-cloud Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 17c4a3f

Command Status Duration Result
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-10-21 18:35:48 UTC

@pkg-pr-new

pkg-pr-new Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11543
npm i https://pkg.pr.new/@nativescript/vite@11543
npm i https://pkg.pr.new/@nativescript/webpack@11543

commit: 17c4a3f

triniwiz and others added 3 commits October 15, 2026 16:25
…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.
@NathanWalker
NathanWalker merged commit 1c22593 into main Oct 21, 2026
10 checks passed
@NathanWalker
NathanWalker deleted the fix/vite-deps-bundle-named-exports branch October 21, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants