This shows how to distribute an ES6 library using Babel and rollup.
package.json#module
points to the raw ES6 code for modern browser consumptionpackage.json#main
points to legacy browser/node support and CommonJS modules.
Under the hood, it uses Babel and babel-runtime for legacy browser/Node.js support. babel-runtime (actually the transform-runtime plugin) polyfills only what the library uses and does so in a way that doesn't conflict with other versions of Babel. This is perfect for libraries.
We use rollup only to create the UMD bundle for JSFiddle consumption.
npm run build
// then publish to npm...
npm install sample-es6-library // from an app
// Web, webpack consumption, modern browsers only
import foo from 'sample-es6-library'
// Web, webpack consumption, legacy browser support
import foo from 'sample-es6-library/dist/legacy/index'
// Node.js
const foo = require('sample-es6-library')
JSFiddle support: dist/legacy-umd/index.js
is a UMD bundle that can
be put behind a CDN and loaded via a <script>
tag. For
example,
here it is via jsdeliver.com,
and here it is in a fiddle.