8000 Regex support for `format.comments` of minifier · Issue #9674 · swc-project/swc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Regex support for format.comments of minifier #9674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DynamiteAndy opened this issue Oct 23, 2024 · 7 comments · Fixed by #10571
Closed

Regex support for format.comments of minifier #9674

DynamiteAndy opened this issue Oct 23, 2024 · 7 comments · Fixed by #10571
Assignees
Milestone

Comments

@DynamiteAndy
Copy link

Describe the feature

The current implementation of the minimizers format.comments does not allow regex to be used like https://webpack.js.org/plugins/terser-webpack-plugin/#preserve-comments

new TerserPlugin({
        terserOptions: {
          format: {
            comments: /@.* [\w|\d].*|==\/?UserScript==/i
          }
        }
      })

it instead requires either comments?: false | 'some' | 'all';, would like to propose supporting a regex so that certain comments will be left untouched

Babel plugin or link to the feature description

https://webpack.js.org/plugins/terser-webpack-plugin/#preserve-comments

Additional context

I raised this originally in the rspack repo but was told it can be sent here as well.

@kdy1 kdy1 added this to the Planned milestone Oct 24, 2024
@lukpsaxo
Copy link

I've been tracking down why our bundle size increased by 10kb and the issue is we have a babel plugin that adds pure comments and then because of this: #9255

all of those comments now stay in the minified file.

I would love at least to be able to configure annotations to not be kept.

@kdy1
Copy link
Member
kdy1 commented Nov 18, 2024

@lukpsaxo It's supported. https://swc.rs/docs/configuration/minification#jscminifyformat
The unsupported thing is regex or function

@kdy1 kdy1 changed the title Alignment with terser-webpack-plugin for format.comments Regex support for format.comments Nov 18, 2024
@kdy1 kdy1 changed the title Regex support for format.comments Regex support for format.comments of minifier Nov 18, 2024
@lukpsaxo
Copy link
lukpsaxo commented Nov 18, 2024

@kdy1 I want to keep license comments and not keep pure annotations... I think that is not covered - at least the docs say only "some" and the code-change introducing the keeping of pure annotations didn't look like it was behind its own option (https://github.com/swc-project/swc/pull/9449/files#diff-1955969d1960346434767b5a39f0310edcab0f857933079e6048a50e67b65e38)

@kdy1 kdy1 self-assigned this Nov 19, 2024
@abettadapur
Copy link
abettadapur commented Nov 19, 2024

+1 to this
We have a very large project, and are blocked on upgrading SWC past 1.7.12 because the PURE comments adds multiple hundreds of KBs to our bundles

I don't need full regex support, but it would be great to preserve license comments and strip the PURE comments in the minifier, as they provide no value at runtime

8000

@kdy1
Copy link
Member
kdy1 commented Dec 2, 2024

@lukpsaxo @abettadapur #9775 will add preserve_annotations to support removing PURE comments without stripping license comments

@kdy1 kdy1 modified the milestones: Planned, Planned (Low priority) Dec 2, 2024
@kdy1 kdy1 removed their assignment Dec 2, 2024
kdy1 added a commit that referenced this issue Dec 2, 2024
**Description:**

 - `jsc.output.preserveAnnotations` (`transform()`/`.swcrc`)
 - `format.preserve_annotations` (`minify()`)

are added to support stripping out PURE comments while preserving license comments.

**Related issue:**

 - #9674
@kdy1 kdy1 modified the milestones: PR will be accepted, Planned May 13, 2025
8000
@CPunisher CPunisher self-assigned this Jun 4, 2025
@CPunisher
Copy link
Member

It's not hard to implement this. But the problem is how to write regex in swcrc json. Currently the type of the comments is undefined | bool | 'some' | 'all'. If we use string, then how to write the type. undefined | bool | 'some' | 'all' | string will be merged into undefined | bool | string

@kdy1
Copy link
Member
kdy1 commented Jun 4, 2025

I think we need to use { regex: string } as the type

kdy1 pushed a commit that referenced this issue Jun 6, 2025
**Description:**

Use [regress](https://github.com/ridiculousfish/regress) because it is compatible with EcmaScript Syntax. So the users can easily migrate from `terser`.

I will also update the docs.

**Related issue:**

 - Closes #9674
@kdy1 kdy1 modified the milestones: Planned, v1.12.0 Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

8 participants
@abettadapur @CPunisher @kdy1 @lukpsaxo @DynamiteAndy and others
0