diff --git a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs index f48729a759d..bdc0ccefd3f 100644 --- a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs @@ -8,8 +8,7 @@ use rspack_util::ext::DynHash; #[cacheable] #[derive(Debug, Clone)] pub struct ModuleArgumentDependency { - #[cacheable(with=Skip)] - id: Option<&'static str>, + id: Option, range: DependencyRange, #[cacheable(with=Skip)] source_map: Option, @@ -17,7 +16,7 @@ pub struct ModuleArgumentDependency { impl ModuleArgumentDependency { pub fn new( - id: Option<&'static str>, + id: Option, range: DependencyRange, source_map: Option, ) -> Self { @@ -55,7 +54,7 @@ impl DependencyTemplate for ModuleArgumentDependency { .expect("should have mgm") .get_module_argument(); - let content = if let Some(id) = self.id { + let content = if let Some(id) = &self.id { format!("{module_argument}.{id}") } else { format!("{module_argument}") diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs index bc2a4323f7f..0412f57b7cf 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs @@ -401,7 +401,7 @@ impl JavascriptParserPlugin for APIPlugin { parser .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( - Some("id"), + Some("id".into()), expr.span().into(), Some(parser.source_map.clone()), ))); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs index 76067b6c5af..5c417334feb 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs @@ -44,7 +44,7 @@ impl JavascriptParser<'_> { self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( - Some("hot"), + Some("hot".into()), span.into(), Some(self.source_map.clone()), ))); @@ -59,7 +59,7 @@ impl JavascriptParser<'_> { self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( - Some("hot.accept"), + Some("hot.accept".into()), call_expr.callee.span().into(), Some(self.source_map.clone()), ))); @@ -95,7 +95,7 @@ impl JavascriptParser<'_> { self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( - Some("hot.decline"), + Some("hot.decline".into()), call_expr.callee.span().into(), Some(self.source_map.clone()), ))); diff --git a/packages/rspack-test-tools/etc/test-tools.api.md b/packages/rspack-test-tools/etc/test-tools.api.md index 9911a669357..2810d89b642 100644 --- a/packages/rspack-test-tools/etc/test-tools.api.md +++ b/packages/rspack-test-tools/etc/test-tools.api.md @@ -112,6 +112,8 @@ export class CacheProcessor extends BasicProcessor { // (undocumented) afterAll(context: ITestContext): Promise; // (undocumented) + build(context: ITestContext): Promise; + // (undocumented) protected _cacheOptions: ICacheProcessorOptions; // (undocumented) static defaultOptions(this: CacheProcessor, context: ITestContext): TCompilerOptions; diff --git a/packages/rspack-test-tools/src/processor/cache.ts b/packages/rspack-test-tools/src/processor/cache.ts index 99aba3f9a9c..eaca8677fee 100644 --- a/packages/rspack-test-tools/src/processor/cache.ts +++ b/packages/rspack-test-tools/src/processor/cache.ts @@ -1,5 +1,6 @@ import path from "node:path"; import { rspack } from "@rspack/core"; +import { removeSync } from "fs-extra"; import { TestHotUpdatePlugin } from "../helper/plugins"; import { @@ -37,6 +38,25 @@ export class CacheProcessor extends BasicProcessor { this.updateOptions = fakeUpdateLoaderOptions; } + async build(context: ITestContext): Promise { + // clear cache directory first time. + const experiments = + this.getCompiler(context).getOptions().experiments || {}; + let directory = ""; + if ( + "cache" in experiments && + typeof experiments.cache === "object" && + experiments.cache.type === "persistent" + ) { + directory = experiments.cache.storage?.directory || directory; + } + removeSync( + path.join(context.getSource(), directory || "node_modules/.cache") + ); + + await super.build(context); + } + async run(env: ITestEnv, context: ITestContext) { context.setValue( this._options.name, diff --git a/packages/rspack-test-tools/tests/Cache.test.js b/packages/rspack-test-tools/tests/Cache.test.js index 65290065ba4..f53f8437065 100644 --- a/packages/rspack-test-tools/tests/Cache.test.js +++ b/packages/rspack-test-tools/tests/Cache.test.js @@ -4,13 +4,9 @@ process.env.RSPACK_CONFIG_VALIDATE = "loose-silent"; const path = require("path"); const { describeByWalk, createCacheCase } = require("../dist"); -function v(name) { - return path.join(__dirname, `cache ${name}`); -} - // Run tests rspack-test-tools/tests/cacheCases in target async-node describeByWalk( - v("hot async-node"), + "cache", (name, src, dist) => { createCacheCase(name, src, dist, "async-node"); }, diff --git a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/file.js b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/file.js index ac2895fc5cf..1e5de9836a0 100644 --- a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/file.js +++ b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/file.js @@ -3,3 +3,5 @@ export default 1; export default 2; --- export default 3; +--- +export default 4; diff --git a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js index 857fb08c5cd..c2772f64885 100644 --- a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js +++ b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js @@ -9,5 +9,7 @@ it("should store and resume asset parser and generator states", async () => { } if (COMPILER_INDEX == 1) { expect(value).toBe(3); + await NEXT_HMR(); + expect(value).toBe(4); } }); diff --git a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/rspack.config.js b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/rspack.config.js index 55527387402..887e87fbd5f 100644 --- a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/rspack.config.js +++ b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/rspack.config.js @@ -1,4 +1,9 @@ /** @type {import("@rspack/core").Configuration} */ module.exports = { - context: __dirname + context: __dirname, + experiments: { + cache: { + type: "persistent" + } + } };