Skip to content
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

Change web and bundler output to mjs #1941

Closed
daxpedda opened this issue Jan 4, 2020 · 4 comments
Closed

Change web and bundler output to mjs #1941

daxpedda opened this issue Jan 4, 2020 · 4 comments
Labels

Comments

@daxpedda
Copy link
Collaborator

daxpedda commented Jan 4, 2020

Summary

Considering that we are producing ES modules on those output modes, the correct recommended file extension is mjs, not js.
Are we interested in implementing/fixing this? I would like to work on this if possible.

Additional Details

This would probably break a whole lot of things out there, and the benefit is "just" following standards recommendations properly, or at least I'm not aware of any other advantage.

See #1332 (comment).

@Pauan
Copy link
Contributor

Pauan commented Jan 4, 2020

To be clear, mjs is not "more correct". It is only used by Node, and even on Node you don't need to use mjs (since you can use "type": "module").

All other ecosystems (including the browser) traditionally use js, which is standardized under the MIME type application/js.

So, this isn't a choice between the "standard" and "wrong" way, this is a choice between two different standardized (and equally correct) ways.

One of those ways has been around for decades, is well established, and works everywhere (including Node). The other way is extremely new, and Node only.

Personally, I think we should continue to output js, since it works everywhere. Node users can just put "type": "module" in their package.json (which is recommended no matter what extension is used).

@daxpedda
Copy link
Collaborator Author

daxpedda commented Jan 4, 2020

You are absolutely correct, thank you!

I spent some time researching this:

Note: In native JavaScript modules, the extension .mjs is important because browsers import the files with a JavaScript compatible MIME-type — text/javascript — using this avoids strict MIME type checking errors like "The server responded with a non-JavaScript MIME type". In addition, the .mjs extension is good for clarity (i.e. this file is a module, and not just regular JavaScript) and for interoperability with other tools. ...

Still, we recommend using the .mjs extension for modules, ...

  • The WHATWG HTML living standard uses it for it's modules everywhere:
<script type="module" src="app.mjs">

... a new file extension of .mjs is to be added to the list of file extensions with the restriction that it must correspond to the Module grammar of [ECMA-262].

So there is actually no standard in place at this point in time that says that mjs must or should be used, the closest is a draft, that only says that file with a mjs file ending have to be modules.

But there are recommendations by both mozilla and google.

@Pauan
Copy link
Contributor

Pauan commented Jan 4, 2020

For browsers, the extension is irrelevant. You can use any extension (or even no extension at all). The browser does not have special handling for mjs.

What matters is using type="module" on the <script> tag and the server using the correct MIME type. If you create a <script src="foo.mjs">, then it will not be interpreted as ES6, even though it uses mjs.

This is important, because most servers are configured to use the js extension in their mime.types, and it will take time for them to upgrade to mjs (if they ever upgrade).

The Google article is quite old (June 2018), and things have changed since then. And the Mozilla article is a wiki that is created by volunteers, it is not necessarily official.

The whole js vs mjs debate has raged for many years, with many changes happening over time. I am making my recommendation based on the official Node recommendations, which are the definitive authority for mjs, and also my practical knowledge of the JS ecosystem.

Right now, most tools do not support mjs, and mjs was only very recently (2019-11-21) released by Node, so we don't know what will happen within the Node community.

It's entirely possible that mjs will rarely be used, with most people preferring to use "type": "module" (which is recommended regardless of what extension you use). I think it's premature to make a change right now, though we should reevaluate this again in a year or so.

@daxpedda
Copy link
Collaborator Author

daxpedda commented Jan 4, 2020

I think it's premature to make a change right now, though we should reevaluate this again in a year or so.

Sounds reasonable to me.

@daxpedda daxpedda closed this as completed Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants