Skip to content

Directly exported functions wrapping the dynamic import causes issues when run in "parallel" #51696

@enisdenjo

Description

@enisdenjo

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

⏯ 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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions