Skip to content

Commit

Permalink
feat(rspack_plugin_swc_js_minimizer): add minify option
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Aug 16, 2024
1 parent 4505da7 commit da509a4
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,7 @@ export interface RawSwcJsMinimizerRspackPluginOptions {
exclude?: string | RegExp | (string | RegExp)[]
extractComments?: RawExtractComments
minimizerOptions: RawSwcJsMinimizerOptions
minify?: boolean
}

export interface RawToOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct RawSwcJsMinimizerRspackPluginOptions {
pub exclude: Option<RawAssetConditions>,
pub extract_comments: Option<RawExtractComments>,
pub minimizer_options: RawSwcJsMinimizerOptions,
pub minify: Option<bool>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -81,6 +82,7 @@ impl TryFrom<RawSwcJsMinimizerRspackPluginOptions> for PluginOptions {
test: value.test.map(into_asset_conditions),
include: value.include.map(into_asset_conditions),
exclude: value.exclude.map(into_asset_conditions),
minify: value.minify,
minimizer_options: MinimizerOptions {
compress,
mangle,
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static JAVASCRIPT_ASSET_REGEXP: LazyLock<Regex> =

#[derive(Debug, Hash)]
pub struct PluginOptions {
pub minify: Option<bool>,
pub test: Option<AssetConditions>,
pub include: Option<AssetConditions>,
pub exclude: Option<AssetConditions>,
Expand Down Expand Up @@ -199,6 +200,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
};

let js_minify_options = JsMinifyOptions {
minify: options.minify.unwrap_or(true),
compress: minimizer_options.compress.clone(),
mangle: minimizer_options.mangle.clone(),
format: minimizer_options.format.clone(),
Expand Down Expand Up @@ -321,6 +323,7 @@ impl Plugin for SwcJsMinimizerRspackPlugin {

#[derive(Debug, Clone, Default)]
pub struct JsMinifyOptions {
pub minify: bool,
pub compress: BoolOrDataConfig<TerserCompressorOptions>,
pub mangle: BoolOrDataConfig<MangleOptions>,
pub format: JsMinifyFormatOptions,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn minify(
names: source_map_names,
},
None,
true,
opts.minify,
Some(&comments),
&opts.format,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Object {
"main.js",
],
"filteredModules": undefined,
"hash": "333e59d94047c9b4ec6c",
"hash": "c0151c6dfca2168c65d7",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -176,7 +176,7 @@ Object {
"errorsCount": 0,
"filteredAssets": undefined,
"filteredModules": undefined,
"hash": "a156b269cd7b4340da9d",
"hash": "d46aa17c3e1115ff8d8a",
"modules": Array [
Object {
"assets": Array [],
Expand Down Expand Up @@ -324,7 +324,7 @@ Object {
"main.js",
],
"filteredModules": undefined,
"hash": "a2024ddd80c1dac51782",
"hash": "e9a1c09dae741d8eab71",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -691,7 +691,7 @@ Object {
"errorsCount": 0,
"filteredAssets": undefined,
"filteredModules": undefined,
"hash": "959ba20161293349edf3",
"hash": "1234d41312362d1e5f6e",
"modules": Array [
Object {
"assets": Array [],
Expand Down Expand Up @@ -1458,7 +1458,7 @@ Object {
"files": Array [
"main.js",
],
"hash": "333e59d94047c9b4ec6c",
"hash": "c0151c6dfca2168c65d7",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -1714,7 +1714,7 @@ Object {
"main.js",
],
"filteredModules": undefined,
"hash": "add6ca394a084131cdb7",
"hash": "b05777d51d49dfde7aa8",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -2063,7 +2063,7 @@ exports.c = require(\\"./c?c=3\\");
"errorsCount": 0,
"filteredAssets": undefined,
"filteredModules": undefined,
"hash": "0e205cc26142ad8eade1",
"hash": "b1ef105808f10ca0733b",
"modules": Array [
Object {
"assets": Array [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = process.env.a;
const b = process.env.b;

console.log(a + b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");
const path = require("path");

it("[minify-disable-minify]: should not minify code", () => {
const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8");

expect(content).toContain("let a = process.env.a;");
expect(content).toContain("console.log(a + process.env.b);");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const rspack = require("@rspack/core");
/**
* @type {import("@rspack/core").Configuration}
*/
module.exports = {
entry: {
a: "./a",
main: "./index"
},
output: {
filename: "[name].js"
},
optimization: {
minimize: true
},
plugins: [
new rspack.SwcJsMinimizerRspackPlugin({
minify: false,
minimizerOptions: {
mangle: false
}
})
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../../dist").TConfigCaseConfig} */
module.exports = {
findBundle: (i, options) => {
return ["main.js"];
}
};
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/tests/statsAPICases/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
entry ./fixtures/a
cjs self exports reference self [585] ./fixtures/a.js
Rspack compiled successfully (a156b269cd7b4340da9d)"
Rspack compiled successfully (d46aa17c3e1115ff8d8a)"
`);
}
};
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/tests/statsAPICases/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
"chunkB.js",
],
"filteredModules": undefined,
"hash": "9aa837bfaefd1fd4ec18",
"hash": "94e5f4b9ba950ceab14f",
"id": "250",
"idHints": Array [],
"initial": false,
Expand Down Expand Up @@ -144,7 +144,7 @@ module.exports = {
"main.js",
],
"filteredModules": undefined,
"hash": "0e471e878e0dbdf0ba5a",
"hash": "5e75c3a7bac911893513",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down
8 changes: 2 additions & 6 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15189,12 +15189,7 @@ type StringOrBufferCallback = (err: NodeJS.ErrnoException | null, data?: string
export const SwcJsMinimizerRspackPlugin: {
new (options?: SwcJsMinimizerRspackPluginOptions | undefined): {
name: BuiltinPluginName;
_args: [options?: SwcJsMinimizerRspackPluginOptions | undefined]; /**
* - `false`: removes all comments
* - `'some'`: preserves some comments
* - `'all'`: preserves all comments
* @default false
*/
_args: [options?: SwcJsMinimizerRspackPluginOptions | undefined];
affectedHooks: "done" | "make" | "compile" | "emit" | "afterEmit" | "invalid" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "environment" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined;
raw(compiler: Compiler_2): BuiltinPlugin;
apply(compiler: Compiler_2): void;
Expand All @@ -15207,6 +15202,7 @@ export type SwcJsMinimizerRspackPluginOptions = {
exclude?: AssetConditions;
include?: AssetConditions;
extractComments?: ExtractCommentsOptions | undefined;
minify?: boolean;
minimizerOptions?: {
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SwcJsMinimizerRspackPluginOptions = {
exclude?: AssetConditions;
include?: AssetConditions;
extractComments?: ExtractCommentsOptions | undefined;
minify?: boolean;
minimizerOptions?: {
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
Expand Down Expand Up @@ -288,6 +289,7 @@ export const SwcJsMinimizerRspackPlugin = create(
}

return {
minify: options?.minify,
test: options?.test,
include: options?.include,
exclude: options?.exclude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
exclude?: AssetConditions;
include?: AssetConditions;
extractComments?: boolean | RegExp;
minify?: boolean;
minimizerOptions: {
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
exclude?: AssetConditions;
include?: AssetConditions;
extractComments?: boolean | RegExp;
minify?: boolean;
minimizerOptions?: {
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
Expand Down

0 comments on commit da509a4

Please sign in to comment.