Skip to content
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

[Feature]: allow customize minify options #1681

Closed
fi3ework opened this issue Feb 28, 2024 · 6 comments · Fixed by #1730
Closed

[Feature]: allow customize minify options #1681

fi3ework opened this issue Feb 28, 2024 · 6 comments · Fixed by #1730
Assignees

Comments

@fi3ework
Copy link
Member

What problem does this feature solve?

Currently, the only option of minify is output.disableMinimize, which is not allowed to pass options of SwcJsMinimizerRspackPlugin and SwcCssMinimizerRspackPlugin to Rsbuild.

What does the proposed API look like?

The implementation is easy, I could come up with a PR after the proposed API is passed.

  /**
   * Whether to disable code minification in production build.
   * remove in v1.0.0
   */
  disableMinimize?: boolean;
  /**
   * Options for minimizing the output JavaScript and CSS files.
   *
   * Set to `false` to disable minification.
   * Set to `true` to enable minification with default options.
   * Set to an object to specify custom options.
   */
  minify?:
    | boolean
    | {
        /**
         * Rspack is using swc minimizer, and the only builtin minimizer
         * so we could pass the options directly.
         * Not naming it to `js` as we might need `jsMinifier` or `cssMinifier` field to specify the minimizer in the future.
         */
        jsOptions?: SwcJsMinimizerRspackPluginOptions;
        /**
         * Rspack is using swc to minimize CSS, which has not options for now.
         * https://github.com/swc-project/swc/blob/main/crates/swc_css_minifier/src/options.rs#L6
         * We could skip `cssOptions` field for now.
         */
      };
@chenjiahan
Copy link
Member

Yeah we can remove the disableMinimize in next minor version, since Rsbuild is still in v0.x

@chenjiahan
Copy link
Member

How about provide both js and css option (maybe html / svg / image /... in the future):

      minify?: {
        /**
         * Rspack is using swc minimizer, and the only builtin minimizer
         * so we could pass the options directly.
         * Not naming it to `js` as we might need `jsMinifier` or `cssMinifier` field to specify the minimizer in the future.
         */
        js?: boolean | SwcJsMinimizerRspackPluginOptions;
        /**
         * Rspack is using swc to minimize CSS, which has not options for now.
         * https://github.com/swc-project/swc/blob/main/crates/swc_css_minifier/src/options.rs#L6
         * We could skip `cssOptions` field for now.
         */
         css: boolean;
      };

@fi3ework
Copy link
Member Author

fi3ework commented Feb 28, 2024

I did considering use that at first, which is much more prettier. But as described

* Not naming it to `js` as we might need `jsMinifier` or `cssMinifier` field to specify the minimizer in the future.

I'm not sure whether we would provide an alternative builtin minimizer like Vite(https://vitejs.dev/config/build-options.html#build-minify), if so, there will an another field to specify the minimizer type or a more complex structure like js: ['terser', { /* options */}] or js: {type: 'terser', options} which is hard to distinguish from an plain option (or maybe a minimizer instance?).

If we're sticking to only one builtin minimizer for each kind of asset, we're good to use js/css.

@chenjiahan
Copy link
Member

Currently we have no plans to support multiple minimizer types, and Rsbuild now supports different minimizers through plugins, such as CSS Minimizer Plugin.

However, I cannot deny that Rsbuild may have multiple built-in minimizers in the future...

there will an another field to specify the minimizer type or a more complex structure like js: ['terser', { /* options */}]

This is a bit complex and not friendly for config merging.

Plain object can be better:

type Output = {
  minify?: {
    /**
     * Whether to enable JS minify.
     */
    js?: boolean;
    /**
     * Rspack is using swc minimizer, and the only builtin minimizer
     * so we could pass the options directly.
     */
    jsOptions?: SwcJsMinimizerRspackPluginOptions;
    /**
     * Whether to enable CSS minify.
     */
    css?: boolean;
    /**
     * Rspack is using swc to minimize CSS, which has not options for now.
     * https://github.com/swc-project/swc/blob/main/crates/swc_css_minifier/src/options.rs#L6
     * We could skip `cssOptions` field for now.
     */
  };
};

@fi3ework
Copy link
Member Author

LGTM, when we support alternative minimizer afterwards, we could set to { js: 'xxx', jsOptions: XXXMinimizeOptions }. minify can also be a boolean type Output = boolean | { /* snip */ } by the way. Guess we're good to go 🫡.

@chenjiahan
Copy link
Member

Great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants