Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
enisdenjo opened this issue Nov 30, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@enisdenjo
Copy link

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;
@Josh-Cena
Copy link
Contributor

Duplicate of #51554 and fixed by #51562

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 2, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants