Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-commonjs

rollup1.2mMITdeprecated10.1.0TypeScript support: included

This package has been deprecated and is no longer maintained. Please use @rollup/plugin-commonjs.

Convert CommonJS modules to ES2015

readme

rollup-plugin-commonjs Build Status

Convert CommonJS modules to ES6, so they can be included in a Rollup bundle

Installation

npm install --save-dev rollup-plugin-commonjs

Usage

Typically, you would use this plugin alongside rollup-plugin-node-resolve, so that you could bundle your CommonJS dependencies in node_modules.

// rollup.config.js
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'iife'
  },
  plugins: [
    nodeResolve({
      jsnext: true,
      main: true
    }),

    commonjs({
      // non-CommonJS modules will be ignored, but you can also
      // specifically include/exclude files
      include: 'node_modules/**',  // Default: undefined
      exclude: [ 'node_modules/foo/**', 'node_modules/bar/**' ],  // Default: undefined
      // these values can also be regular expressions
      // include: /node_modules/

      // search for files other than .js files (must already
      // be transpiled by a previous plugin!)
      extensions: [ '.js', '.coffee' ],  // Default: [ '.js' ]

      // if true then uses of `global` won't be dealt with by this plugin
      ignoreGlobal: false,  // Default: false

      // if false then skip sourceMap generation for CommonJS modules
      sourceMap: false,  // Default: true

      // explicitly specify unresolvable named exports
      // (see below for more details)
      namedExports: { 'react': ['createElement', 'Component' ] },  // Default: undefined

      // sometimes you have to leave require statements
      // unconverted. Pass an array containing the IDs
      // or a `id => boolean` function. Only use this
      // option if you know what you're doing!
      ignore: [ 'conditional-runtime-dependency' ]
    })
  ]
};

Symlinks 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:

commonjs({
  include: /node_modules/
})

Whether symlinked module paths are realpathed 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.

Custom named exports

This plugin will attempt to create named exports, where appropriate, so you can do this...

// importer.js
import { named } from './exporter.js';

// exporter.js
module.exports = { named: 42 }; // or `exports.named = 42;`

...but that's not always possible:

// importer.js
import { named } from 'my-lib';

// my-lib.js
var myLib = exports;
myLib.named = 'you can\'t see me';

In those cases, you can specify custom named exports:

commonjs({
  namedExports: {
    // left-hand side can be an absolute path, a path
    // relative to the current directory, or the name
    // of a module in node_modules
    'my-lib': [ 'named' ]
  }
})

Strict mode

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

Luckily, 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.

License

MIT

changelog

rollup-plugin-commonjs changelog

10.1.0

