{"id":13393890,"url":"https://github.com/ai/dual-publish","last_synced_at":"2025-04-05T00:05:43.973Z","repository":{"id":45034303,"uuid":"240781702","full_name":"ai/dual-publish","owner":"ai","description":"Publish JS project as dual ES modules and CommonJS package to npm","archived":false,"fork":false,"pushed_at":"2024-08-30T23:12:20.000Z","size":2044,"stargazers_count":187,"open_issues_count":2,"forks_count":11,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T23:07:00.630Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/ai.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-15T20:14:15.000Z","updated_at":"2024-10-29T17:56:17.000Z","dependencies_parsed_at":"2024-12-21T17:06:37.282Z","dependency_job_id":"5fbe2c05-392b-4c49-806c-3a385d3e2c94","html_url":"https://github.com/ai/dual-publish","commit_stats":{"total_commits":269,"total_committers":10,"mean_commits":26.9,"dds":0.1189591078066915,"last_synced_commit":"b046ba4cac333bd30521318cc3da80bd526e89ba"},"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fdual-publish","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fdual-publish/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fdual-publish/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fdual-publish/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ai","download_url":"https://codeload.github.com/ai/dual-publish/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266562,"owners_count":20910836,"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-07-30T17:01:01.941Z","updated_at":"2025-04-05T00:05:43.952Z","avatar_url":"https://github.com/ai.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Tools"],"sub_categories":[],"readme":"# Dual Publish\n\nPublish JS project as dual ES modules and CommonJS package to npm.\n\n* Tested on [projects] with **16M downloads per month**.\n* [CI tests] with it works with **Node.js**, **browsers**, **React Native**,\n  **bundlers** (webpack, Parcel, Rollup, or esbuild) and **CDNs** (like jspm).\n* Does not change **line numbers in stacktrace** and keep sources **readable**.\n* **No build step.** No need for separated `src/` and `dist/` dirs in repository.\n  You will be able to test branch by installing version from GitHub like\n  `npm i example@you/example#fix`.\n* **Multiple files support**. Your user will be able to import separated files\n  \u003cbr/\u003elike `import { nanoid } from 'nanoid/async'`.\n* Cleans npm package from development configs [before publishing].\n* Supports `process.env.NODE_ENV` for development checks, which you want\n  to remove in production JS bundle.\n\nYou write CommonJS in your npm library sources:\n\n```js\n// index.js\nmodule.exports = { lib }\n```\n\n`npx dual-publish` compiles your library during publishing to npm:\n\n```js\n// index.js\nexport { lib }\n\n// index.cjs\nmodule.exports = { lib }\n\n// package.json\n{\n  …\n  \"type\": \"module\",\n  \"module\": \"index.js\",\n  \"main\": \"index.cjs\",\n  \"exports\": {\n    \"require\": \"./index.cjs\",\n    \"import\": \"./index.js\"\n  }\n}\n```\n\nNow your library can be imported natively as ESM or CommonJS:\n\n```js\n// CommonJS\nlet { lib } = require('lib')\n\n// ESM in Node.js, webpack, Parcel, and Rollup\nimport { lib } from 'lib'\n\n// ESM in browser\nimport { lib } from 'https://cdn.jsdelivr.net/npm/lib/index.js'\n```\n\n[before publishing]: https://github.com/shashkovdanil/clean-publish/\n[CI tests]: https://github.com/ai/dual-publish/blob/master/test/index.test.js\n[projects]: https://github.com/search?l=JSON\u0026q=%22dual-publish%22\u0026type=Code\n\n\u003ca href=\"https://evilmartians.com/?utm_source=dual-publish\"\u003e\n  \u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\"\n      alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\n\u003c/a\u003e\n\n\n## Limits\n\n* We recommend to avoid default export because of [bug in webpack].\n* Every JS file should be in own dir. `lib/index.js` instead of `lib.js`.\n  We need it to put `package.json` with `module`.\n\n[bug in webpack]: https://github.com/webpack/webpack/issues/6584\n\n\n## Usage\n\n1. Take a normal CommonJS project with `require()` and `module.exports =`.\n2. Because of [bug in webpack] we recommend to use only named exports:\n\n   ```js\n   const NAME = 'a'\n   function change {\n     …\n   }\n\n   module.exports = { NAME, change }\n   ```\n3. Move all files into separated dirs. Rename `lib.js` to `lib/index.js`.\n   Old `require('./lib')` will work.\n4. Add `dual-publish` to the project:\n\n   ```sh\n   npm i --save-dev dual-publish\n   ```\n5. Test the result by calling `npx dual-publish --check`.\n   It will create a folder in your project with converted files.\n   Review them manually.\n6. Publish your project with `npx dual-publish` instead of `npm publish`.\n\n   ```sh\n   npx dual-publish\n   ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fdual-publish","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fai%2Fdual-publish","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fdual-publish/lists"}