From e4fdc750b3d37f2b7baeb75058e29a7b4dd7fbb0 Mon Sep 17 00:00:00 2001 From: Hana Date: Fri, 26 Apr 2024 17:25:44 +0800 Subject: [PATCH] fix: swc minimizer extracted comments should be stable (#6380) --- .../src/minify.rs | 1 + .../ConfigTestCases.test.js.snap | 22 +++++++++++++++++++ .../minimizer-swc-extract-comments/index.js | 9 ++++++++ .../node_modules/bar/index.js | 5 +++++ .../node_modules/bar/package.json | 4 ++++ .../node_modules/baz/index.js | 5 +++++ .../node_modules/baz/package.json | 4 ++++ .../node_modules/foo/index.js | 5 +++++ .../node_modules/foo/package.json | 4 ++++ .../relative.js | 5 +++++ .../webpack.config.js | 18 +++++++++++++++ 11 files changed, 82 insertions(+) create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/package.json create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/package.json create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/package.json create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/relative.js create mode 100644 packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/webpack.config.js diff --git a/crates/rspack_plugin_swc_js_minimizer/src/minify.rs b/crates/rspack_plugin_swc_js_minimizer/src/minify.rs index a1b8d8acd31..965b207d3c9 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/minify.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/minify.rs @@ -272,6 +272,7 @@ pub fn minify( // if not matched comments, we don't need to emit .License.txt file if !extracted_comments.is_empty() { + extracted_comments.sort(); all_extract_comments .lock() .expect("all_extract_comments lock failed") diff --git a/packages/rspack-test-tools/tests/__snapshots__/ConfigTestCases.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/ConfigTestCases.test.js.snap index 47f1a966f9a..9f2a9d79f9b 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/ConfigTestCases.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/ConfigTestCases.test.js.snap @@ -313,6 +313,28 @@ div { " `; +exports[`ConfigTestCases optimization minimizer-swc-extract-comments exported tests should keep the extracted license file stable 1`] = ` +"/** + * bar + * @license MIT + */ + +/** + * baz + * @license MIT + */ + +/** + * foo + * @license MIT + */ + +/** + * relative + * @license MIT + */" +`; + exports[`ConfigTestCases plugins chunk-modules exported tests chunk-modules 1`] = ` Object { "515": Object { diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/index.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/index.js new file mode 100644 index 00000000000..a2af458c6fd --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/index.js @@ -0,0 +1,9 @@ +const fs = require("fs") +const path = require("path") +it("should keep the extracted license file stable", () => { + require("foo") + require("bar") + require("baz") + require("./relative") + expect(fs.readFileSync(path.join(__dirname, "bundle0.js.LICENSE.txt"), "utf8")).toMatchSnapshot() +}) diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/index.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/index.js new file mode 100644 index 00000000000..4c13e81a647 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/index.js @@ -0,0 +1,5 @@ +/** + * bar + * @license MIT + */ +module.exports = "bar" diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/package.json b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/package.json new file mode 100644 index 00000000000..16cd983ea1c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/bar/package.json @@ -0,0 +1,4 @@ +{ + "name": "bar", + "main": "index.js" +} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/index.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/index.js new file mode 100644 index 00000000000..d3fb6566c40 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/index.js @@ -0,0 +1,5 @@ +/** + * baz + * @license MIT + */ +module.exports = "baz" diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/package.json b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/package.json new file mode 100644 index 00000000000..c3214552cda --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/baz/package.json @@ -0,0 +1,4 @@ +{ + "name": "baz", + "main": "index.js" +} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/index.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/index.js new file mode 100644 index 00000000000..8c5a8233d15 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/index.js @@ -0,0 +1,5 @@ +/** + * foo + * @license MIT + */ +module.exports = "foo" diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/package.json b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/package.json new file mode 100644 index 00000000000..8f22e5e0420 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/node_modules/foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo", + "main": "index.js" +} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/relative.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/relative.js new file mode 100644 index 00000000000..2d5b48b66e5 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/relative.js @@ -0,0 +1,5 @@ +/** + * relative + * @license MIT + */ +module.exports = "relative" diff --git a/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/webpack.config.js b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/webpack.config.js new file mode 100644 index 00000000000..554840ce9a1 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/optimization/minimizer-swc-extract-comments/webpack.config.js @@ -0,0 +1,18 @@ +const { SwcJsMinimizerRspackPlugin, SwcCssMinimizerRspackPlugin } = require("@rspack/core") + +/** + * @type {import("@rspack/core").Configuration} + */ +module.exports = { + optimization: { + minimize: true, + minimizer: [ + new SwcJsMinimizerRspackPlugin({ + extractComments: {}, + format: { + comments: "all" + } + }), + ] + } +}