Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 Modules without a Transpiler/Bundler step

I'm interested in using a bunch of JS libraries without depending on npm-based tooling and additional bundling steps.

With ES6 modules support in the browser, i can use modules like this:

<script type="module">
    import Vue from 'https://unpkg.com/[email protected]/dist/vue.esm.browser.min.js';
    new Vue({...});
</script>

Which is fine when the required module doesn't have any transitive dependencies. But usually, those modules from the transpiled pre-ES6 world do it like this:

import Vue from 'vue'

Which doesn't seem to work in todays browsers. I'm missing some kind of option, to associate the module specifier with a certain URL, let's say as an attribute to a <script> tag.

A pragmatic solution would be to just go back to use the UMD builds of modules, which are installed into the global namespace and allows me to explictly list all dependencies in the main HTML file.

But I'm interested in the conceptual story. The bundler tools tell it as they will be obsolete in the future when there is native support, but as of now, the browser support is pretty useless, because the ecosystem is probably not consistently going to shift to importing modules by relative paths.

like image 959
strfry Avatar asked Apr 26 '19 13:04

strfry


People also ask

Can you use modules without Webpack?

With JavaScript modules fully supported in every major browser, savvy developers can now deliver production-ready apps without Webpack.

Do I need Webpack for ES6 modules?

For most browsers, yes, you can accomplish getting all needed code to the browser with just ES6 modules, without Webpack.

Is Webpack a transpiler?

Webpack bundles our code and outputs a transpiled version down to the target as specified in the configuration file. In our webpack configuration file, module rules allow us to specify different loaders, which is an easy way to display loaders.

What is ES6 module transpiler?

ES6 Module Transpiler is an experimental compiler that allows you to write your JavaScript using a subset of the ES6 module syntax, and compile it into AMD or CommonJS modules. This compiler provides a way to experiment with ES6 syntax in real world scenarios to see how the syntax holds up.


2 Answers

For ES module features without the use of a bundler in "most" browsers try es-module-shims:

  • https://github.com/guybedford/es-module-shims

This adds a -shim variant of the current import map specification. Which can be polyfilled onto browsers with baseline ES module support.

like image 170
CoreyOConnor Avatar answered Sep 17 '22 18:09

CoreyOConnor


I found a pending extension that promises to fill this gap: https://github.com/WICG/import-maps

import maps allow to specify a mapping between short module specifiers and a URL:

<script type="importmap">
{
  "imports": {
    "vue": "https://unpkg.com/[email protected]/dist/vue.esm.browser.js" 
  }
}
</script>

Only downside is, as of now, they're only support in most recent Chrome 74, and hidden behind an experimental flag: chrome://flags/#enable-built-in-module-infra

like image 38
strfry Avatar answered Sep 17 '22 18:09

strfry