Skip to content

feat: support regex flags and JavaScript lookaheads in extractComments condition#12990

Merged
CPunisher merged 5 commits intomainfrom
copilot/fix-swcjsminimizer-regex-support
Mar 6, 2026
Merged

feat: support regex flags and JavaScript lookaheads in extractComments condition#12990
CPunisher merged 5 commits intomainfrom
copilot/fix-swcjsminimizer-regex-support

Conversation

Copy link
Contributor

Copilot AI commented Feb 9, 2026

SwcJsMinimizerRspackPlugin's extractComments.condition was dropping regex flags and failing on JavaScript regex features like lookaheads, breaking patterns that worked in terser-webpack-plugin.

Changes

  • Pass regex flags through the binding layer

    • Add conditionFlags field to RawExtractComments
    • Extract both source and flags from JavaScript RegExp objects
  • Replace Rust regex with JavaScript-compatible engine

    • Use rspack_regex::RspackRegex (wraps regress) instead of regex::Regex
    • Supports lookaheads/lookbehinds, backreferences, and all JS regex semantics

Example

Patterns like the following now work correctly:

new SwcJsMinimizerRspackPlugin({
  extractComments: {
    // Negative lookahead + case-insensitive flag
    condition: /^\**!(?! *(SuppressStringValidation|StartNoStringValidationRegion))/i
  }
})

Extracts /*! important */ but skips /*! SuppressStringValidation */.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: The condition in the SwcJsMinimizerRspackPlugin has limited support for regular expressions.</issue_title>
<issue_description>### System Info

System:
OS: Windows 11 10.0.22631
CPU: (20) x64 12th Gen Intel(R) Core(TM) i9-12900H
Memory: 15.74 GB / 63.68 GB
Binaries:
Node: 24.13.0 - C:\Program Files\nodejs\node.EXE
Browsers:
Chrome: 144.0.7559.97
Edge: Chromium (140.0.3485.66)
Firefox: 147.0.1 - C:\Program Files\Mozilla Firefox\firefox.exe
Internet Explorer: 11.0.22621.3527
npmPackages:
@rspack/cli: ^1.7.4 => 1.7.5
@rspack/core: ^1.7.4 => 1.7.5

Details

This is the regular expression I wrote, which previously worked in the terser-webpack-plugin.

/^\**!(?! *(SuppressStringValidation|StartNoStringValidationRegion|EndNoStringValidationRegion))/i

I suspect that Rust's support for regular expressions isn't as extensive as JavaScript's.

Reproduce link

No response

Reproduce Steps

Add configuration:

			minimizer: [
				new SwcJsMinimizerRspackPlugin({
					extractComments: {
						condition: /^\**!(?! *(SuppressStringValidation|StartNoStringValidationRegion|EndNoStringValidationRegion))/i,
					},
				}),
			],

Then run rspack build</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 9, 2026 05:45
Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>
Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>
Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix limited regex support in SwcJsMinimizerRspackPlugin Support regex flags and JavaScript lookaheads in extractComments condition Feb 9, 2026
Copilot AI requested a review from hardfist February 9, 2026 06:06
@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

📦 Binary Size-limit

