Skip to content

frankandrobot/sample-es6-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sample-es6-library

This shows how to distribute an ES6 library using Babel and rollup.

  • package.json#module points to the raw ES6 code for modern browser consumption
  • package.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.

Building

npm run build
// then publish to npm...

Installation

npm install sample-es6-library // from an app

Consumption

// 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.