Overview of cjstoesm
mastercjstoesm is a tool designed to transform CommonJS modules into tree-shakeable ES Modules. This enables CommonJS modules to be bundled for the browser or used with modern tools like Rollup.
Unlike other solutions that use module wrappers, cjstoesm produces clean, idiomatic, and granular code. It attempts to convert exports and module.exports into direct export statements and converts require() calls into import statements.
Example Transformations:
Exports:
Input:
exports.foo = function foo() {};Output:
export function foo() {}Input:
module.exports = { foo() { ... }, bar: 3 };Output:
export function foo() { ... }; export const bar = 3; export default {foo, bar};
Imports:
- Input:
const {foo: bar} = require("./my-module"); - Output:
import {foo as bar} from "./my-module.js";
Import Attributes (e.g., JSON):
- Input:
const pkg = require("./package.json"); - Output:
import pkg from "./package.json" with {type: "json"};