@hyrious/esbuild-plugin-commonjs
Advanced tools
Comparing version 0.1.0 to 0.2.0
88
index.js
@@ -0,4 +1,6 @@ | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
@@ -18,2 +20,5 @@ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
}; | ||
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) => { | ||
@@ -194,5 +199,24 @@ return (module2, temp) => { | ||
} | ||
var reservedWords = "break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public"; | ||
var builtin = "arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl"; | ||
var forbiddenIdentifiers = new Set(`${reservedWords} ${builtin}`.split(" ")); | ||
forbiddenIdentifiers.add(""); | ||
var makeLegalIdentifier = function makeLegalIdentifier2(str) { | ||
let identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, "_"); | ||
if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) { | ||
identifier = `_${identifier}`; | ||
} | ||
return identifier || "_"; | ||
}; | ||
// index.ts | ||
function commonjs({ filter = /\.c?js$/ } = {}) { | ||
function commonjs({ | ||
filter = /\.c?js$/, | ||
transform = false, | ||
transformConfig | ||
} = {}) { | ||
let init_cjs_module_lexer; | ||
if (transform) { | ||
init_cjs_module_lexer = import("cjs-module-lexer"); | ||
} | ||
return { | ||
@@ -206,2 +230,8 @@ name: "commonjs", | ||
onLoad({ filter }, async (args) => { | ||
let parseCJS; | ||
if (init_cjs_module_lexer) { | ||
const { init, parse } = await init_cjs_module_lexer; | ||
await init(); | ||
parseCJS = parse; | ||
} | ||
let contents; | ||
@@ -213,4 +243,56 @@ try { | ||
} | ||
const willTransform = transform === true || typeof transform === "function" && transform(args.path); | ||
let cjsExports; | ||
if (parseCJS && willTransform) { | ||
let sourcemapIndex = contents.lastIndexOf("//# sourceMappingURL="); | ||
let sourcemap; | ||
if (sourcemapIndex !== -1) { | ||
sourcemap = contents.slice(sourcemapIndex); | ||
let sourcemapEnd = sourcemap.indexOf("\n"); | ||
if (sourcemapEnd !== -1 && sourcemap.slice(sourcemapEnd + 1).trimStart().length > 0) { | ||
sourcemap = void 0; | ||
} else { | ||
contents = contents.slice(0, sourcemapIndex); | ||
} | ||
} | ||
cjsExports = parseCJS(contents); | ||
let { behavior, exports, sideEffects } = typeof willTransform === "object" ? willTransform : {}; | ||
behavior ?? (behavior = (transformConfig == null ? void 0 : transformConfig.behavior) ?? "node"); | ||
exports = orderedUniq(cjsExports.exports.concat(exports ?? [])); | ||
sideEffects ?? (sideEffects = (transformConfig == null ? void 0 : transformConfig.sideEffects) ?? true); | ||
let exportDefault = behavior === "node" ? "export default exports;" : "export default exports.__esModule ? exports.default : exports;"; | ||
let exportsMap = exports.map((e) => [e, makeLegalIdentifier(e)]); | ||
if (exportsMap.some(([e]) => e === "default")) { | ||
if (behavior === "node") { | ||
exportsMap = exportsMap.filter(([e]) => e !== "default"); | ||
} else { | ||
exportDefault = ""; | ||
} | ||
} | ||
let reexports = cjsExports.reexports.map((e) => `export * from ${JSON.stringify(e)};`).join(""); | ||
let transformed; | ||
if (sideEffects === false) { | ||
transformed = [ | ||
reexports + "var mod, exports = /* @__PURE__ */ ((exports, module) => {" + contents, | ||
"return module.exports})((mod = { exports: {} }).exports, mod); " + exportDefault | ||
]; | ||
if (exportsMap.length > 0) { | ||
for (const [e, name] of exportsMap) { | ||
transformed.push(`var ${name} = /* @__PURE__ */ (() => exports[${JSON.stringify(e)}])();`); | ||
} | ||
transformed.push(`export { ${exportsMap.map(([e, name]) => e === name ? e : `${name} as ${JSON.stringify(e)}`).join(", ")} };`); | ||
} | ||
} else { | ||
transformed = [ | ||
reexports + "var exports = {}, module = { exports }; {" + contents, | ||
"}; exports = module.exports; " + exportDefault | ||
]; | ||
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(", ")} };`); | ||
} | ||
} | ||
contents = transformed.join("\n") + (sourcemap ? "\n" + sourcemap : ""); | ||
} | ||
function makeName(path) { | ||
let name = `__import_${path.replace(/-(\w)/g, (_, x) => x.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, "_")}`; | ||
let name = `__import_${makeLegalIdentifier(path)}`; | ||
if (contents.includes(name)) { | ||
@@ -258,3 +340,3 @@ let suffix = 2; | ||
} | ||
contents = [...imports, "exports;", contents].join(""); | ||
contents = [...imports, cjsExports ? "exports;" : "", contents].join(""); | ||
return { contents }; | ||
@@ -261,0 +343,0 @@ } |
169
index.ts
import type { Message, Plugin } from "esbuild"; | ||
import { promises } from "fs"; | ||
import { Lexer } from "./lexer"; | ||
import { cachedReduce, orderedUniq } from "./utils"; | ||
import { cachedReduce, makeLegalIdentifier, orderedUniq } from "./utils"; | ||
@@ -9,8 +9,86 @@ export interface CommonJSOptions { | ||
* The regexp passed to onLoad() to match commonjs files. | ||
* | ||
* @default /\.c?js$/ | ||
*/ | ||
filter?: RegExp; | ||
/** | ||
* _Experimental_: Transform commonjs to es modules. You have to install | ||
* `cjs-module-lexer` to let it work. | ||
* | ||
* When `true`, the plugin tries to wrap the commonjs module into: | ||
* | ||
* ```js | ||
* var exports = {}, module = { exports }; | ||
* { | ||
* // ... original content ... | ||
* } | ||
* exports = module.exports; | ||
* // the exported names are extracted by cjs-module-lexer | ||
* export default exports; | ||
* var { something, "a-b" as a_b } = exports; | ||
* export { something, a_b as "a-b" }; | ||
* ``` | ||
* | ||
* @default false | ||
*/ | ||
transform?: boolean | ((path: string) => TransformConfig | null | void); | ||
/** | ||
* _Experimental_: This options acts as a fallback of the `transform` option above. | ||
*/ | ||
transformConfig?: Pick<TransformConfig, "behavior" | "sideEffects">; | ||
} | ||
export function commonjs({ filter = /\.c?js$/ }: CommonJSOptions = {}): Plugin { | ||
export interface TransformConfig { | ||
/** | ||
* If `"babel"`, it will check if there be `exports.__esModule`, | ||
* then export `exports.default`. i.e. The wrapper code becomes: | ||
* | ||
* ```js | ||
* export default exports.__esModule ? exports.default : exports; | ||
* ``` | ||
* | ||
* @default "node" | ||
*/ | ||
behavior?: "babel" | "node"; | ||
/** | ||
* Also include these named exports if they aren't recognized automatically. | ||
* | ||
* @example ["something"] | ||
*/ | ||
exports?: string[]; | ||
/** | ||
* If `false`, slightly change the result to make it side-effect free. | ||
* But it doesn't actually remove many code. So you maybe not need this. | ||
* | ||
* ```js | ||
* var mod; | ||
* var exports = /+ @__PURE__ +/ ((exports, module) => { | ||
* // ... original content ... | ||
* return module.exports; | ||
* })((mod = { exports: {} }).exports, mod); | ||
* export default exports; | ||
* var a_b = /+ @__PURE__ +/ (() => exports['a-b'])(); | ||
* var something = /+ @__PURE__ +/ (() => exports.something)(); | ||
* export { a_b as "a-b", something }; | ||
* ``` | ||
* | ||
* Note: the `/+ @__PURE__ +/` above is actually `'/' + '* @__PURE__ *' + '/'`. | ||
*/ | ||
sideEffects?: boolean; | ||
} | ||
export function commonjs({ | ||
filter = /\.c?js$/, | ||
transform = false, | ||
transformConfig, | ||
}: CommonJSOptions = {}): Plugin { | ||
let init_cjs_module_lexer: Promise<typeof import("cjs-module-lexer")> | undefined; | ||
if (transform) { | ||
init_cjs_module_lexer = import("cjs-module-lexer"); | ||
} | ||
return { | ||
@@ -25,2 +103,9 @@ name: "commonjs", | ||
onLoad({ filter }, async args => { | ||
let parseCJS: typeof import("cjs-module-lexer").parse | undefined; | ||
if (init_cjs_module_lexer) { | ||
const { init, parse } = await init_cjs_module_lexer; | ||
await init(); | ||
parseCJS = parse; | ||
} | ||
let contents: string; | ||
@@ -33,6 +118,77 @@ try { | ||
const willTransform = transform === true || (typeof transform === "function" && transform(args.path)); | ||
let cjsExports: ReturnType<NonNullable<typeof parseCJS>> | undefined; | ||
if (parseCJS && willTransform) { | ||
// move sourcemap to the end of the transformed file | ||
let sourcemapIndex = contents.lastIndexOf("//# sourceMappingURL="); | ||
let sourcemap: string | undefined; | ||
if (sourcemapIndex !== -1) { | ||
sourcemap = contents.slice(sourcemapIndex); | ||
let sourcemapEnd = sourcemap.indexOf("\n"); | ||
if (sourcemapEnd !== -1 && sourcemap.slice(sourcemapEnd + 1).trimStart().length > 0) { | ||
// if there's code after sourcemap, it is invalid, don't do this. | ||
sourcemap = undefined; | ||
} else { | ||
contents = contents.slice(0, sourcemapIndex); | ||
} | ||
} | ||
// transform commonjs to es modules, easy mode | ||
cjsExports = parseCJS(contents); | ||
let { behavior, exports, sideEffects } = | ||
typeof willTransform === "object" ? willTransform : ({} as TransformConfig); | ||
behavior ??= transformConfig?.behavior ?? "node"; | ||
exports = orderedUniq(cjsExports.exports.concat(exports ?? [])); | ||
sideEffects ??= transformConfig?.sideEffects ?? true; | ||
let exportDefault = | ||
behavior === "node" | ||
? "export default exports;" | ||
: "export default exports.__esModule ? exports.default : exports;"; | ||
let exportsMap = exports.map(e => [e, makeLegalIdentifier(e)]); | ||
if (exportsMap.some(([e]) => e === "default")) { | ||
if (behavior === "node") { | ||
exportsMap = exportsMap.filter(([e]) => e !== "default"); | ||
} else { | ||
exportDefault = ""; | ||
} | ||
} | ||
let reexports = cjsExports.reexports.map(e => `export * from ${JSON.stringify(e)};`).join(""); | ||
let transformed: string[]; | ||
if (sideEffects === false) { | ||
transformed = [ | ||
// make sure we don't manipulate the first line so that sourcemap is fine | ||
reexports + "var mod, exports = /* @__PURE__ */ ((exports, module) => {" + contents, | ||
"return module.exports})((mod = { exports: {} }).exports, mod); " + exportDefault, | ||
]; | ||
if (exportsMap.length > 0) { | ||
for (const [e, name] of exportsMap) { | ||
transformed.push(`var ${name} = /* @__PURE__ */ (() => exports[${JSON.stringify(e)}])();`); | ||
} | ||
transformed.push( | ||
`export { ${exportsMap | ||
.map(([e, name]) => (e === name ? e : `${name} as ${JSON.stringify(e)}`)) | ||
.join(", ")} };` | ||
); | ||
} | ||
} else { | ||
transformed = [ | ||
reexports + "var exports = {}, module = { exports }; {" + contents, | ||
"}; exports = module.exports; " + exportDefault, | ||
]; | ||
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(", ")} };` | ||
); | ||
} | ||
} | ||
contents = transformed.join("\n") + (sourcemap ? "\n" + sourcemap : ""); | ||
} | ||
function makeName(path: string) { | ||
let name = `__import_${path | ||
.replace(/-(\w)/g, (_, x) => x.toUpperCase()) | ||
.replace(/[^$_a-zA-Z0-9]/g, "_")}`; | ||
let name = `__import_${makeLegalIdentifier(path)}`; | ||
@@ -90,3 +246,4 @@ if (contents.includes(name)) { | ||
contents = [...imports, "exports;", contents].join(""); | ||
// if we have transformed this module (i.e. having `cjsExports`), don't make the file commonjs | ||
contents = [...imports, cjsExports ? "exports;" : "", contents].join(""); | ||
@@ -93,0 +250,0 @@ return { contents }; |
{ | ||
"name": "@hyrious/esbuild-plugin-commonjs", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Bundle commonjs externals in es module in esbuild.", | ||
@@ -24,14 +24,24 @@ "main": "index.js", | ||
"peerDependencies": { | ||
"cjs-module-lexer": "*", | ||
"esbuild": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"cjs-module-lexer": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
"@hyrious/esbuild-dev": "^0.7.2", | ||
"@types/node": "^17.0.8", | ||
"esbuild": "^0.14.11" | ||
"@types/node": "^17.0.19", | ||
"cjs-module-lexer": "^1.2.2", | ||
"esbuild": "^0.14.23" | ||
}, | ||
"engines": { | ||
"node": "14" | ||
}, | ||
"scripts": { | ||
"build": "esbuild index.ts --bundle --external:esbuild --sourcemap --sources-content=false --outfile=index.js --platform=node --target=node12", | ||
"build": "esbuild index.ts --bundle --external:esbuild --external:cjs-module-lexer --sourcemap --sources-content=false --outfile=index.js --platform=node --target=node14", | ||
"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$/ });\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## 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## 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.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" | ||
} |
@@ -57,3 +57,3 @@ # @hyrious/esbuild-plugin-commonjs | ||
```js | ||
commonjs({ filter: /\.c?js$/ }); | ||
commonjs({ filter: /\.c?js$/, transform: false }); | ||
``` | ||
@@ -67,2 +67,40 @@ | ||
**transform** (default: `false`) | ||
Try to transform commonjs to es modules. This trick is done with [`cjs-module-lexer`](https://github.com/nodejs/cjs-module-lexer) | ||
to match the native (node) behavior as much as possible. Because this | ||
transformation may cause many bugs around the interop between cjs and esm, | ||
it can also accept a function to filter in the "safe to convert" modules by yourself. | ||
Type: | ||
```ts | ||
transform: boolean | ((path: string) => { | ||
behavior?: "node" | "babel", exports?: string[], sideEffects?: boolean | ||
} | null | void) | ||
``` | ||
By default, if you toggle `transform` to `true`, it will convert this code: | ||
```js | ||
exports.__esModule = true; | ||
exports.default = {}; | ||
exports.foo = 42; | ||
``` | ||
To this: | ||
<!-- prettier-ignore --> | ||
```js | ||
var exports = {}, module = { exports }; | ||
{ | ||
exports.__esModule = true; | ||
exports.default = {}; | ||
exports.foo = 42; | ||
} | ||
export default exports; | ||
var { foo } = exports; | ||
export { foo }; | ||
``` | ||
## This is not equal to [@rollup/plugin-commonjs][2]. | ||
@@ -81,2 +119,8 @@ | ||
## Changelog | ||
### 0.1.1 | ||
Add experimental option `transform` and `transformConfig`. | ||
## License | ||
@@ -83,0 +127,0 @@ |
15
utils.ts
@@ -16,1 +16,16 @@ export function orderedUniq<T>(array: T[]) { | ||
} | ||
// from @rollup/pluginutils | ||
const reservedWords = | ||
"break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public"; | ||
const builtin = | ||
"arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl"; | ||
const forbiddenIdentifiers = new Set(`${reservedWords} ${builtin}`.split(" ")); | ||
forbiddenIdentifiers.add(""); | ||
export const makeLegalIdentifier = function makeLegalIdentifier(str: string) { | ||
let identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, "_"); | ||
if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) { | ||
identifier = `_${identifier}`; | ||
} | ||
return identifier || "_"; | ||
}; |
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
43729
728
132
2
4