forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: add option to interpret __esModule like Babel
Adds an option `--cjs-import-interop`. When enabled, a CJS module will be translated to ESM slightly differently with respect to default exports. - When the module defines `module.exports.__esModule` as a truthy value, the value of `module.exports.default` is used as the default export. - Otherwise, `module.exports` is used as the default export. (existing behavior) It allows better interoperation between full ES modules and CJS modules transformed from ES modules by Babel or tsc. Consider the following example: ```javascript // Transformed from: // export default "Hello"; Object.defineProperty(module.exports, "__esModule", { value: true }); module.exports.default = "Hello"; ``` When imported from the following module: ```javascript import greeting from "./hello.cjs"; console.log(greeting); ``` With `--cjs-import-interop`, it will print "Hello". Fixes: nodejs#40891
- Loading branch information
Showing
7 changed files
with
82 additions
and
1 deletion.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { strictEqual, deepEqual } from 'assert'; | ||
|
||
import m, { π } from './exports-cases.js'; | ||
import * as ns from './exports-cases.js'; | ||
|
||
deepEqual(Object.keys(ns), ['?invalid', 'default', 'invalid identifier', 'isObject', 'package', 'z', 'π', '\u{d83c}\u{df10}']); | ||
strictEqual(π, 'yes'); | ||
strictEqual(typeof m.isObject, 'undefined'); | ||
strictEqual(m.π, 'yes'); | ||
strictEqual(m.z, 'yes'); | ||
strictEqual(m.package, 10); | ||
strictEqual(m['invalid identifier'], 'yes'); | ||
strictEqual(m['?invalid'], 'yes'); | ||
|
||
import m2, { __esModule as __esModule2, name as name2 } from './exports-cases2.js'; | ||
import * as ns2 from './exports-cases2.js'; | ||
|
||
strictEqual(__esModule2, true); | ||
strictEqual(name2, 'name'); | ||
strictEqual(typeof ns2, 'object'); | ||
strictEqual(m2, 'the default'); | ||
strictEqual(ns2.__esModule, true); | ||
strictEqual(ns2.name, 'name'); | ||
deepEqual(Object.keys(ns2), ['__esModule', 'case2', 'default', 'name', 'pi']); | ||
|
||
import m3, { __esModule as __esModule3, name as name3 } from './exports-cases3.js'; | ||
import * as ns3 from './exports-cases3.js'; | ||
|
||
strictEqual(__esModule3, true); | ||
strictEqual(name3, 'name'); | ||
deepEqual(Object.keys(ns3), ['__esModule', 'case2', 'default', 'name', 'pi']); | ||
strictEqual(m3, 'the default'); | ||
strictEqual(ns3.__esModule, true); | ||
strictEqual(ns3.name, 'name'); | ||
strictEqual(ns3.case2, 'case2'); | ||
|
||
console.log('ok'); |