Skip to content

fix(modules): do not compile import.{defer,source,sync}() as import() - #18661

Merged
JLHwung merged 6 commits into
mainfrom
jlhwung/fix-modules-dynamic-import-phase
Nov 23, 2026
Merged

JLHwung merged 6 commits into
mainfrom
jlhwung/fix-modules-dynamic-import-phase

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? The dynamic import version of #18660
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

This PR fixes a similar issue as #18660 for the dynamic import: The transform-dynamic-import should not silently treat import.phase(), import.defer() and import.sync() as import().

In this PR, we also implement the lowering of import.defer() in the proposal-import-defer transform when compiling to the commonJS:

// input
import.defer("x");
// output
Promise.resolve().then(() =>importDeferProxy(() =>interopRequireWildcard(require("x"))));

The returned promise resolves to a deferred namespace. x will be required and evaluated on the first property access. We also fix a helper bug: The importDeferProxy no longer evaluates the module when then is read, since resolving a promise with a namespace reads then.

@JLHwung
JLHwung added this pull request to stack #18587 November 19, 2026 15:00
@JLHwung JLHwung added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Nov 19, 2026
@babel-bot

babel-bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63660

@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 6faec13

@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch from 3c9af06 to 5f09e5d Compare November 19, 2026 15:44
@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch from 5f09e5d to 1f60218 Compare November 19, 2026 23:16
@babel-bot

babel-bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

test262 results

As of 6faec13, this PR does not change any test262 results compared to main (tc39/test262@2e58f87).

@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch 2 times, most recently from 7acba3b to 61f6fa1 Compare November 20, 2026 00:08
@JLHwung

JLHwung commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

The test262 errors are caused by this PR.

Currently, the dynamic-import transform converts import.defer() as if it were import() and the result happens to run on Node.js 0.10, the module was evaluated eagerly, which is wrong, but these test262 tests only check that the import.defer() call can be parsed, and a specifier whose toString throws gives a rejected promise. They do not test whether the module was evaluated eagerly, so on main they just pass by incident.

In this PR, we no longer transform import.defer() in dynamic import, since proposal-import-defer does not lower import.defer(), it was then printed to the output which Node.js 0.10 apparently does not support.

To fix these test262 error, we can let proposal-import-defer lower the import.defer() as well. I will try to do it on this PR so the end user experience will be going from getting invalid output to getting somehow valid output, rather than getting a big compilation error that Babel does not support this so far.

Base automatically changed from jlhwung/fix-modules-static-import-phase to main November 20, 2026 01:22
@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch from 61f6fa1 to c3fa0a5 Compare November 20, 2026 01:22
@JLHwung

JLHwung commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Done, implemented the import.defer() lowering in 5bc5bdf, ptal.

@JLHwung JLHwung changed the title fix(modules): throw on dynamic import phases/sync that are not handled fix(modules): do not compile import.{defer,source,sync}() as import() Nov 20, 2026
@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch 2 times, most recently from 08a5fbc to 6db7c6b Compare November 20, 2026 18:52
@nicolo-ribaudo

Copy link
Copy Markdown
Member

Do we already compile import.source() properly?

return function (target: T, p?: string | symbol, receiver?: any) {
// "then" does not trigger evaluation of a deferred namespace, so that it
// is not evaluated (or used as a thenable) when a promise resolves to it.
if (p === "then") return run(target, p, receiver);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're adding the exclusion here let's also exclude symbols, as the proposal does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 7de5a18. I settle down to typeof != string so that we don't have to pull the typeof helper in runtime-corejs3 package.

// one defined by @babel/plugin-proposal-import-defer) handles them.
// In that case they are compiled regardless of whether regular
// dynamic imports are.
wrapNamespace = makeInvokers(this.file).getDynamicImportWrapper!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the ! safe here? What if somebody is using an older version of the import defer plugin?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is safe, makeInvokers lives in src/hooks in the same library, which always return an object, so getDynamicImportWrapper is always defined. For an older import-defer plugin, wrapNamesapce, the evaluation result of getDynamicImportWrapper, might be nullish, which is guarded in the next check.

I will see if I can refine the typings here such that the ! is not required.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fafcf1d.

@JLHwung

JLHwung commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Do we already compile import.source() properly?

Yes, to the extent of which there might be still some open edge cases, import-wasm-source already hooked into the ImportExpression visitor. So it is safe to throw import.source in the dynamic-import transform. But import.defer() lowering is not implemented until this PR.

…s `import()`

The CommonJS, AMD and SystemJS transforms ignored the phase of a
dynamic import, so `import.source("mod")` and `import.defer("mod")`
were compiled to a regular dynamic import that resolves to the
evaluated module namespace.

They are now left untouched. A plugin that handles the phase, such as
the import wasm source one, can still compile them whatever the plugin
order is.

Assisted-by: Claude Fable 5.2
`import.sync()` has no phase, so the CommonJS, AMD and SystemJS
transforms still compiled it to a regular dynamic import, which
returns a promise instead of the module namespace. It is now left
untouched like `import.source()` and `import.defer()`.

Assisted-by: Claude Opus 5.5
Since the previous commits, `import.defer()` is no longer compiled as
`import()` by the CommonJS transform, so it was left untouched even
when `@babel/plugin-proposal-import-defer` was enabled.

The CommonJS transform now asks its hooks how to wrap the namespace of a
dynamic import with a phase, and the import-defer plugin wraps it with
`importDeferProxy`, so that `import.defer(x)` compiles to
`Promise.resolve().then(() => importDeferProxy(() => require(x)))`
(with the usual interop). The `importInterop` option is respected.

Since a promise resolved with the namespace reads its `then` property,
`importDeferProxy` no longer evaluates the module when `then` is
accessed. For the same reason, `ns.then` and computed accesses on a
static `import defer * as ns` now use the proxy.

Assisted-by: Claude Opus 5.5
The import defer proposal treats every Symbol key, like "then", as not
triggering the evaluation of a deferred namespace: `[[Get]]`,
`[[GetOwnProperty]]`, `[[HasProperty]]`, `[[DefineOwnProperty]]` and
`[[Delete]]` handle them as ordinary properties of the namespace object.

`importDeferProxy` now forwards symbol keys to the proxy target without
evaluating the module.

Assisted-by: Claude Opus 5.5
`makeInvokers` always defines every invoker, so its return type no
longer marks them as optional. Hooks registered by other plugins (or by
older versions of them) may still lack a method, which the invokers
already handle by skipping that hook.

Assisted-by: Claude Opus 5.5
`typeof p === "symbol"` is compiled by
@babel/plugin-transform-typeof-symbol to a `_typeof` call, which adds the
`typeof` helper to the output (and an import of it in @babel/runtime)
even though engines with `Proxy` support symbols natively.

Property keys are either strings or symbols, so check that the key is
not a string instead, which that plugin leaves as is. The key is
undefined in the `ownKeys` trap, which must still evaluate the module.

Assisted-by: Claude Opus 5.5
@JLHwung
JLHwung force-pushed the jlhwung/fix-modules-dynamic-import-phase branch from 7de5a18 to 6faec13 Compare November 23, 2026 14:12
@JLHwung
JLHwung added this pull request to the merge queue Nov 23, 2026
Merged via the queue into main with commit 0e197e9 Nov 23, 2026
40 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-modules-dynamic-import-phase branch November 23, 2026 14:54
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18696.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants