10.1.0 • Published 5 years ago

rollup-plugin-commonjs v10.1.0

Weekly downloads
433,227
License
MIT
Repository
github
Last release
5 years ago

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' ]
    })
  ]
};

Usage with symlinks

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

zewidedv-chartsvuecomponents-testpackage@admin-bro/coremarigold-build@bunchee/buncheecloudenly-payment-plugincloudenlypaymentplugincf-storybookjsxtal@mycolorway/vest@jumpmann3o/component-librarytsdx-extrasvelteit@huozhi/burritoqinshixixing-rollupfv-player@nodesoccoop/angular-ncr-srcsadmin-bro-jzunga-frameworkreact-form-typescriptnotiontheory-basic-build@codebite/babel-plugin-web-modulesnew-vue-testnew-vue-test1finlab-components@naetverkjs/naetverk-cliadmiral_storybooklegal-content-front-webcmap-build-helpersesrlab-build-helpersmapyyw-build-helpers@idata-apps/random-scatersmap-build-helpers@deity/falcon-scriptstamm-pkg-uicloud-archive-s3@yoteam/toolgoodyui@peter.ingram/section-s@victoriatong/common-componentreact-mxgraph-editorvideo-clip-ljsgha-componentsgilsomo-newstructurerikaard1briolink-ui-kitvigoworks.componentsreact-artsy-modal@shiluodexiaobaitu/zcutilskingsun-ui-expandlightlib-uibussr-admin-brovue-waterfall-jy@lp0124/a@codewitchbella/microbundlediversify-uitakla-builder-rollup@schoonlabs/blocks@fle.cli/react-build@thebespokepixel/cordialcross-selectoralex-frontend-sdk-stagingnbsdkchangzhn-jsonptinboxis-64-colorgulp-pipelinessr-enginestorybook-tutorial-bhjwangyin2020xiao-zhang-uiblazedocbib-librarykitopi-commoniotcommonjsnarumi-test@moreillon/vue_login_form@chn-js/canvas-toolsazure-functions-bundleryan-test-bricks-teachxania-clizg-libvue-rollup-exampleformily-antdvdyc1-components@infinitebrahmanuniverse/nolb-rollup-plugin-cpackage-scripts@hui_wu/testfamom-ui3automatic-interactionez-toastnncouijgh-single-componentlibreria-reactshared-react-native-types@everything-registry/sub-chunk-2680react-styled-ui-componentsautographql@ken.zeng/app-scripts
10.1.0

5 years ago

10.0.2

5 years ago

10.0.1

5 years ago

10.0.0

6 years ago

9.3.4

6 years ago

9.3.3

6 years ago

9.3.2

6 years ago

9.3.1

6 years ago

9.3.0

6 years ago

9.2.3

6 years ago

9.2.2

6 years ago

9.2.1

6 years ago

9.2.0

6 years ago

9.1.8

6 years ago

9.1.7

6 years ago

9.1.6

6 years ago

9.1.5

6 years ago

9.1.4

6 years ago

9.1.3

7 years ago

9.1.2

7 years ago

9.1.1

7 years ago

9.1.0

7 years ago

9.0.0

7 years ago

8.4.1

7 years ago

8.4.0

7 years ago

8.3.0

7 years ago

8.2.6

7 years ago

8.2.5

7 years ago

8.2.4

7 years ago

8.2.3

7 years ago

8.2.1

7 years ago

8.2.0

7 years ago

8.1.1

7 years ago

8.1.0

7 years ago

8.0.2

8 years ago

8.0.1

8 years ago

8.0.0

8 years ago

7.1.0

8 years ago

7.0.2

8 years ago

7.0.1

8 years ago

7.0.0

8 years ago

6.0.1

8 years ago

6.0.0

8 years ago

5.0.5

8 years ago

5.0.4

8 years ago

5.0.3

8 years ago

5.0.2

8 years ago

5.0.1

8 years ago

5.0.0

8 years ago

4.1.0

8 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.3.1

8 years ago

3.3.0

8 years ago

3.2.0

8 years ago

3.1.0

9 years ago

3.0.2

9 years ago

3.0.1

9 years ago

3.0.0

9 years ago

2.2.1

9 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.4.0

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago