@hyrious/esbuild-plugin-commonjs
Advanced tools
Comparing version 0.2.1 to 0.2.2
38
index.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
@@ -7,3 +8,2 @@ var __defProp = Object.defineProperty; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
@@ -13,18 +13,15 @@ for (var name in all) | ||
}; | ||
var __reExport = (target, module2, copyDefault, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
return to; | ||
}; | ||
var __toESM = (module2, isNodeMode) => { | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
return (module2, temp) => { | ||
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); | ||
}; | ||
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
@@ -37,2 +34,3 @@ // index.ts | ||
}); | ||
module.exports = __toCommonJS(esbuild_plugin_commonjs_exports); | ||
var import_fs = require("fs"); | ||
@@ -282,3 +280,5 @@ | ||
} | ||
transformed.push(`export { ${exportsMap.map(([e, name]) => e === name ? e : `${name} as ${JSON.stringify(e)}`).join(", ")} };`); | ||
transformed.push( | ||
`export { ${exportsMap.map(([e, name]) => e === name ? e : `${name} as ${JSON.stringify(e)}`).join(", ")} };` | ||
); | ||
} | ||
@@ -291,3 +291,6 @@ } else { | ||
if (exportsMap.length > 0) { | ||
transformed.push(`var { ${exportsMap.map(([e, name]) => e === name ? e : `${JSON.stringify(e)}: ${name}`).join(", ")} } = exports;`, `export { ${exportsMap.map(([e, name]) => e === name ? e : `${name} as ${JSON.stringify(e)}`).join(", ")} };`); | ||
transformed.push( | ||
`var { ${exportsMap.map(([e, name]) => e === name ? e : `${JSON.stringify(e)}: ${name}`).join(", ")} } = exports;`, | ||
`export { ${exportsMap.map(([e, name]) => e === name ? e : `${name} as ${JSON.stringify(e)}`).join(", ")} };` | ||
); | ||
} | ||
@@ -349,3 +352,2 @@ } | ||
var esbuild_plugin_commonjs_default = commonjs; | ||
module.exports = __toCommonJS(esbuild_plugin_commonjs_exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -352,0 +354,0 @@ 0 && (module.exports = { |
@@ -205,3 +205,3 @@ import type { Message, Plugin } from "esbuild"; | ||
} catch (err) { | ||
({ warnings } = err); | ||
({ warnings } = err as any); | ||
} | ||
@@ -208,0 +208,0 @@ |
{ | ||
"name": "@hyrious/esbuild-plugin-commonjs", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Bundle commonjs externals in es module in esbuild.", | ||
"main": "index.js", | ||
"types": "index.ts", | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -12,3 +12,4 @@ "index.js", | ||
"lexer.ts", | ||
"utils.ts" | ||
"utils.ts", | ||
"index.d.ts" | ||
], | ||
@@ -34,6 +35,9 @@ "keywords": [ | ||
"devDependencies": { | ||
"@hyrious/esbuild-dev": "^0.7.4", | ||
"@types/node": "^17.0.21", | ||
"@hyrious/esbuild-dev": "^0.8.6", | ||
"@types/node": "^14", | ||
"cjs-module-lexer": "^1.2.2", | ||
"esbuild": "^0.14.26" | ||
"esbuild": "^0.15.14", | ||
"rollup": "^3.3.0", | ||
"rollup-plugin-dts": "^5.0.0", | ||
"typescript": "^4.9.3" | ||
}, | ||
@@ -44,6 +48,7 @@ "engines": { | ||
"scripts": { | ||
"build": "esbuild index.ts --bundle --external:esbuild --external:cjs-module-lexer --sourcemap --sources-content=false --outfile=index.js --platform=node --target=node14", | ||
"build": "npm run build:js && npm run build:type", | ||
"build:js": "esbuild index.ts --bundle --external:esbuild --external:cjs-module-lexer --sourcemap --sources-content=false --outfile=index.js --platform=node --target=node14", | ||
"build:type": "esbuild-dev build-type.ts", | ||
"test": "esbuild-dev index.test.ts" | ||
}, | ||
"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" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
9
804
43338
7