dmnsgn/rollup-plugin-commonjs-named-exports(opens in a new tab)
Language: JavaScript
Re-export CommonJS named exports using Node.js cjs-module-lexer.
- stars
- 3
- watchers
- 1
- forks
- 1
- open issues
- 0
- Last updated
- 06/10/2026
Language: JavaScript
Re-export CommonJS named exports using Node.js cjs-module-lexer.
Re-export CommonJS named exports using Node.js cjs-module-lexer.
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:
// 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 and jspm-rollup.
npm install rollup-plugin-commonjs-named-exports
No additional plugins required — Rolldown handles CJS conversion natively.
Create a rolldown.config.js configuration file and import the plugin:
import commonjsNamedExports from "rollup-plugin-commonjs-named-exports"; export default { input: "src/index.js", output: { dir: "output", format: "es", }, plugins: [commonjsNamedExports()], };
@rollup/plugin-commonjs is still required for the actual CJS→ESM conversion. This plugin only provides the named export declarations on top.
Create a rollup.config.js configuration file and import the plugin:
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 or the API.
cjs-module-lexer (the same lexer Node.js uses internally). Modules that assign exports entirely at runtime (e.g. module.exports[key] = value inside a loop) will not have their named exports detected; only the default export will be available.MIT. See license file.