Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WebReflection/mapped-modules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mapped Modules: Proof of Concept

This folder showcases ESM through modules as mapped names.

The modules.json file exports names usable as ESM imports.

{
  "module": "/js/hello-world.js"
}

As example, that /js/hello-world.js can be now imported via the following:

import {esm} from '/:module';
esm();

regardless of where the import comes from.

Caveats

  • the import must start with a / or ESM will throw
  • to explicitly opt in within the Service Worker modules resolver, you need to add a special, uncommon, module name prefix such : or ~ or ! or even @, so that there is no ambiguity on what the SW should look for

The only script, at the end of the page, that is needed to run /js/esm.js as entry point and fallback to /js/bundle.js is the following one:

(function (L, M, A, O) {
  if (A in M) M[A].register('sw.js')
    .then(function () {
      M[A].ready.then(function () {
        (M = L.createElement('script')).type = 'module';
        L.head.appendChild(M).src = 'js/esm.js';
      })
    })
    .catch(O);
  else O();
}(
  document,
  navigator,
  'serviceWorker',
  function () {
    document.head.appendChild(
      document.createElement('script')
    ).src = 'js/bundle.js';
  }
));

You can test this PoC directly here.

About

Proof of concept

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published