rollup-plugin-commonjs-named-exports
Version:
Re-export CommonJS named exports using Node.js cjs-module-lexer.
67 lines (51 loc) • 3.46 kB
Markdown
# rollup-plugin-commonjs-named-exports
[](https://www.npmjs.com/package/rollup-plugin-commonjs-named-exports)
[](https://www.npmjs.com/package/rollup-plugin-commonjs-named-exports)
[](https://bundlephobia.com/package/rollup-plugin-commonjs-named-exports)
[](https://github.com/dmnsgn/rollup-plugin-commonjs-named-exports/blob/main/package.json)
[](https://github.com/microsoft/TypeScript)
[](https://conventionalcommits.org)
[](https://github.com/prettier/prettier)
[](https://github.com/eslint/eslint)
[](https://github.com/dmnsgn/rollup-plugin-commonjs-named-exports/blob/main/LICENSE.md)
Re-export CommonJS named exports using Node.js cjs-module-lexer.
[](https://paypal.me/dmnsgn)
[](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[](https://twitter.com/dmnsgn)
Useful to allow `import { Fragment, useState } from "react";` (instead of forcing `import React from "react";`) instance for a bundled React as the source package still re-exports its CJS named exports based on `process.env.NODE_ENV`:
```cjs
// react/index.js
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/react.production.min.js");
} else {
module.exports = require("./cjs/react.development.js");
}
// react/cjs/react.development.js
// ...
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.useReducer = useReducer;
exports.useRef = useRef;
exports.useState = useState;
// ...
```
Based on [esinstall](https://github.com/FredKSchott/snowpack/blob/main/esinstall/src/rollup-plugins/rollup-plugin-wrap-install-targets.ts) and [jspm-rollup](https://github.com/jspm/rollup-plugin-jspm/blob/main/jspm-rollup.js).
## Installation
```bash
npm install rollup-plugin-commonjs-named-exports
```
## Usage
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```js
import commonjs from "@rollup/plugin-commonjs";
import commonjsNamedExports from "rollup-plugin-commonjs-named-exports";
export default {
input: "src/index.js",
output: {
dir: "output",
},
plugins: [commonjs(), commonjsNamedExports()],
};
```
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
## License
MIT. See [license file](https://github.com/dmnsgn/rollup-plugin-commonjs-named-exports/blob/main/LICENSE.md).