{"id":16759136,"url":"https://github.com/alcalzone/esm2cjs","last_synced_at":"2025-03-21T23:31:52.197Z","repository":{"id":57096175,"uuid":"377512000","full_name":"AlCalzone/esm2cjs","owner":"AlCalzone","description":"Command line utility to compile a JS project from ES modules to CommonJS","archived":false,"fork":false,"pushed_at":"2022-08-17T11:09:21.000Z","size":236,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-14T04:07:21.058Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlCalzone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"AlCalzone","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2021-06-16T13:49:32.000Z","updated_at":"2024-08-13T14:08:47.000Z","dependencies_parsed_at":"2022-08-22T23:00:30.625Z","dependency_job_id":null,"html_url":"https://github.com/AlCalzone/esm2cjs","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlCalzone%2Fesm2cjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlCalzone%2Fesm2cjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlCalzone%2Fesm2cjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlCalzone%2Fesm2cjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlCalzone","download_url":"https://codeload.github.com/AlCalzone/esm2cjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221820548,"owners_count":16886201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T04:07:24.260Z","updated_at":"2025-03-21T23:31:52.190Z","avatar_url":"https://github.com/AlCalzone.png","language":"JavaScript","funding_links":["https://github.com/sponsors/AlCalzone"],"categories":[],"sub_categories":[],"readme":"# esm2cjs\n\nCommand line utility to compile a JS project from ES modules to CommonJS. This is handy when building hybrid ESM/CommonJS npm packages.\n\nBuilt on top of the blazing fast [esbuild](https://github.com/evanw/esbuild) and supports all modern JS features.\n\n## Install\n\nEither globally:\n```bash\n# using npm\nnpm i -g @alcalzone/esm2cjs\n# using yarn\nyarn add --global @alcalzone/esm2cjs\n```\n\nOr locally as a `devDependency`:\n```bash\n# using npm\nnpm i -D @alcalzone/esm2cjs\n# using yarn\nyarn add --dev @alcalzone/esm2cjs\n```\n\n## Usage\nUsing the binary outside of `package.json` scripts requires global installation.\n```bash\nesm2cjs --in path/to/input --out path/to/output [options]\n```\n\nDetailed help is shown on the command line using\n\n```bash\nesm2cjs --help\n```\n\n## Example for a TypeScript project\n\n1. Configure `tsconfig.json` to write ESM output into `build/esm`:\n\n    ```jsonc\n    // tsconfig.json\n    {\n    \t// ...\n    \t\"compilerOption\": {\n    \t\t// ...\n    \t\t\"outDir\": \"build/esm\",\n    \t\t\"module\": \"ES2020\" // or some of the new options in TS 4.5 \"node12\", \"nodenext\"\n    \t}\n    }\n    ```\n\n1. Add a `postbuild` script that transforms the ESM output to CommonJS:\n\n    ```jsonc\n    // package.json\n    {\n    \t// ...\n    \t\"scripts\": {\n    \t\t// ...\n    \t\t\"postbuild\": \"esm2cjs --in build/esm --out build/cjs -l error\"\n    \t}\n    }\n    ```\n\n1. Set up the `exports` field in `package.json`. Note that the syntax for `types` might need additional changes when [TypeScript 4.5](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#packagejson-exports-imports-and-self-referencing) lands.\n    ```jsonc\n    // package.json\n    {\n    \t\"main\": \"build/cjs/index.js\",\n    \t\"module\": \"build/esm/index.js\",\n    \t\"exports\": {\n    \t\t\".\": {\n    \t\t\t\"import\": \"./build/esm/index.js\",\n    \t\t\t\"require\": \"./build/cjs/index.js\"\n    \t\t},\n    \t\t// This is necessary to require/import `your-library/package.json`\n    \t\t\"./package.json\": \"./package.json\",\n    \t\t// additional subpath exports go here, e.g. your-library/tools\n    \t\t\"./tools\": {\n    \t\t\t\"import\": \"./build/esm/tools.js\",\n    \t\t\t\"require\": \"./build/cjs/tools.js\"\n    \t\t},\n    \t\t// or everything in the source root:\n    \t\t\"./*\": {\n    \t\t\t\"import\": \"./build/esm/*.js\",\n    \t\t\t\"require\": \"./build/cjs/*.js\"\n    \t\t}\n    \t},\n    \t// ...\n    }\n    ```\n\n## Example for a JavaScript project without compilation\n\nThis assumes your ESM modules are located in `lib/` and the cjs output goes to `dist/cjs/`.\n\n1. Add a `build` script that transforms the ESM output to CommonJS:\n\n    ```jsonc\n    // package.json\n    {\n    \t// ...\n    \t\"scripts\": {\n    \t\t// ...\n    \t\t\"build\": \"esm2cjs --in lib --out dist/cjs -l error\"\n    \t}\n    }\n    ```\n\n1. Set up the `exports` field in `package.json`.\n    ```jsonc\n    // package.json\n    {\n    \t\"main\": \"dist/cjs/index.js\",\n    \t\"module\": \"lib/index.js\",\n    \t\"exports\": {\n    \t\t\".\": {\n    \t\t\t\"import\": \"./lib/index.js\",\n    \t\t\t\"require\": \"./dist/cjs/index.js\"\n    \t\t},\n    \t\t// This is necessary to require/import `your-library/package.json`\n    \t\t\"./package.json\": \"./package.json\",\n    \t\t// additional subpath exports go here, e.g. `your-library/tools`\n    \t\t\"./tools\": {\n    \t\t\t\"import\": \"./lib/tools.js\",\n    \t\t\t\"require\": \"./dist/cjs/tools.js\"\n    \t\t},\n    \t\t// or everything in the lib folder:\n    \t\t\"./*\": {\n    \t\t\t\"import\": \"./lib/*.js\",\n    \t\t\t\"require\": \"./dist/cjs/*.js\"\n    \t\t}\n    \t}\n    \t// ...\n    }\n    ```\n\n## Performance\n\n### Test case 1: [`zwave-js`](https://github.com/zwave-js/node-zwave-js)\n\n| No. of input files | total size | time taken |\n| ------------------ | ---------- | ---------- |\n| 141                | 987 KB     | 390 ms     |\n\n## Changelog\n\n\u003c!--\n  Placeholder for the next version:\n  ### **WORK IN PROGRESS**\n--\u003e\n### 1.4.1 (2024-11-20)\n* Set esbuild's `keepNames` option to `true` by default\n\n### 1.4.0 (2024-11-11)\n* Support inheriting and rewriting the `imports` field in `package.json`\n\n### 1.3.0 (2024-11-06)\n* Support specifying `sideEffects` in the generated `package.json` as a hint for bundlers\n\n### 1.2.3 (2024-11-04)\n* Update default target to node18\n* Shim `import.meta.url` by default\n* Update documentation for package.json setup\n\n### 1.1.2 (2022-08-17)\nDependency updates\n\n### 1.1.1 (2021-10-18)\nDependency updates\n\n### 1.1.0 (2021-06-19)\nSupport specifying output target and platform\n\n### 1.0.0 (2021-06-16)\n\nInitial release\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcalzone%2Fesm2cjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falcalzone%2Fesm2cjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcalzone%2Fesm2cjs/lists"}