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
[commonjs] Add defaultIsModuleExports
option to match Node.js behavior
#838
Merged
guybedford
merged 4 commits into
rollup:master
from
nicolo-ribaudo:commonjs-node-interop
Apr 3, 2021
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions
5
packages/commonjs/test/fixtures/form/nodeDefaultImport-__esModule/_config.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,5 @@ | ||
module.exports = { | ||
options: { | ||
nodeDefaultImport: true | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/commonjs/test/fixtures/form/nodeDefaultImport-__esModule/input.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,3 @@ | ||
exports.__esModule = true; | ||
exports.default = 2; | ||
exports.named = 3; |
14 changes: 14 additions & 0 deletions
14
packages/commonjs/test/fixtures/form/nodeDefaultImport-__esModule/output.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 @@ | ||
var __esModule = true; | ||
var _default = 2; | ||
var named = 3; | ||
|
||
var input = { | ||
__esModule: __esModule, | ||
default: _default, | ||
named: named | ||
}; | ||
|
||
export default input; | ||
export { input as __moduleExports }; | ||
export { __esModule }; | ||
export { named }; |
5 changes: 5 additions & 0 deletions
5
packages/commonjs/test/fixtures/form/nodeDefaultImport-no-__esModule/_config.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,5 @@ | ||
module.exports = { | ||
options: { | ||
nodeDefaultImport: true | ||
} | ||
}; |
2 changes: 2 additions & 0 deletions
2
packages/commonjs/test/fixtures/form/nodeDefaultImport-no-__esModule/input.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 @@ | ||
exports.default = 2; | ||
exports.named = 3; |
11 changes: 11 additions & 0 deletions
11
packages/commonjs/test/fixtures/form/nodeDefaultImport-no-__esModule/output.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,11 @@ | ||
var _default = 2; | ||
var named = 3; | ||
|
||
var input = { | ||
default: _default, | ||
named: named | ||
}; | ||
|
||
export default input; | ||
export { input as __moduleExports }; | ||
export { named }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for moving forward here!
I was thinking about the name of the option and I am not happy about this choice as it references an environment (Node) that may change while IMO it should rather reflect functionality. So what this is about is as I understand it is to skip the default import interop in favour of assuming the default is
module.exports
. There are several ways to handle this (names are just suggestions):moduleExportsIsDefault: true | false | "auto"
where "auto" is the current behaviour (could also be "detect")cjsDefaultExport: "module.exports" | "default" | "auto"
where "auto" is the current behaviour (orcjsDefaultImport
depending which way you view it)Admittedly that the version where the default export is always
exports.default
may not be as useful, but what do I know. Could be a micro-optimisation for some. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we should change the default, but I keep twisting the idea of a config plugin in my head that would just configure commonjs, node-resolve and rollup for Node compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a slight preference for the
moduleExportsIsDefault: true | false | "auto"
(ordefaultIsModuleExports
? since we are giving a value to the "default" binding) option, since typing"module.exports"
inside a string (thus often without autocompletion) can be a bit harder.There are already precedents for
boolean | string
options, such asrequireReturnsDefault
.We actually have an option in Babel exactly for that (https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#nointerop), even if we don't have data about how many people use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you do not have the
false
case, i.e. always assuming the default export ismodule.exports.default
, right? That's the one where I am not sure people really would use it (and if we should even add it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noInterop: true
means "always get.default
, without first checking if it has__esModule
: https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop/import-default-onlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. So reading babel/babel#12838 I understand now that you are also going for a three-state flag 👍
I would be fine with that as well.