@hyrious/esbuild-plugin-commonjs
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "@hyrious/esbuild-plugin-commonjs", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Bundle commonjs externals in es module in esbuild.", | ||
@@ -33,9 +33,9 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@hyrious/esbuild-dev": "^0.7.2", | ||
"@types/node": "^17.0.19", | ||
"@hyrious/esbuild-dev": "^0.7.4", | ||
"@types/node": "^17.0.21", | ||
"cjs-module-lexer": "^1.2.2", | ||
"esbuild": "^0.14.23" | ||
"esbuild": "^0.14.26" | ||
}, | ||
"engines": { | ||
"node": "14" | ||
"node": ">=14" | ||
}, | ||
@@ -46,3 +46,3 @@ "scripts": { | ||
}, | ||
"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\"); // <- you dislike this\n(\"...\");\nexport { render };\n\n// with this plugin\nimport __import_react from \"react\"; // <- 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<!-- prettier-ignore -->\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(() => 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**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\nType:\n\n```ts\ntransform: boolean | ((path: string) => {\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<!-- prettier-ignore -->\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 <q>rollup</q>, but is not a good\nsolution. It depends on a feature [<q>syntheticNamedExports</q>][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.1.1\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" | ||
"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\"); // <- you dislike this\n(\"...\");\nexport { render };\n\n// with this plugin\nimport __import_react from \"react\"; // <- 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<!-- prettier-ignore -->\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(() => 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**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\nType:\n\n```ts\ntransform: boolean | ((path: string) => {\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<!-- prettier-ignore -->\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 <q>rollup</q>, but is not a good\nsolution. It depends on a feature [<q>syntheticNamedExports</q>][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.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" | ||
} |
@@ -119,3 +119,3 @@ # @hyrious/esbuild-plugin-commonjs | ||
### 0.1.1 | ||
### 0.2.0 | ||
@@ -122,0 +122,0 @@ Add experimental option `transform` and `transformConfig`. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44539