I've been experimenting with new native ECMAScript module support that has recently been added to browsers. It's pleasant to finally be able import scripts directly and cleanly from JavaScript.
/example.html
I've been experimenting with new native ECMAScript module support that has recently been added to browsers. It's pleasant to finally be able import scripts directly and cleanly from JavaScript.
/example.html
This is possible with service workers.
Since a service worker should be installed before it will be able to process a page, this requires to have a separate page to initialize a worker to avoid chicken/egg problem - or a page can reloaded when a worker is ready.
Here's an example that is supposed to be workable in browsers that support native ES modules and
async..await
(namely Chrome):index.html
sw.js
Script bodies are being cached (native
Cache
can be used as well) and returned for respective module requests.Concerns
The approach is inferior to the application built and chunked with bundling tool like Webpack or Rollup in terms of performance, flexibility, solidity and browser support - especially if blocking concurrent requests are the primary concern.
Inline scripts increase bandwidth usage, this is naturally avoided when scripts are loaded once and cached by the browser.
Inline scripts aren't modular and contradict the concept of ES modules (unless they are generated from real modules by server-side template).
Service worker initialization should be performed on a separate page to avoid unnecessary requests.
The solution is limited to a single page and doesn't take
<base>
into account.Regular expression is used for demonstration purposes only. When used like in the example above it enables the execution of arbitrary JS code that is available on page. A proven library like
parse5
should be used instead (it will result in performance overhead, and still, there may be security concerns). Never use regular expressions to parse DOM.Hacking Together Our Own
import from '#id'
Exports/imports between inline scripts aren't natively supported, but it was a fun exercise to hack together an implementation for my documents. Code-golfed down to a small block, I use it like this:
Instead of
<script type="module">
, we need to define our script elements using a custom type like<script type="inline-module">
. This prevents the browser from trying to execute their contents itself, leaving them for us to handle. The script (full version below) finds allinline-module
script elements in the document, and transforms them into regular script module elements with the behaviour we want.Inline scripts can't be directly imported from each other, so we need to give the scripts importable URLs. We generate a
blob:
URL for each of them, containing their code, and set thesrc
attribute to run from that URL instead of running inline. Theblob:
URLs acts like normal URLs from the server, so they can be imported from other modules. Each time we see a subsequentinline-module
trying to import from'#example'
, whereexample
is the ID of ainline-module
we've transformed, we modify that import to import from theblob:
URL instead. This maintains the one-time execution and reference deduplication that modules are supposed to have.The execution of module script elements is always deferred until after the document is parsed, so we don't need to worry about trying to support the way that traditional script elements can modify the document while it's still being parsed.
Warnings: this simple implementation doesn't handle script elements added after the initial document has been parsed, or allow script elements to import from other script elements that occur after them in the document. If you have both
module
andinline-module
script elements in a document, their relative execution order may not be correct. The source code transformation is performed using a crude regex that won't handle some edge cases such as periods in IDs.I don't believe that's possible.
For inline scripts you're stuck with one of the more traditional ways of modularizing code, like the namespacing you demonstrated using object literals.
With webpack you can do code splitting which you could use to grab a very minimal chunk of code on page load and then incrementally grab the rest as needed. Webpack also has the advantage of allowing you to use the module syntax (plus a ton of other ES201X improvements) in way more environments that just Chrome Canary.