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
[8.x] Gain consensus on path forward for "sourceType" for parsing modules vs scripts #6242
Comments
One possible difficulty is what to do with In TypeScript with |
Another way to look at this is that right now, without modifying the code of the file itself with things like Being confident of when something is a module is important because things need to know this stuff to know what the properties of the file are. Can a transform inject a Even in our own unit tests, which are This is all a huge pain for users that we've essentially glossed over because most people don't run into it, but it's only going to happen more and more as stuff in We can also discuss defaulting to "unambiguous" parsing, but I think it's important that we default to standards-compliant behavior, even if we provide more flexibility as an opt-in. |
The difficulty with supporting that for This problem should also increase as node modules start shipping A possible way to handle TypeScript files, though, is to just always treat them as modules. If TypeScript's module option is set to |
Just dropping in to say that in no way should "unambiguous" be the default - it's nonstandard, and the only defaults should ever be "the standard". Adding it as an option is great! But it should only be explicitly opt-in. I think that the proper thing is to do what node is planning, which is ".mjs is Module, .js is Script" by default, and if users want to override this by extension they should have the ability to do that. That means that |
Are there clear examples of this right now? Also things are starting to look like you can declare the media type for source text and Node is moving it's interpretation of |
How would a MIME type be relevant to non-browsers tho? Babel transpiles for server apps too. |
@ljharb various useful APIs, like |
I didn't realize those worked in node, but I'm still not understanding how a mime type is relevant to how Babel parses things :-/ |
@ljharb because if they have a "sourceMode" having it be MIME based would be great. https://github.com/bmeck/I-D/blob/master/javascript-mjs/draft.md#textjavascript updated internet draft also added a |
Right, but what besides the extension informs the mime type? |
@ljharb for file backed source text extension mostly informs the type. However |
Right, but babel doesn't target browsers; that's a bundler's job. |
@ljharb it should still use the MIME under the hood when there are not files instead of a random enum. |
Without an extension, where would it get the MIME from? (Sorry if I'm being dense here; I just don't see how mime types ever matter before the point of sending a response to a browser) |
@ljharb a variety of potential places, mostly from generated source that is not on disk. |
This seems like a needless restriction for Babel to enforce. The environment that I choose to provide to my Babel-compiled code (including |
But that means we're not actually enforcing the proper ES6 semantics. If I write
in an ES6 module, that's reading I believe Webpack already mixing ES6 and CommonJS exports because it uses a different wrapper for modules that it detects as ES6, so the We can always have a flag to opt out of it, but we're doing our best to move toward our default being spec compliance. |
cc @jdalton per #6983 (comment) |
Alright, I've posted #7501 so let's continue this conversation over there. |
Per #7501 (comment), we have decided to postpone this discussion until Babel 8.x. |
I wrote get-package-type to determine how node.js will want to load a given |
Rather than have this on Slack, I thought it would be useful to have this as an issue so we can refer to it in the future. So, the question:
What this means in practice is:
.mjs
or putsourceType: "module"
in their.babelrc
or whatever Babel config they have.use strict
to their CommonJS files will have to addtransform-strict-mode
to their Babel configs.Because Babel has parsed as
module
by default for a long time, it has logic that essentially says "if there are no imports/exports in thismodule
, it's actually ascript
". This adds a lot of complexity to Babel's module processing because this logic is not standard in any way, and it means that there is no central way for code to ask "is this a module".The reason "is this a module" is such an important question is that because of this, currently it is valid to have both
import
statements andrequire()
usageexport
statements andmodule.exports
/exports.foo
usageWebpack now disallows the
#2
case, while allowing#1
. Ideally Babel should also adopt that behavior, but where it's not too bad for Webpack, Babel's iterative transformational nature makes it a pain to constantly not know what type ofsourceType
the file should be considered as.My proposal is the following:
sourceType
to default toscript
for.js
, and force tomodule
for.mjs
Disallow use of CommonJS'smodule.exports
/exports
whensourceType: module
(potentially with a flag to enable this again to ease transition)Update the parts of Babel that injectimport
statements to be smart enough to inject bothimport
andrequire
based on the.sourceType
To ease the transition, I'd propose a few more possibilities.
Land sourceType: "unambiguous" parsing so that users with codebases that aggressively mix ES6 modules and CommonJS modules don't pull their hair out. This should essentially make parsing act like it did before, minus theSupport for this has landed in Allow sourceType:unambiguous as a way to tell Babylon to guess the type. #6789.use strict
injection.babel-cli
output files with a.mjs
extension if they havesourceType: 'module'
Support Configuration based on file path so that if people don't want to usesourceType: 'unambiguous'
, they can at least explicitly opt specific files into asourceType
What do people think?
The text was updated successfully, but these errors were encountered: