Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support invalidate persistent cache using config.mode and config.name #8920

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ export interface RawOptimizationOptions {
}

export interface RawOptions {
name?: string
mode?: undefined | 'production' | 'development' | 'none'
context: string
output: RawOutputOptions
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/raw_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use crate::raw_resolve::*;
#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawOptions {
pub name: Option<String>,
#[napi(ts_type = "undefined | 'production' | 'development' | 'none'")]
pub mode: Option<RawMode>,
pub context: String,
Expand Down Expand Up @@ -80,6 +81,7 @@ impl TryFrom<RawOptions> for CompilerOptions {
let node = value.node.map(|n| n.into());

Ok(CompilerOptions {
name: value.name,
context,
mode,
module,
Expand Down
10 changes: 8 additions & 2 deletions crates/rspack_core/src/cache/persistent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod occasion;
pub mod snapshot;
pub mod storage;
mod version;
use std::{path::PathBuf, sync::Arc};
use std::{hash::Hash, path::PathBuf, sync::Arc};

pub use cacheable_context::{CacheableContext, FromContext};
use occasion::MakeOccasion;
Expand Down Expand Up @@ -49,7 +49,13 @@ impl PersistentCache {
let version = version::get_version(
input_filesystem.clone(),
&option.build_dependencies,
vec![compiler_path, &option.version, rspack_version!()],
|hasher| {
compiler_path.hash(hasher);
option.version.hash(hasher);
rspack_version!().hash(hasher);
compiler_options.name.hash(hasher);
compiler_options.mode.hash(hasher);
},
);
let storage = create_storage(option.storage.clone(), version, intermediate_filesystem);
let context = Arc::new(CacheableContext {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/cache/persistent/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rspack_paths::AssertUtf8;
pub fn get_version(
fs: Arc<dyn ReadableFileSystem>,
dependencies: &Vec<PathBuf>,
salt: Vec<&str>,
add_salt: impl FnOnce(&mut DefaultHasher),
) -> String {
let mut hasher = DefaultHasher::new();
for dep in dependencies {
Expand All @@ -24,6 +24,6 @@ pub fn get_version(
.unwrap_or_else(|_| panic!("Failed to read buildDependency({path}) content."));
bytes.hash(&mut hasher);
}
salt.hash(&mut hasher);
add_salt(&mut hasher);
hex::encode(hasher.finish().to_ne_bytes())
}
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/compiler_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{

#[derive(Debug)]
pub struct CompilerOptions {
pub name: Option<String>,
pub context: Context,
pub output: OutputOptions,
pub mode: Mode,
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/options/compiler_options_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro_rules! f {

#[derive(Debug, Default)]
pub struct CompilerOptionsBuilder {
name: Option<String>,
target: Option<Target>,
entry: IndexMap<String, EntryDescription>,
context: Option<Context>,
Expand All @@ -50,6 +51,11 @@ impl CompilerOptions {
}

impl CompilerOptionsBuilder {
pub fn name(&mut self, name: String) -> &mut Self {
self.name = Some(name);
self
}

pub fn target(&mut self, target: Target) -> &mut Self {
self.target = Some(target);
self
Expand Down Expand Up @@ -108,6 +114,7 @@ impl CompilerOptionsBuilder {
}

pub fn build(&mut self) -> CompilerOptions {
let name = self.name.take();
let context = self.context.take().unwrap_or_else(|| {
std::env::current_dir()
.expect("`current_dir` should be available")
Expand Down Expand Up @@ -160,6 +167,7 @@ impl CompilerOptionsBuilder {
);

CompilerOptions {
name,
context,
output,
mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/options/mode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
pub enum Mode {
Development,
Production,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default 1;
---
export default 2;
---
export default 3;
---
export default 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import value from "./file";

it("should invalidation using config.mode work", async () => {
if (COMPILER_INDEX == 0) {
expect(value).toBe(1);
await NEXT_HMR();
expect(value).toBe(2);
await NEXT_START();
}
if (COMPILER_INDEX == 1) {
expect(value).toBe(3);
await NEXT_HMR();
expect(value).toBe(4);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require("path");

let index = 1;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
experiments: {
cache: {
type: "persistent",
snapshot: {
immutablePaths: [path.join(__dirname, "./file.js")]
}
}
},
plugins: [
{
apply(compiler) {
compiler.hooks.beforeCompile.tap("Test Plugin", function () {
if (index === 1) {
compiler.options.mode = "development";
} else {
compiler.options.mode = "production";
}
index++;
});
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default 1;
---
export default 2;
---
export default 3;
---
export default 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import value from "./file";

it("should invalidation using config.name work", async () => {
if (COMPILER_INDEX == 0) {
expect(value).toBe(1);
await NEXT_HMR();
expect(value).toBe(2);
await NEXT_START();
}
if (COMPILER_INDEX == 1) {
expect(value).toBe(3);
await NEXT_HMR();
expect(value).toBe(4);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require("path");

let index = 1;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
experiments: {
cache: {
type: "persistent",
snapshot: {
immutablePaths: [path.join(__dirname, "./file.js")]
}
}
},
plugins: [
{
apply(compiler) {
compiler.hooks.beforeCompile.tap("Test Plugin", function () {
compiler.options.name = String(index);
index++;
});
}
}
]
};
1 change: 1 addition & 0 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const getRawOptions = (
const mode = options.mode;
const experiments = options.experiments as Required<ExperimentsNormalized>;
return {
name: options.name,
mode,
context: options.context!,
output: options.output as Required<OutputNormalized>,
Expand Down
Loading