UNPKG

3.36 kBMarkdownView Raw
1# rollup-plugin-commonjs [![Build Status][travis-img]][travis]
2
3[travis-img]: https://travis-ci.org/rollup/rollup-plugin-commonjs.svg
4[travis]: https://travis-ci.org/rollup/rollup-plugin-commonjs
5
6Convert CommonJS modules to ES6, so they can be included in a Rollup bundle
7
8
9## Installation
10
11```bash
12npm install --save-dev rollup-plugin-commonjs
13```
14
15
16## Usage
17
18Typically, you would use this plugin alongside [rollup-plugin-node-resolve](https://github.com/rollup/rollup-plugin-node-resolve), so that you could bundle your CommonJS dependencies in `node_modules`.
19
20```js
21// rollup.config.js
22import commonjs from 'rollup-plugin-commonjs';
23import nodeResolve from 'rollup-plugin-node-resolve';
24
25export default {
26 input: 'main.js',
27 output: {
28 file: 'bundle.js',
29 format: 'iife'
30 },
31 plugins: [
32 nodeResolve({
33 jsnext: true,
34 main: true
35 }),
36
37 commonjs({
38 // non-CommonJS modules will be ignored, but you can also
39 // specifically include/exclude files
40 include: 'node_modules/**', // Default: undefined
41 exclude: [ 'node_modules/foo/**', 'node_modules/bar/**' ], // Default: undefined
42 // these values can also be regular expressions
43 // include: /node_modules/
44
45 // search for files other than .js files (must already
46 // be transpiled by a previous plugin!)
47 extensions: [ '.js', '.coffee' ], // Default: [ '.js' ]
48
49 // if true then uses of `global` won't be dealt with by this plugin
50 ignoreGlobal: false, // Default: false
51
52 // if false then skip sourceMap generation for CommonJS modules
53 sourceMap: false, // Default: true
54
55 // explicitly specify unresolvable named exports
56 // (see below for more details)
57 namedExports: { './module.js': ['foo', 'bar' ] }, // Default: undefined
58
59 // sometimes you have to leave require statements
60 // unconverted. Pass an array containing the IDs
61 // or a `id => boolean` function. Only use this
62 // option if you know what you're doing!
63 ignore: [ 'conditional-runtime-dependency' ]
64 })
65 ]
66};
67```
68
69### Custom named exports
70
71This plugin will attempt to create named exports, where appropriate, so you can do this...
72
73```js
74// importer.js
75import { named } from './exporter.js';
76
77// exporter.js
78module.exports = { named: 42 }; // or `exports.named = 42;`
79```
80
81...but that's not always possible:
82
83```js
84// importer.js
85import { named } from 'my-lib';
86
87// my-lib.js
88var myLib = exports;
89myLib.named = 'you can\'t see me';
90```
91
92In those cases, you can specify custom named exports:
93
94```js
95commonjs({
96 namedExports: {
97 // left-hand side can be an absolute path, a path
98 // relative to the current directory, or the name
99 // of a module in node_modules
100 'node_modules/my-lib/index.js': [ 'named' ]
101 }
102})
103```
104
105
106## Strict mode
107
108ES modules are *always* parsed in strict mode. That means that certain non-strict constructs (like octal literals) will be treated as syntax errors when Rollup parses modules that use them. Some older CommonJS modules depend on those constructs, and if you depend on them your bundle will blow up. There's basically nothing we can do about that.
109
110Luckily, there is absolutely no good reason *not* to use strict mode for everything — so the solution to this problem is to lobby the authors of those modules to update them.
111
112
113## License
114
115MIT