Skip to content

matthewp/script-type-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

<script type=module> - the polyfill!

An attempt to build a polyfill for <script type="module">. Because the waiting is the hardest part.

This polyfill includes:

  • All import and export forms.
  • Import bindings compliant with the spec; meaning a dependency can update its exported value and your module binding will reflect that change.
  • A web worker architecture that allows for spawning multiple workers for faster load times (experimental).
  • Full source maps.

This is alpha-ish software that is tested, but not used for any real projects (yet). Check out the tests/ folder to see what's been worked on, submit any issues you encounter.

This polyfill uses a different type "module-polyfill" to avoid constraining browser implementations.

Install

npm install script-type-module

Use

<script src="./node_modules/script-type-module/polyfill.js"></script>

<script type="module-polyfill" src="./foo.js"></script>

Dynamic import

The stage 2 import() proposal allows you to dynamically import code based on runtime considerations. This is useful if you want to delay loading parts of the application for any reason (such as based on user input).

This polyfill includes support for import(). Here's an example usage:

/pages/home.js

export function page(){
  let section = document.createElement('section');
  section.innerHTML = `
    <h1>Home</h1>

    <p>Welcome home!</p>
  `;

  return section;
}

index.html

<main></main>

<script src="./node_modules/script-type-module/polyfill.js"></script>

<script type="module-polyfill">
  let page = location.hash.substr(1);

  import(`./pages/${page}.js`).then(ns => {
    document.querySelector('main').appendChild(ns.page());
  });
</script>

In this case we are dynamically inserting DOM based on the location.hash, so that if the URL is http://example.com#home the /pages/home.js module is dynamically imported and inserted into <main>.

FAQ

Why use this instead of a bundler?

Bundlers are not great for development workflows. Being able to create new .html pages to test out small parts of your application without creating a new build just for that, is a super powerful thing.

Should I use this in production?

No! This is primarily meant for better development workflows. Do a real build for production.

How does this compare to es-module-loader?

es-module-loader is a polyfill for the in progress whatwg/loader spec. This spec has been through several revisions going back many years and isn't likely to appear in browsers soon.

This polyfill is based on the already specified <script type="module"> that is coming to browsers soon (in Edge developer preview already). The hope is that most evergreen browsers will have support within 6 months and this will only be used for testing on older browsers.

Licence

BSD 2 Clause

About

A <script type=module> polyfill

Resources

Stars

Watchers

Forks

Packages

No packages published