Skip to content

Commit

Permalink
fix(minimizer): resolve error in minimizing esm asset file (#6521)
Browse files Browse the repository at this point in the history
* fix: minimize js asset from a module package well

* test: issue 6513

* style: fix clippy error
  • Loading branch information
xc2 committed May 14, 2024
1 parent e2b13ff commit 06e98c9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
12 changes: 8 additions & 4 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,15 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
let input_source_map = original_source.map(&MapOptions::default());

let is_module = if let Some(module) = self.options.module {
module
Some(module)
} else if let Some(module) = original.info.javascript_module {
module
Some(module)
} else if filename.ends_with(".mjs") {
Some(true)
} else if filename.ends_with(".cjs") {
Some(false)
} else {
filename.ends_with(".mjs")
None
};

let js_minify_options = JsMinifyOptions {
Expand Down Expand Up @@ -362,7 +366,7 @@ pub struct JsMinifyOptions {
pub ecma: TerserEcmaVersion,
pub keep_class_names: bool,
pub keep_fn_names: bool,
pub module: bool,
pub module: Option<bool>,
pub safari10: bool,
pub toplevel: bool,
pub source_map: BoolOrDataConfig<TerserSourceMapKind>,
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_plugin_swc_js_minimizer/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn minify(
// top_level defaults to true if module is true

// https://github.com/swc-project/swc/issues/2254
if opts.module {
if opts.module.unwrap_or(false) {
if let Some(opts) = &mut min_opts.compress {
if opts.top_level.is_none() {
opts.top_level = Some(TopLevelOptions { functions: true });
Expand All @@ -174,7 +174,9 @@ pub fn minify(
decorators_before_export: true,
..Default::default()
}),
IsModule::Bool(opts.module),
opts
.module
.map_or_else(|| IsModule::Unknown, IsModule::Bool),
Some(&comments),
)
.map_err(|errs| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ div {
"
`;
exports[`config config/optimization/minimizer-esm-asset exported tests minimizing an asset file of esm type should success 1`] = `"console.log(import.meta.url);export const a=1;"`;
exports[`config config/optimization/minimizer-swc-extract-comments exported tests should keep the extracted license file stable 1`] = `
"/**
* bar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { fileURLToPath } from "node:url";
import { readFileSync } from "node:fs";

it("minimizing an asset file of esm type should success", () => {
const worker = new URL("./pkg/pkg.js", import.meta.url);
const minifiedContent = readFileSync(fileURLToPath(worker), "utf-8");
expect(minifiedContent).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(import.meta.url);

export const a = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @type {import("@rspack/core").Configuration}
*/
module.exports = {
optimization: {
minimize: true
}
}

2 comments on commit 06e98c9

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-05-14 e2b13ff) Current Change
10000_development-mode + exec 2.72 s ± 21 ms 2.72 s ± 29 ms -0.15 %
10000_development-mode_hmr + exec 708 ms ± 5.2 ms 710 ms ± 5.4 ms +0.31 %
10000_production-mode + exec 2.51 s ± 21 ms 2.51 s ± 28 ms +0.23 %
arco-pro_development-mode + exec 2.51 s ± 96 ms 2.51 s ± 80 ms +0.08 %
arco-pro_development-mode_hmr + exec 437 ms ± 0.88 ms 435 ms ± 2.8 ms -0.46 %
arco-pro_development-mode_hmr_intercept-plugin + exec 445 ms ± 2.3 ms 444 ms ± 1.1 ms -0.20 %
arco-pro_development-mode_intercept-plugin + exec 3.34 s ± 67 ms 3.31 s ± 57 ms -0.75 %
arco-pro_production-mode + exec 4.04 s ± 120 ms 4.07 s ± 67 ms +0.64 %
arco-pro_production-mode_intercept-plugin + exec 4.88 s ± 78 ms 4.89 s ± 50 ms +0.26 %
threejs_development-mode_10x + exec 2 s ± 11 ms 1.99 s ± 15 ms -0.25 %
threejs_development-mode_10x_hmr + exec 775 ms ± 2.8 ms 781 ms ± 1.9 ms +0.77 %
threejs_production-mode_10x + exec 5.21 s ± 22 ms 5.19 s ± 27 ms -0.26 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci ❌ failure
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ❌ failure
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

Please sign in to comment.