Comparing e80913f to chore(deps): update patch crates (#13185) by renovate[bot]

❌ Size increased by 3.38KB from 48.98MB to 48.99MB (⬆️0.01%)

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 4 projects with changes.

📊 Quick Summary
Project Total Size Change
react-10k 5.7 MB +2.0 B (0.0%)
react-1k 826.2 KB +2.0 B (0.0%)
react-5k 2.7 MB +2.0 B (0.0%)
rome 984.2 KB 0
ui-components 2.3 MB +60.0 B (0.0%)
📋 Detailed Reports (Click to expand)

📁 react-10k

Path: ../build-tools-performance/cases/react-10k/dist/rsdoctor-data.json

📌 Baseline Commit: a7cbf0e886 | PR: #13185

Metric Current Baseline Change
📊 Total Size 5.7 MB 5.7 MB +2.0 B (0.0%)
📄 JavaScript 5.7 MB 5.7 MB +2.0 B (0.0%)
🎨 CSS 21.0 B 21.0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 0 B 0 B 0

📦 Download Diff Report: react-10k Bundle Diff

📁 react-1k

Path: ../build-tools-performance/cases/react-1k/dist/rsdoctor-data.json

📌 Baseline Commit: a7cbf0e886 | PR: #13185

Metric Current Baseline Change
📊 Total Size 826.2 KB 826.2 KB +2.0 B (0.0%)
📄 JavaScript 826.2 KB 826.2 KB +2.0 B (0.0%)
🎨 CSS 0 B 0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 0 B 0 B 0

📦 Download Diff Report: react-1k Bundle Diff

📁 react-5k

Path: ../build-tools-performance/cases/react-5k/dist/rsdoctor-data.json

📌 Baseline Commit: a7cbf0e886 | PR: #13185

Metric Current Baseline Change
📊 Total Size 2.7 MB 2.7 MB +2.0 B (0.0%)
📄 JavaScript 2.7 MB 2.7 MB +2.0 B (0.0%)
🎨 CSS 21.0 B 21.0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 0 B 0 B 0

📦 Download Diff Report: react-5k Bundle Diff

📁 ui-components

Path: ../build-tools-performance/cases/ui-components/dist/rsdoctor-data.json

📌 Baseline Commit: a7cbf0e886 | PR: #13185

Metric Current Baseline Change
📊 Total Size 2.3 MB 2.3 MB +60.0 B (0.0%)
📄 JavaScript 2.0 MB 2.0 MB +60.0 B (0.0%)
🎨 CSS 271.4 KB 271.4 KB 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 0 B 0 B 0

📦 Download Diff Report: ui-components Bundle Diff

Generated by Rsdoctor GitHub Action

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 9, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing copilot/fix-swcjsminimizer-regex-support (e80913f) with main (a7cbf0e)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@CPunisher CPunisher changed the title Support regex flags and JavaScript lookaheads in extractComments condition feat: support regex flags and JavaScript lookaheads in extractComments condition Mar 6, 2026
@github-actions github-actions bot added the release: feature release: feature related release(mr only) label Mar 6, 2026
@CPunisher CPunisher marked this pull request as ready for review March 6, 2026 05:10
Copilot AI review requested due to automatic review settings March 6, 2026 05:10
@CPunisher CPunisher merged commit f1db546 into main Mar 6, 2026
53 checks passed
@CPunisher CPunisher deleted the copilot/fix-swcjsminimizer-regex-support branch March 6, 2026 05:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes SwcJsMinimizerRspackPlugin’s extractComments.condition handling so that JavaScript RegExp flags are preserved and JavaScript regex features (e.g. lookaheads) work as expected (closer to terser-webpack-plugin behavior).

Changes:

  • Preserve RegExp.flags across the JS → binding → Rust pipeline via a new conditionFlags/condition_flags field.
  • Switch extractComments.condition matching in the SWC minimizer from Rust regex::Regex to rspack_regex::RspackRegex (regress-backed).
  • Add config-case tests covering lookaheads and case-(in)sensitive flag behavior.

Reviewed changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/rspack-test/configCases/plugins/minify-extract-comments-regex-flags/rspack.config.js Adds a regression test config using a lookahead + i flag regex condition.
tests/rspack-test/configCases/plugins/minify-extract-comments-regex-flags/index.js Verifies extracted license output matches expected include/exclude behavior.
tests/rspack-test/configCases/plugins/minify-extract-comments-case-sensitive/rspack.config.js Adds a case-sensitive regex condition test config.
tests/rspack-test/configCases/plugins/minify-extract-comments-case-sensitive/index.js Asserts only uppercase matches are extracted.
tests/rspack-test/configCases/plugins/minify-extract-comments-case-insensitive/rspack.config.js Adds a case-insensitive (i) regex condition test config.
tests/rspack-test/configCases/plugins/minify-extract-comments-case-insensitive/index.js Asserts both uppercase/lowercase matches are extracted with i.
packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts Extracts and forwards regex source + flags through to raw options.
crates/rspack_plugin_swc_js_minimizer/src/lib.rs Uses RspackRegex for extractComments matching and threads flags through hashing/options.
crates/rspack_plugin_swc_js_minimizer/Cargo.toml Adds rspack_regex dependency for JS-compatible regex evaluation.
crates/rspack_binding_api/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs Extends RawExtractComments to accept condition_flags and maps it into plugin options.
crates/node_binding/napi-binding.d.ts Updates binding typings to expose conditionFlags.
Cargo.lock Locks the added workspace dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +233 to +235
const conditionStr = (
condition?: ExtractCommentsCondition,
): { source: string; flags: string } => {
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conditionStr no longer returns a string (it now returns { source, flags }), so the name is misleading and makes the call sites harder to read. Consider renaming it to something like getConditionParts/normalizeCondition (or split into getConditionSource + getConditionFlags) to reflect what it returns.

Copilot uses AI. Check for mistakes.
Comment on lines +23 to +27
const mainFile = fs.readFileSync(
path.resolve(__dirname, "bundle0.js"),
"utf-8"
);

Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mainFile is read from disk but never used, which adds unnecessary I/O and can confuse future readers about what the assertion depends on. Remove the read (or use it in an assertion) to keep the test focused.

Suggested change
const mainFile = fs.readFileSync(
path.resolve(__dirname, "bundle0.js"),
"utf-8"
);

Copilot uses AI. Check for mistakes.
CPunisher added a commit that referenced this pull request Mar 23, 2026
…s condition (#12990)

* Initial plan

* Add regex flags support for extractComments condition

Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>

* Add tests for regex flags support in extractComments

Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>

* Address code review comments

Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>

* fix: cargo fmt

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>
Co-authored-by: CPunisher <1343316114@qq.com>
CPunisher added a commit that referenced this pull request Mar 23, 2026
…actComments condition (#13437)

feat: support regex flags and JavaScript lookaheads in extractComments condition (#12990)

* Initial plan

* Add regex flags support for extractComments condition



* Add tests for regex flags support in extractComments



* Address code review comments



* fix: cargo fmt

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: hardfist <8898718+hardfist@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: feature release: feature related release(mr only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The condition in the SwcJsMinimizerRspackPlugin has limited support for regular expressions.

4 participants