-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
Bug Report
When wrapping the dynamic import, exporting the function directly and building for CommonJS, the built artifact will contain a var _a outside of the function's scope which will cause issues when importing in "parallel", for example with Promise.all.
🔎 Search Terms
dynamic import, import, hoist, outside scope, var _a
🕗 Version & Regression Information
- This is a regression starting from version
4.9.0-dev.20221019introduced with fix: avoid downleveled dynamic import closing over specifier expression #49663
⏯ Playground and reproduction link
💻 Code
// importFn.ts
export async function importFn(path: string) {
return import(path);
}The transpiled code will have problems with parallelism, running the following:
// assuming neither module exists
Promise.all(["idontexist-1", "idontexist-2"].map(importFn));will raise an error: "Cannot find module 'idontexist-2'" while importing idontexist-1.
🙁 Actual behavior
// importFn.js
"use strict";
var _a; // 👈 outside func scope
Object.defineProperty(exports, "__esModule", { value: true });
exports.importFn = void 0;
async function importFn(path) {
return _a = path, Promise.resolve().then(() => require(_a));
}
exports.importFn = importFn;🙂 Expected behavior
// importFn.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.importFn = void 0;
async function importFn(path) {
var _a; // 👈 inside func scope
return _a = path, Promise.resolve().then(() => require(_a));
}
exports.importFn = importFn;charlypoly, dotansimha, saihaj, Urigo and dimaMachinadotansimha, saihaj, ardatan, Urigo and dimaMachina
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created