2019-08-27

  • Normalize ids before looking up in named export map (#406)
  • Update README.md with note on symlinks (#405)

10.0.2

2019-08-03

  • Support preserveSymlinks: false (#401)

10.0.1

2019-06-27

  • Make tests run with Node 6 again and update dependencies (#389)
  • Handle builtins appropriately for resolve 1.11.0 (#395)

10.0.0

2019-05-15

  • Use new Rollup@1.12 context functions, fix issue when resolveId returns an object (#387)

9.3.4

2019-04-04

  • Make "extensions" optional (#384)
  • Use same typing for include and exclude properties (#385)

9.3.3

2019-04-04

  • Remove colon from module prefixes (#371)

9.3.2

2019-04-04

  • Use shared extractAssignedNames, fix destructuring issue (#303)

9.3.1

2019-04-04

  • Include typings in release (#382)

9.3.0

2019-04-03

  • Add TypeScript types (#363)

9.2.3

2019-04-02

  • Improve support for ES3 browsers (#364)
  • Add note about monorepo usage to readme (#372)
  • Add .js extension to generated helper file (#373)

9.2.2

2019-03-25

  • Handle array destructuring assignment (#379)

9.2.1

2019-02-23

  • Use correct context when manually resolving ids (#370)

9.2.0

2018-10-10

  • Fix missing default warning, produce better code when importing known ESM default exports (#349)
  • Refactor code and add prettier (#346)

9.1.8

2018-09-18

  • Ignore virtual modules created by other plugins (#327)
  • Add "location" and "process" to reserved words (#330)

9.1.6

2018-08-24

  • Keep commonJS detection between instantiations (#338)

9.1.5

2018-08-09

  • Handle object form of input (#329)

9.1.4

2018-07-27

  • Make "from" a reserved word (#320)

9.1.3

2018-04-30

  • Fix a caching issue (#316)

9.1.2

2018-04-30

  • Re-publication of 9.1.0

9.1.1

2018-04-30

  • Fix ordering of modules when using rollup 0.58 (#302)

9.1.0

  • Do not automatically wrap modules with return statements in top level arrow functions (#302)

9.0.0

  • Make rollup a peer dependency with a version range (#300)

8.4.1

  • Re-release of 8.3.0 as #287 was actually a breaking change

8.4.0

  • Better handle non-CJS files that contain CJS keywords (#285)
  • Use rollup's plugin contextparse function (#287)
  • Improve error handling (#288)

8.3.0

  • Handle multiple entry points (#283)
  • Extract named exports from exported object literals (#272)
  • Fix when options.external is modified by other plugins (#264)
  • Recognize static template strings in require statements (#271)

8.2.4

  • Don't import default from ES modules that don't export default (#206)

8.2.3

  • Prevent duplicate default exports (#230)
  • Only include default export when it exists (#226)
  • Deconflict require aliases (#232)

8.2.1

  • Fix magic-string deprecation warning

8.2.0

  • Avoid using index as a variable name (#208)

8.1.1

  • Compatibility with 0.48 (#220)

8.1.0

  • Handle options.external correctly (#212)
  • Support top-level return (#195)

8.0.2

  • Fix another var rewrite bug (#181)

8.0.1

  • Remove declarators within a var declaration correctly (#179)

8.0.0

  • Prefer the names dependencies are imported by for the common var foo = require('foo') pattern (#176)

7.1.0

  • Allow certain require statements to pass through unmolested (#174)

7.0.2

  • Handle duplicate default exports (#158)

7.0.1

  • Fix exports with parentheses (#168)

7.0.0

  • Rewrite typeof module, typeof module.exports and typeof exports as 'object' (#151)

6.0.1

  • Don't overwrite globals (#127)

6.0.0

  • Rewrite top-level define as undefined, so AMD-first UMD blocks do not cause breakage (#144)
  • Support ES2017 syntax (#132)
  • Deconflict exported reserved keywords (#116)

5.0.5

  • Fix parenthesis wrapped exports (#120)

5.0.4

  • Ensure named exports are added to default export in optimised modules (#112)

5.0.3

  • Respect custom namedExports in optimised modules (#35)

5.0.2

  • Replace require (outside call expressions) with commonjsRequire helper (#77, #83)

5.0.1

  • Deconflict against globals (#84)

5.0.0

  • Optimise modules that don't need to be wrapped in a function (#106)
  • Ignore modules containing import and export statements (#96)

4.1.0

  • Ignore dead branches (#93)

4.0.1

  • Fix ignoreGlobal option (#86)

4.0.0

  • Better interop and smaller output (#92)

3.3.1

3.3.0

  • Keep the order of execution for require calls (#43)
  • Use interopDefault as helper (#42)

3.2.0

  • Use named exports as a function when no default export is defined (#524)

3.1.0

  • Replace typeof require with 'function' (#38)
  • Don't attempt to resolve entry file relative to importer (#63)

3.0.2

  • Handle multiple references to global

3.0.1

  • Return a name

3.0.0

  • Make transform stateless (#71)
  • Support web worker global (#50)
  • Ignore global with options.ignoreGlobal (#48)

2.2.1

  • Prevent false positives with namedExports (#36)

2.2.0

  • Rewrite top-level this expressions to mean the same as global (#31)

2.1.0

  • Optimised module wrappers (#20)
  • Allow control over named exports via options.namedExports (#18)
  • Handle bare imports correctly (#23)
  • Blacklist all reserved words as export names (#21)
  • Configure allowed file extensions via options.extensions (#27)

2.0.0

  • Support for transpiled modules – exports.default is used as the default export in place of module.exports, if applicable, and __esModule is not exported (#16)

1.4.0

  • Generate sourcemaps by default

1.3.0

  • Handle references to global (#6)

1.2.0

  • Generate named exports where possible (#5)
  • Handle shadowed require/module/exports

1.1.0

  • Handle dots in filenames (#3)
  • Wrap modules in IIFE for more readable output

1.0.0

  • Stable release, now that Rollup supports plugins

0.2.1

  • Allow mixed CommonJS/ES6 imports/exports
  • Use var instead of let

0.2.0

  • Sourcemap support
  • Support options.include and options.exclude
  • Bail early if module is obviously not a CommonJS module

0.1.1

Add dist files to package (whoops!)

0.1.0

  • First release