From ef06039beb4a0f2c9cc23843a2d713bc8018696e Mon Sep 17 00:00:00 2001 From: CodewithEVILXD <126612139+CodewithEvilxd@users.noreply.github.com> Date: Thu, 9 Oct 2025 14:26:06 +0530 Subject: [PATCH] feat: implement Object.defineProperty support and optimize regex generation --- README.md | 2 +- .../CommonJsExportRequireDependency.js | 17 +++++++++++- lib/util/compileBooleanMatcher.js | 26 ++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fc5b1ec7db7..c665eac1765 100644 --- a/README.md +++ b/README.md @@ -649,7 +649,7 @@ Before we started using OpenCollective, donations were made anonymously. Now tha

(In chronological order)

-- [@google](https://github.com/google) for [Google Web Toolkit (GWT)](http://www.gwtproject.org/), which aims to compile Java to JavaScript. It features a similar [Code Splitting](http://www.gwtproject.org/doc/latest/DevGuideCodeSplitting.html) as webpack. +- [@google](https://github.com/google) for [Google Web Toolkit (GWT)](http://www.gwtproject.org/), which aims to compile Java to JavaScript. It features a similar [Code Splitting](http://www.gwtproject.org/doc/latest/DevGuideCodeSplitting.html) to webpack. - [@medikoo](https://github.com/medikoo) for [modules-webmake](https://github.com/medikoo/modules-webmake), which is a similar project. webpack was born because of the desire for code splitting for modules such as Webmake. Interestingly, the [Code Splitting issue is still open](https://github.com/medikoo/modules-webmake/issues/7) (thanks also to @Phoscur for the discussion). - [@substack](https://github.com/substack) for [browserify](https://browserify.org/), which is a similar project and source for many ideas. - [@jrburke](https://github.com/jrburke) for [require.js](https://requirejs.org/), which is a similar project and source for many ideas. diff --git a/lib/dependencies/CommonJsExportRequireDependency.js b/lib/dependencies/CommonJsExportRequireDependency.js index 96b361a57e0..1f7e81a6735 100644 --- a/lib/dependencies/CommonJsExportRequireDependency.js +++ b/lib/dependencies/CommonJsExportRequireDependency.js @@ -400,7 +400,22 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency ); return; case "Object.defineProperty": - throw new Error("TODO"); + if (!used) { + source.replace( + dep.range[0], + dep.range[1] - 1, + `/* unused reexport */ ${requireExpr}` + ); + return; + } + source.replace( + dep.range[0], + dep.range[1] - 1, + `Object.defineProperty(${base}${propertyAccess( + used.slice(0, -1) + )}, ${JSON.stringify(used[used.length - 1])}, { value: ${requireExpr} })` + ); + return; default: throw new Error("Unexpected type"); } diff --git a/lib/util/compileBooleanMatcher.js b/lib/util/compileBooleanMatcher.js index 4ca1b81ce79..b411caef750 100644 --- a/lib/util/compileBooleanMatcher.js +++ b/lib/util/compileBooleanMatcher.js @@ -147,8 +147,32 @@ const itemsToRegexp = (itemsArr) => { } } // special case for only single char items + // optimize by using character ranges: e.g., [1-4a] instead of [1234a] if (countOfSingleCharItems === itemsArr.length) { - return `[${quoteMeta(itemsArr.sort().join(""))}]`; + const sorted = itemsArr.sort(); + const ranges = []; + let start = sorted[0]; + let prev = sorted[0]; + for (let i = 1; i < sorted.length; i++) { + const current = sorted[i]; + if (current.charCodeAt(0) === prev.charCodeAt(0) + 1) { + prev = current; + } else { + if (start === prev) { + ranges.push(quoteMeta(start)); + } else { + ranges.push(`${quoteMeta(start)}-${quoteMeta(prev)}`); + } + start = current; + prev = current; + } + } + if (start === prev) { + ranges.push(quoteMeta(start)); + } else { + ranges.push(`${quoteMeta(start)}-${quoteMeta(prev)}`); + } + return `[${ranges.join("")}]`; } /** @type {Set} */ const items = new Set(itemsArr.sort());