UNPKG

4.2 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: { 'react': ['createElement', 'Component' ] }, // 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### Usage with symlinks
70
71Symlinks are common in monorepos and are also created by the `npm link` command. Rollup with `rollup-plugin-node-resolve` resolves modules to their real paths by default. So `include` and `exclude` paths should handle real paths rather than symlinked paths (e.g. `../common/node_modules/**` instead of `node_modules/**`). You may also use a regular expression for `include` that works regardless of base path. Try this:
72
73```
74commonjs({
75 include: /node_modules/
76})
77```
78
79Whether symlinked module paths are [realpathed](http://man7.org/linux/man-pages/man3/realpath.3.html) or preserved depends on Rollup's `preserveSymlinks` setting, which is false by default, matching Node.js' default behavior. Setting `preserveSymlinks` to true in your Rollup config will cause `import` and `export` to match based on symlinked paths instead.
80
81### Custom named exports
82
83This plugin will attempt to create named exports, where appropriate, so you can do this...
84
85```js
86// importer.js
87import { named } from './exporter.js';
88
89// exporter.js
90module.exports = { named: 42 }; // or `exports.named = 42;`
91```
92
93...but that's not always possible:
94
95```js
96// importer.js
97import { named } from 'my-lib';
98
99// my-lib.js
100var myLib = exports;
101myLib.named = 'you can\'t see me';
102```
103
104In those cases, you can specify custom named exports:
105
106```js
107commonjs({
108 namedExports: {
109 // left-hand side can be an absolute path, a path
110 // relative to the current directory, or the name
111 // of a module in node_modules
112 'my-lib': [ 'named' ]
113 }
114})
115```
116
117
118## Strict mode
119
120ES 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.
121
122Luckily, 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.
123
124
125## License
126
127MIT