{"id":17968513,"url":"https://github.com/hyrious/esbuild-plugin-commonjs","last_synced_at":"2025-04-10T04:57:17.907Z","repository":{"id":45806618,"uuid":"445137511","full_name":"hyrious/esbuild-plugin-commonjs","owner":"hyrious","description":"An esbuild plugin to help you bundle commonjs external modules.","archived":false,"fork":false,"pushed_at":"2025-02-18T05:53:19.000Z","size":95,"stargazers_count":30,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T03:09:14.850Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/hyrious.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2022-01-06T10:58:52.000Z","updated_at":"2025-03-10T16:46:18.000Z","dependencies_parsed_at":"2024-06-18T15:15:14.782Z","dependency_job_id":"223e8120-20e9-4a54-bfbd-77a6db7cd243","html_url":"https://github.com/hyrious/esbuild-plugin-commonjs","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyrious%2Fesbuild-plugin-commonjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyrious%2Fesbuild-plugin-commonjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyrious%2Fesbuild-plugin-commonjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyrious%2Fesbuild-plugin-commonjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyrious","download_url":"https://codeload.github.com/hyrious/esbuild-plugin-commonjs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161264,"owners_count":21057554,"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-29T14:40:33.450Z","updated_at":"2025-04-10T04:57:17.881Z","avatar_url":"https://github.com/hyrious.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @hyrious/esbuild-plugin-commonjs\n\nAn esbuild plugin to help you bundle commonjs external modules.\n\nThis plugin is used to address [evanw/esbuild#1467][1], where you want to\nbundle some commonjs external modules in es modules context. But accidentally\nyou see a `__require` in your code prints error at runtime and forbids\nother bundlers from analyzing the dependencies. For example:\n\n```js\n// some commonjs library, like react-dom\nvar React = require('react')\n\n// your esm code\nexport { render } from 'react-dom'\n\n// after esbuild --bundle\nvar React = __require('react') // \u003c- you dislike this\n// ...\nexport { render }\n\n// with this plugin\nimport __import_react from 'react' // \u003c- you want this\nvar React = __import_react\n// ...\nexport { render }\n```\n\nThis plugin was inspired by [a comment under esbuild#1921][4]\nand the [prototype][5] was done after a day.\n\n## Install\n\n```bash\nnpm add -D @hyrious/esbuild-plugin-commonjs\n```\n\n## Usage\n\n\u003c!-- prettier-ignore --\u003e\n```js\nconst { commonjs } = require(\"@hyrious/esbuild-plugin-commonjs\");\n\nrequire(\"esbuild\").build({\n  entryPoints: [\"lib.js\"],\n  bundle: true,\n  format: \"esm\",\n  external: [\"react\"],\n  outfile: \"out.js\",\n  plugins: [commonjs()],\n}).catch(() =\u003e process.exit(1));\n```\n\n## Options\n\n```js\ncommonjs({ filter: /\\.c?js$/, transform: false })\n```\n\n**filter** (default: `/\\.c?js$/`)\n\nA RegExp passed to [`onLoad()`](https://esbuild.github.io/plugins/#on-load) to\nmatch commonjs modules, it is recommended to set a custom filter to skip files\nfor better performance.\n\n**requireReturnsDefault** (default: `true`)\n\n```ts\nrequireReturnsDefault: boolean | ((path: string) =\u003e boolean)\n```\n\nControls which style of import statement to use replacing require calls in commonjs modules.\n\n```js\n// input\nconst foo = require('foo')\n\n// output if requireReturnsDefault is true (default behavior)\nimport foo from 'foo'\n\n// output if requireReturnsDefault is false\nimport * as foo from 'foo'\n```\n\n**ignore**\n\nDo not convert require calls to these modules. Note that this will cause esbuild\nto generate `__require()` wrappers and throw errors at runtime.\n\n```ts\nignore: string[] | ((path: string) =\u003e boolean)\n```\n\n**transform** (default: `false`)\n\nTry to transform commonjs to es modules. This trick is done with [`cjs-module-lexer`](https://github.com/nodejs/cjs-module-lexer)\nto match the native (node) behavior as much as possible. Because this\ntransformation may cause many bugs around the interop between cjs and esm,\nit can also accept a function to filter in the \"safe to convert\" modules by yourself.\n\n```ts\ntransform: boolean | ((path: string) =\u003e {\n  behavior?: \"node\" | \"babel\", exports?: string[], sideEffects?: boolean\n} | null | void)\n```\n\nBy default, if you toggle `transform` to `true`, it will convert this code:\n\n```js\nexports.__esModule = true\nexports.default = {}\nexports.foo = 42\n```\n\nTo this:\n\n\u003c!-- prettier-ignore --\u003e\n```js\nvar exports = {}, module = { exports };\n{\n  exports.__esModule = true;\n  exports.default = {};\n  exports.foo = 42;\n}\nexport default exports;\nvar { foo } = exports;\nexport { foo };\n```\n\n## This is not equal to [@rollup/plugin-commonjs][2].\n\nThis plugin does not convert your commonjs file into es modules, it just\nreplace those `require(\"x\")` expressions with import statements. It turns out\nthat esbuild can handle this kind of mixed module (having import statement and\n`module.exports` at the same time) correctly.\n\nThe one acting the same exists in the branch \u003cq\u003erollup\u003c/q\u003e, but is not a good\nsolution. It depends on a feature [\u003cq\u003esyntheticNamedExports\u003c/q\u003e][3] and evanw\n(the author of esbuild) doesn't want to implement something out of spec.\nWithout which you have to tell the plugin every single commonjs file's named\nexports, which sucks obviously.\n\n## Changelog\n\n### 0.2.5\n\nFix: skip shebangs when injecting import statements. ([#6](https://github.com/hyrious/esbuild-plugin-commonjs/issues/6))\n\n### 0.2.4\n\nAdd options `requireReturnsDefault` and `ignore`.\n\n### 0.2.0\n\nAdd experimental option `transform` and `transformConfig`.\n\n## License\n\nMIT @ [hyrious](https://github.com/hyrious)\n\n[1]: https://github.com/evanw/esbuild/issues/1467\n[2]: https://github.com/rollup/plugins/blob/master/packages/commonjs\n[3]: https://github.com/evanw/esbuild/issues/1919\n[4]: https://github.com/evanw/esbuild/issues/1921#issuecomment-1010490128\n[5]: https://gist.github.com/hyrious/7120a56c593937457c0811443563e017\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyrious%2Fesbuild-plugin-commonjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyrious%2Fesbuild-plugin-commonjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyrious%2Fesbuild-plugin-commonjs/lists"}