-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Differential bundling for <script type="module"> #2792
Comments
There are two open issues about the
When browsers adopt it, we can talk about that again. |
That's great. I did search for old issues but those don't have great names. Are there any details about how this will work in Parcel 2? Has it been implemented already? I agree that there's no point in starting to implement |
For context, this is the syntax proposal: whatwg/html#4432 |
A few things to keep in mind:
How transpilation works - it gets your code written in higher language, and transpiles it to lower language, acceptable by a browser.
According to this - to create es5 target you might use a As long as this operation is needed only to create a production build (you are not using browser requiring a lower language during dev), and no cache is present - that could be a quite efficient solution. Plus - you might use not babel, but If you will place result bundles into separate directories, not separate filenames - And you already might do it - https://github.com/thekashey/devolution |
@theKashey That's awesome. I'd not seen that before. I think that pattern is a lot neater than the Webpack chicanery I'm currently using. I think I'll try the switch to Parcel + Devolution tomorrow. |
@theKashey Could you explain how to do this?
To clarify, to use |
yarn devolution ./dist ./dist index.js true
var script = document.createElement('script');
var prefix = (!('noModule' in check)) ? "/ie11" : "/esm";
script.src = 'dist' + prefix + "/index.js"; // dist/target/script
document.head.appendChild(script); |
What about using Also, why does |
That is yet unsolved. You may use assets plugin to generate scripts independently, and then construct your
By default, a script will prefix all used
This is changing file names, and breaks code splitting, as long as you will have to rename all chunks, and then rebuild chunks map, which is impossible without deep integration with a bundler. |
🙋 feature request
Using
<script type="module">
alongside<script nomodule>
should allow different bundles to be served to modern browsers. Bundles targeting modern browers are usually significantly smaller than ones that need to target legacy browsers.It appears that the Chrome team also has plans to support a
syntax
property to allow<picture>
-style serving of different syntaxes. The benefits of this are unlikely to be as big as the basic behaviour described above, but would be worth supporting if browser support lands.Are there any plans now to support either of these behaviours?
🤔 Expected Behavior
If an HTML file contains
<script type="module">
alongside<script nomodule>
, then generate an.mjs
bundle with modern syntax as well as a normal bundle. If a script tag includes a syntax attribute then also transpile bundles targeting those syntaxes.😯 Current Behavior
If an HTML file has both
<script type=module>
and<script nomodule>
with the same entrypoint, then the generated HTML only has one script tag and one bundle.💁 Possible Solution
The example tweeted by @developit uses
package.json
to specify targets, but Parcel could read it directly from the script tags. Themjs
bundle would use something like babel preset-envtarget: {esmodules: true}
.Support for the
syntax
attribute could either read different configs from a babelrc, or could infer the required targets from the content of the syntax attribute.Filenames for the entry point and code-split bundles could either use subdirectories or filenames. e.g.
2017/main.js
or2017.main.js
.🔦 Context
I'm currently trying to approximate the basic version of this in webpack, using two webpack configs, each with a different output path and babel env name. This is messy, as it also bundles two copies of all other files, and can't generate HTML.
💻 Examples
The text was updated successfully, but these errors were encountered: