Skip to content

Commit

Permalink
test: add case
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Aug 7, 2024
1 parent c7af40c commit f4af1b6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class JsCompilation {
rebuildModule(moduleIdentifiers: Array<string>, f: (...args: any[]) => any): void
importModule(request: string, publicPath: JsFilename | undefined | null, baseUri: string | undefined | null, originalModule: string | undefined | null, originalModuleContext: string | undefined | null, callback: (...args: any[]) => any): void
get entries(): JsEntries
finalize(): void
}

export class JsEntries {
Expand Down
41 changes: 25 additions & 16 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,30 @@ impl JsCompilation {
pub fn entries(&'static mut self) -> JsEntries {
JsEntries::new(self.0)
}

#[napi]
pub fn finalize(&self, env: Env) {
// TODO: use napi_add_finalizer if N-API version >= 5
let compilation_id = self.0.id();

COMPILATION_INSTANCE_REFS.with(|refs| {
let mut refs = refs.borrow_mut();
let r = refs.remove(&compilation_id);
if let Some(mut r) = r {
let _ = r.unref(env.raw());
}
});

MODULE_INSTANCE_REFS.with(|refs| {
let mut refs_by_compilation_id = refs.borrow_mut();
let refs = refs_by_compilation_id.remove(&compilation_id);
if let Some(mut refs) = refs {
for (_, mut r) in refs.drain() {
let _ = r.unref(env.raw());
}
}
});
}
}

thread_local! {
Expand All @@ -582,9 +606,6 @@ impl JsCompilationWrapper {

impl ToNapiValue for JsCompilationWrapper {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut env_wrapper = Env::from_raw(env);
let compilation_id = val.0.id();

COMPILATION_INSTANCE_REFS.with(|refs| {
let mut refs = refs.borrow_mut();
match refs.entry(val.0.id()) {
Expand All @@ -593,19 +614,7 @@ impl ToNapiValue for JsCompilationWrapper {
ToNapiValue::to_napi_value(env, r)
}
std::collections::hash_map::Entry::Vacant(entry) => {
// TODO: use napi_add_finalizer if N-API version >= 5
let _ = env_wrapper.add_env_cleanup_hook(env, move |env| {
MODULE_INSTANCE_REFS.with(|refs| {
let mut refs_by_compilation_id = refs.borrow_mut();
let refs = refs_by_compilation_id.remove(&compilation_id);
if let Some(mut refs) = refs {
for (_, mut r) in refs.drain() {
let _ = r.unref(env);
}
}
});
});

let env_wrapper = Env::from_raw(env);
let instance = JsCompilation(val.0).into_instance(env_wrapper)?;
let napi_value = ToNapiValue::to_napi_value(env, instance)?;
let r = Ref::new(env, napi_value, 1)?;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dev": "tsc -b -w",
"test": "sh -c 'run-s \"test:* -- $*\"' sh",
"testu": "sh -c 'run-s \"test:* -u -- $*\"' sh",
"test:base": "cross-env NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.js --passWithNoTests",
"test:base": "RUST_BACKTRACE=1 cross-env NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.js --passWithNoTests Config.test.js",
"test:hot": "cross-env RSPACK_HOT_TEST=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.hot.js --passWithNoTests",
"test:diff": "cross-env RSPACK_DIFF=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.diff.js --passWithNoTests",
"api-extractor": "api-extractor run --verbose",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: "./index.js",
target: "node",
output: {
filename: "[name].js"
},
optimization: {
splitChunks: {
cacheGroups: {
common: {
test(module) {
expect(module.size()).toBe(5);
return true;
}
}
}
}
}

};
4 changes: 4 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ export class Compilation {
// @internal
__internal__setAssetSource(filename: string, source: Source): void;
// @internal
__internal_finalize(): void;
// @internal
__internal_getInner(): binding.JsCompilation;
get assets(): Record<string, Source>;
// (undocumented)
Expand Down Expand Up @@ -6075,6 +6077,8 @@ export class Module {
// (undocumented)
resource?: Readonly<string>;
// (undocumented)
size(type?: string): number;
// (undocumented)
type: string;
// (undocumented)
userRequest?: Readonly<string>;
Expand Down
9 changes: 9 additions & 0 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,15 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return this.#inner;
}

/**
* Note: This is not a webpack public API, maybe removed in future.
*
* @internal
*/
__internal_finalize() {
return this.#inner.finalize();
}

seal() {}
unseal() {}

Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ class Compiler {
this.#compilation!.startTime = startTime;
this.#compilation!.endTime = Date.now();
this.hooks.afterCompile.callAsync(this.#compilation!, err => {
this.#compilation!.__internal_finalize();
if (err) {
return callback(err);
}
Expand Down

0 comments on commit f4af1b6

Please sign in to comment.