Skip to content

Usage with build tools

Koen edited this page Nov 8, 2019 · 1 revision

This package is published with untranspiled JavaScript. All files are in the form of ECMAScript Modules (ESM), with .mjs as file extension. This means that you'll need to transpile the used functions yourself, especially when used on the web.

Not every build tool or bundler will recognize .mjs files correctly, and not every setup will transpile these files when they're in the node_modules folder. Here's a list with commonly used tools and usage instructions:

Webpack

The latest version of Webpack should transpile .mjs files properly when used with the default Babel loader (babel-loader).

Rollup

The latest version of Rollup should transpile .mjs files properly when used with the default Babel plugin (rollup-plugin-babel).

Browserify

Use the following babelify settings to transform .mjs files in the node_modules:

global: true,
ignore: /\/node_modules\/(?!.*.*\/.*.mjs)/,

The esmify plugin might also prove to be useful.

Babel

If you're transpiling with Babel in any other setup, use the following ignore pattern to properly ignore the node_modules and allow .mjs files to be transpiled:

ignore: [/\/node_modules\/(?!.*.*\/.*.mjs)/],

This can be added in your babel.config.js, .babelrc or package.json; quotes will be necessary for JSON-based configurations.

Node

Run Node with support for ECMAScript Modules enabled:

node --experimental-modules my-app.mjs
Clone this wiki locally