-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add useAutoDirectory option to transform-runtime plugin #8885
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/9271/ |
@@ -56,6 +57,14 @@ export default declare((api, options, dirname) => { | |||
"The 'useESModules' option must be undefined, or a boolean, or 'auto'.", | |||
); | |||
} | |||
if (useESModules && useAutoDirectory) { | |||
throw new Error("The 'useAutoDirectory' option must be a boolean."); |
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.
Looks like this message got swapped with the other error.
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.
🤦♂️ fixed
filename, | ||
JSON.stringify( | ||
{ | ||
name: `${runtimeName}/helpers/auto/${helperName}`, |
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.
Are the name
and private
fields needed? I'd have expected only main
and module
.
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.
They are probably not, Im just accustomed to using them. IMHO private
gives an extra pinch of confidence that this won't be tried to get published (especially in lerna setup in case of some weird bug on their side).
name
gives some more meaning to those files (kinda same with private, semantically both are ok). So I find adding those fields a nice touch, but as mentioned - they are not probably strictly required, so I can check it out and remove them if you feel they are not needed here.
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.
My feeling is that if name
isn't there, then an accidental publish isn't a concern either way. If we really want name
, then I agree having the private: true
is a good choice though. I'd probably rather leave them out and add them if we get reports of it being an issue.
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.
Removed them.
2affdf7
to
53b6d49
Compare
@loganfsmyth thanks for the comments! WDYT about the issue described in the "warning" section of the PR? |
4c355aa
to
2d64ad6
Compare
Hmm, I'd actually considered the current behavior to be more compatible since logically the default import of a CommonJS file is the Does Rollup have different expectations in this case? |
The problem is that webpack became stricter (I think it has happened in v4) about module shapes and cjs/esm interop. I've described the issue here - webpack/webpack#7973 . |
Ah right. So I guess if we wanted to be as compliant and useable as possible, we'd want our CommonJS exports to change from
to something more like
which I think would allow it to work in all contexts alongside |
Depends on if we consider manual (not generated by If we allow it then people might use non- If we don't allow it - then yeah, it should work - because the only usage of those modules from CJS code would be inserted by babel itself and thus they should be imported with interop helper, right? |
I don't think I'd consider that valid, but I also wouldn't want to go out of my way to break it.
That would only break if they are using the |
If you are referring the other auto behaviour (based on When looking into the source of @babel/plugin-transform-runtime & @babel/helper-module-imports right now I see I was wrong - imports to those helper modules are inserted with Let's look at possible scenarios - I'm narrowing it down only to libraries providing only CJS entry, because it's the only situation which is problematic.
var _classCallCheck = require("@babel/runtime/helpers/classCallCheck");
let Foo = function Foo() {
"use strict";
_classCallCheck(this, Foo);
}; Should be OK as
var _classCallCheck = require("@babel/runtime/helpers/auto/classCallCheck");
let Foo = function Foo() {
"use strict";
_classCallCheck(this, Foo);
}; I think this won't work because webpack will think that imported module has To sum it up I think the only way to support it at the moment is to introduce a separate "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
module.exports.default = require('../createClass') and point to that from |
@loganfsmyth any more thoughts on this? do you think this is too convoluted at this point in time to proceed with it? |
this is being superseded by #12295 |
Why?
When building libraries it's annoying to setup different options for
@babel/plugin-transform-runtime
for ESM and CJS builds.With this the configuration can be unified with the newly introduced option and i.e. when bundling with rollup we could shorten this:
to this
This technique of creating extra package.json files is not well-known but it's part of the node resolution algorithm - https://nodejs.org/api/modules.html#modules_all_together & resolution algorithms of webpack, rollup and other tools support this. This has an advantage of allowing tools to pick up appropriate files thanks to being able to specify both main & module entries for a single request path.
Unfortunately helpers written in CJS format have different shape from the helpers written in ESM. With ESM things are exported as default, with CJS they are assigned directly to
module.exports
.As we know those things are not really compatible and I have 2 ideas how we could fix this (the issue to be fixed is that package.json in auto directory should point to modules with the same shape, regardless of the format):
module.exports.default = require('../createClass')
and point to those proxy files from auto package.json files.default
property that would self-reference to them, I'm not sure if this is completely safe though, could it cause problems with live bindings such as inextends
helper?