From 8d70aeb823da027e1e8f9e1ecaeba2a0ba1f5115 Mon Sep 17 00:00:00 2001 From: SoonIter Date: Sun, 28 Apr 2024 17:09:40 +0800 Subject: [PATCH] fix(config): snapshot config --- .../configCases/cache/issues-6381/a/a.js | 1 + .../configCases/cache/issues-6381/index.js | 5 ++++ .../cache/issues-6381/webpack.config.js | 11 +++++++ packages/rspack/src/config/defaults.ts | 30 ++++++++++++------- 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/cache/issues-6381/a/a.js create mode 100644 packages/rspack-test-tools/tests/configCases/cache/issues-6381/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/cache/issues-6381/webpack.config.js diff --git a/packages/rspack-test-tools/tests/configCases/cache/issues-6381/a/a.js b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/a/a.js new file mode 100644 index 00000000000..aef22247d75 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/a/a.js @@ -0,0 +1 @@ +export default 1; diff --git a/packages/rspack-test-tools/tests/configCases/cache/issues-6381/index.js b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/index.js new file mode 100644 index 00000000000..adb1795ef0f --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/index.js @@ -0,0 +1,5 @@ +const a = "a"; +import(`./a/${a}.js`); +it("should work when snapshot.module or snapshot.resolve only set { timestamp: true }", () => { + expect(require("./a/a").default).toBe(1); +}); diff --git a/packages/rspack-test-tools/tests/configCases/cache/issues-6381/webpack.config.js b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/webpack.config.js new file mode 100644 index 00000000000..70b8fda8f68 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/cache/issues-6381/webpack.config.js @@ -0,0 +1,11 @@ +module.exports = { + cache: true, + snapshot: { + module: { + timestamp: true, + }, + resolve: { + timestamp: true, + }, + }, +}; diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index e78ee5ad32d..c1a0d9ce80f 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -191,16 +191,26 @@ const applySnapshotDefaults = ( snapshot: SnapshotOptions, { production }: { production: boolean } ) => { - F(snapshot, "module", () => - production - ? { timestamp: true, hash: true } - : { timestamp: true, hash: false } - ); - F(snapshot, "resolve", () => - production - ? { timestamp: true, hash: true } - : { timestamp: true, hash: false } - ); + if (typeof snapshot.module === "object") { + D(snapshot.module, "timestamp", false); + D(snapshot.module, "hash", false); + } else { + F(snapshot, "module", () => + production + ? { timestamp: true, hash: true } + : { timestamp: true, hash: false } + ); + } + if (typeof snapshot.resolve === "object") { + D(snapshot.resolve, "timestamp", false); + D(snapshot.resolve, "hash", false); + } else { + F(snapshot, "resolve", () => + production + ? { timestamp: true, hash: true } + : { timestamp: true, hash: false } + ); + } }; const applyJavascriptParserOptionsDefaults = (