Mirror: Rollup plugin to automatically check the exports of a library for cjs-module-lexer compatibility.
1# rollup-plugin-cjs-check
2
3A Rollup plugin for checking CommonJS exports against `cjs-module-lexer`.
4
5## Why?
6
7As Node.js defines it, CommonJS modules consist of a `module.exports` object which can be of any type.
8When importing a `CommonJS`, while Node.js is in ESM mode, a CommonJS module will always be reliably
9importable via a default import.
10
11For modern modules, it's common to use Rollup to bundle and build both CommonJS and ECMAScript Modules (ESM)
12outputs for any given module. This means that to achieve compatibility between both modules either requires
13the module to only have a default-export, or to rely on Node.js' compatibility mode.
14
15For better compatibility, Node.js attempts to detect the CommonJS exports of every CommonJS module that's
16exported. Since sometimes, especially in legacy CommonJS mode, the import of a given CommonJS module built with
17Rollup cannot be prevented entirely, the module has to be compatible with `cjs-module-lexer`.
18
19The [`cjs-module-lexer` library](https://github.com/nodejs/cjs-module-lexer) is what Node.js uses to detect
20exports of a given CommonJS module and hence, when choosing how to configure Rollup, the Rollup bundle outputs
21have to be compatible with it, or otherwise, the module's exports won't be detected.
22
23**This plugin checks that `cjs-module-lexer` agrees with the ECMAScript modules exports that Rollup detects.**
24
25For more information, check out [the Node.js documentation on CommonJS namespaces.](https://nodejs.org/api/esm.html#commonjs-namespaces)
26
27## Requirements
28
29This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.
30
31## Install
32
33```console
34npm install --save-dev rollup-plugin-cjs-check
35# or
36yarn add -D rollup-plugin-cjs-check
37# or
38pnpm add --save-dev rollup-plugin-cjs-check
39```
40
41## Usage
42
43Import the `rollup-plugin-cjs-check` plugin in your [Rollup configuration file](https://www.rollupjs.org/guide/en/#configuration-files),
44and add it to your plugins:
45
46```js
47import cjsCheck from 'rollup-plugin-cjs-check';
48
49export default {
50 input: 'src/index.js',
51 output: {
52 dir: 'output',
53 format: 'cjs'
54 },
55 plugins: [
56 cjsCheck(),
57 ]
58};
59```