Repository navigation
fix(modules): do not compile import.{defer,source,sync}() as import() - #18661
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63660 |
|
commit: |
3c9af06 to
5f09e5d
Compare
5f09e5d to
1f60218
Compare
test262 resultsAs of 6faec13, this PR does not change any test262 results compared to |
7acba3b to
61f6fa1
Compare
|
The test262 errors are caused by this PR. Currently, the dynamic-import transform converts In this PR, we no longer transform To fix these test262 error, we can let |
61f6fa1 to
c3fa0a5
Compare
|
Done, implemented the |
import.{defer,source,sync}() as import()
08a5fbc to
6db7c6b
Compare
|
Do we already compile |
| 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); |
There was a problem hiding this comment.
If we're adding the exclusion here let's also exclude symbols, as the proposal does.
There was a problem hiding this comment.
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!( |
There was a problem hiding this comment.
Is the ! safe here? What if somebody is using an older version of the import defer plugin?
There was a problem hiding this comment.
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.
Yes, to the extent of which there might be still some open edge cases, |
…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
7de5a18 to
6faec13
Compare
|
Backported to 7.x in #18696. |
This PR fixes a similar issue as #18660 for the dynamic import: The
transform-dynamic-importshould not silently treatimport.phase(),import.defer()andimport.sync()asimport().In this PR, we also implement the lowering of
import.defer()in theproposal-import-defertransform when compiling to the commonJS:The returned promise resolves to a deferred namespace.
xwill berequired and evaluated on the first property access. We also fix a helper bug: TheimportDeferProxyno longer evaluates the module whenthenis read, since resolving a promise with a namespace readsthen.