-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dynamic import on plain CJS files
Fixes #778 Previously, `import()` transpiled to a promise-wrapped `require`, which worked fine for ESM-to-CJS-transpiled modules, but when importing plain CJS modules, the behavior was inconsistent with other implementations. In that case, we needed to call `_interopRequireWildcard` to nest the module under a `default` key. I tested to confirm that this updated behavior is consistent with Node ESM-to-CJS dynamic import, as well as Babel, TypeScript, and swc, so this change will be considered a bug fix rather than a breaking change, even though it is possible that existing use cases may have been relying on the old behavior.
- Loading branch information
1 parent
5636395
commit c59729c
Showing
7 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
integration-test/test-cases/register-cases/allows-dynamic-import/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
async function main() { | ||
const plainCJSFile = await import("./plain-cjs-file"); | ||
const transpiledESMFile = await import("./transpiled-esm-file"); | ||
if (plainCJSFile.default !== 15) { | ||
throw new Error(); | ||
} | ||
if (transpiledESMFile.a !== 1) { | ||
throw new Error(); | ||
} | ||
if (transpiledESMFile.default !== 3) { | ||
throw new Error(); | ||
} | ||
} | ||
main(); |
1 change: 1 addition & 0 deletions
1
integration-test/test-cases/register-cases/allows-dynamic-import/plain-cjs-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 15; |
2 changes: 2 additions & 0 deletions
2
integration-test/test-cases/register-cases/allows-dynamic-import/transpiled-esm-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const a = 1; | ||
export default 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters