Skip to content

Commit

Permalink
fix: alias sequence order & merge result (#9206)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Feb 10, 2025
1 parent 46514f0 commit a2132dc
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export declare class JsCompilation {
emitAsset(filename: string, source: JsCompatSource, assetInfo: JsAssetInfo): void
deleteAsset(filename: string): void
renameAsset(filename: string, newName: string): void
get entrypoints(): Record<string, JsChunkGroup>
get entrypoints(): JsChunkGroup[]
get chunkGroups(): JsChunkGroup[]
get hash(): string | null
dependencies(): JsDependencies
Expand Down
13 changes: 4 additions & 9 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,15 @@ impl JsCompilation {
Ok(())
}

#[napi(getter, ts_return_type = "Record<string, JsChunkGroup>")]
pub fn entrypoints(&self) -> Result<HashMap<&String, JsChunkGroupWrapper>> {
#[napi(getter, ts_return_type = "JsChunkGroup[]")]
pub fn entrypoints(&self) -> Result<Vec<JsChunkGroupWrapper>> {
let compilation = self.as_ref()?;

Ok(
compilation
.entrypoints()
.iter()
.map(|(n, _)| {
(
n,
JsChunkGroupWrapper::new(compilation.entrypoint_by_name(n).ukey, compilation),
)
})
.values()
.map(|ukey| JsChunkGroupWrapper::new(*ukey, compilation))
.collect(),
)
}
Expand Down
54 changes: 30 additions & 24 deletions crates/rspack_core/src/options/resolve/clever_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ fn parse_resolve(resolve: Resolve) -> ResolveWithEntry {

fn overwrite<T, F>(a: Option<T>, b: Option<T>, f: F) -> Option<T>
where
F: FnOnce(&T, T) -> T,
F: FnOnce(T, T) -> T,
{
match (a, b) {
(Some(a), Some(b)) => Some(f(&a, b)),
(Some(a), Some(b)) => Some(f(a, b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
Expand Down Expand Up @@ -268,7 +268,7 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve {
extensions,
second.extensions.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
prefer_relative: merge!(
prefer_relative,
Expand All @@ -292,25 +292,25 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve {
main_files,
second.main_files.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
main_fields: merge!(
main_fields,
second.main_fields.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
condition_names: merge!(
condition_names,
second.condition_names.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
modules: merge!(
modules,
second.modules.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
fully_specified: merge!(
fully_specified,
Expand All @@ -326,7 +326,7 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve {
description_files,
second.description_files.base.get_value_type(),
need_merge_base,
|a, b| normalize_string_array(a, b)
normalize_string_array
),
enforce_extension: merge!(
enforce_extension,
Expand Down Expand Up @@ -443,31 +443,37 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve {
}
}

fn normalize_string_array(a: &[String], b: Vec<String>) -> Vec<String> {
fn normalize_string_array(a: Vec<String>, b: Vec<String>) -> Vec<String> {
b.into_iter().fold(vec![], |mut acc, item| {
if item.eq("...") {
acc.append(&mut a.to_vec());
acc.append(&mut a.clone());
} else {
acc.push(item);
}
acc
})
}

fn extend_alias(a: &Alias, b: Alias) -> Alias {
let mut b = b;
// FIXME: I think this clone can be removed
b.extend(a.clone());
b.dedup();
b
fn extend_alias(mut a: Alias, b: Alias) -> Alias {
for (key, value) in b {
if let Some((_, v)) = a.iter_mut().find(|(k, _)| *k == key) {
*v = value;
} else {
a.push((key, value));
}
}
a
}

fn extend_extension_alias(a: &ExtensionAlias, b: ExtensionAlias) -> ExtensionAlias {
let mut b = b;
// FIXME: I think this clone can be removed
b.extend(a.clone());
b.dedup();
b
fn extend_extension_alias(mut a: ExtensionAlias, b: ExtensionAlias) -> ExtensionAlias {
for (key, value) in b {
if let Some((_, v)) = a.iter_mut().find(|(k, _)| *k == key) {
*v = value;
} else {
a.push((key, value));
}
}
a
}

#[cfg(test)]
Expand Down Expand Up @@ -636,8 +642,8 @@ mod test {
assert_eq!(
options.alias.expect("should be Ok"),
vec![
("c2".to_string(), vec![AliasMap::Ignore]),
("c".to_string(), vec![AliasMap::Ignore])
("c".to_string(), vec![AliasMap::Ignore]),
("c2".to_string(), vec![AliasMap::Ignore])
]
);
assert_eq!(options.condition_names.expect("should be Ok").len(), 3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Math.random();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Math.random();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Math.random();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const PLUGIN_NAME = "plugin";

class Plugin {
/**
* @param {import("@rspack/core").Compiler} compiler
*/
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
compilation.hooks.finishModules.tap(PLUGIN_NAME, () => {
expect(Array.from(compilation.entrypoints.keys())).toEqual([]);
});

compilation.hooks.afterProcessAssets.tap(PLUGIN_NAME, () => {
expect(Array.from(compilation.entrypoints.keys())).toEqual(["main", "foo", "bar"]);
})
});
}
}

/**@type {import("@rspack/core").Configuration}*/
module.exports = {
entry: {
main: {
import: "./index.js",
},
foo: {
import: "./foo.js",
asyncChunks: true
},
bar: {
import: "./bar.js",
asyncChunks: true
},
},
plugins: [new Plugin()]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("foo");
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
module: {
rules: [
{
resolve: {
alias: {
"foo": "./not-exist"
}
},
},
{
resolve: {
alias: {
"foo": "./exist"
}
},
},
]
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("foo/bar");
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
module: {
rules: [
{
resolve: {
alias: {
"foo/bar": "./exist"
}
},
},
{
resolve: {
alias: {
"foo": "./not-exist"
}
},
},
]
},
};
22 changes: 9 additions & 13 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import { createReadonlyMap } from "./util/createReadonlyMap";
import { createFakeCompilationDependencies } from "./util/fake";
import type { InputFileSystem } from "./util/fs";
import type Hash from "./util/hash";
import { memoizeValue } from "./util/memoize";
import { JsSource } from "./util/source";
export type { AssetInfo } from "./util/AssetInfo";

Expand Down Expand Up @@ -399,27 +398,24 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
* Get a map of all assets.
*/
get assets(): Record<string, Source> {
return memoizeValue(() => this.#createCachedAssets());
return this.#createCachedAssets();
}

/**
* Get a map of all entrypoints.
*/
get entrypoints(): ReadonlyMap<string, Entrypoint> {
return memoizeValue(
() =>
new Map(
Object.entries(this.#inner.entrypoints).map(([n, e]) => [
n,
Entrypoint.__from_binding(e)
])
)
return new Map(
this.#inner.entrypoints.map(binding => {
const entrypoint = Entrypoint.__from_binding(binding);
return [entrypoint.name!, entrypoint];
})
);
}

get chunkGroups(): ReadonlyArray<ChunkGroup> {
return memoizeValue(() =>
this.#inner.chunkGroups.map(binding => ChunkGroup.__from_binding(binding))
return this.#inner.chunkGroups.map(binding =>
ChunkGroup.__from_binding(binding)
);
}

Expand Down Expand Up @@ -456,7 +452,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
}

get chunks(): ReadonlySet<Chunk> {
return memoizeValue(() => new Set(this.__internal__getChunks()));
return new Set(this.__internal__getChunks());
}

/**
Expand Down

2 comments on commit a2132dc

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on a2132dc Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-02-10 09af1d8) Current Change
10000_big_production-mode_disable-minimize + exec 37.7 s ± 622 ms 38.8 s ± 996 ms +2.89 %
10000_development-mode + exec 1.87 s ± 37 ms 1.83 s ± 21 ms -2.44 %
10000_development-mode_hmr + exec 684 ms ± 18 ms 686 ms ± 12 ms +0.34 %
10000_production-mode + exec 2.31 s ± 63 ms 2.28 s ± 57 ms -1.24 %
10000_production-mode_persistent-cold + exec 2.45 s ± 74 ms 2.42 s ± 75 ms -1.22 %
10000_production-mode_persistent-hot + exec 1.64 s ± 26 ms 1.66 s ± 40 ms +1.50 %
arco-pro_development-mode + exec 1.72 s ± 90 ms 1.72 s ± 82 ms -0.03 %
arco-pro_development-mode_hmr + exec 389 ms ± 2.1 ms 387 ms ± 1.2 ms -0.38 %
arco-pro_production-mode + exec 3.62 s ± 212 ms 3.65 s ± 207 ms +0.73 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.67 s ± 262 ms 3.71 s ± 162 ms +0.98 %
arco-pro_production-mode_persistent-cold + exec 3.83 s ± 285 ms 3.74 s ± 110 ms -2.27 %
arco-pro_production-mode_persistent-hot + exec 2.36 s ± 60 ms 2.45 s ± 196 ms +3.88 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 197 ms 3.67 s ± 71 ms +0.92 %
large-dyn-imports_development-mode + exec 2.12 s ± 39 ms 2.11 s ± 79 ms -0.60 %
large-dyn-imports_production-mode + exec 2.14 s ± 20 ms 2.17 s ± 34 ms +1.19 %
threejs_development-mode_10x + exec 1.53 s ± 28 ms 1.57 s ± 17 ms +2.45 %
threejs_development-mode_10x_hmr + exec 783 ms ± 13 ms 785 ms ± 6.6 ms +0.35 %
threejs_production-mode_10x + exec 5.22 s ± 186 ms 5.28 s ± 348 ms +1.21 %
threejs_production-mode_10x_persistent-cold + exec 5.31 s ± 155 ms 5.33 s ± 383 ms +0.47 %
threejs_production-mode_10x_persistent-hot + exec 4.53 s ± 315 ms 4.55 s ± 339 ms +0.50 %
10000_big_production-mode_disable-minimize + rss memory 8773 MiB ± 74.2 MiB 8726 MiB ± 118 MiB -0.54 %
10000_development-mode + rss memory 653 MiB ± 15.5 MiB 652 MiB ± 30.9 MiB -0.12 %
10000_development-mode_hmr + rss memory 1264 MiB ± 261 MiB 1218 MiB ± 161 MiB -3.67 %
10000_production-mode + rss memory 623 MiB ± 16.1 MiB 634 MiB ± 14.8 MiB +1.82 %
10000_production-mode_persistent-cold + rss memory 743 MiB ± 22.3 MiB 742 MiB ± 21.6 MiB -0.19 %
10000_production-mode_persistent-hot + rss memory 714 MiB ± 18.8 MiB 729 MiB ± 31.1 MiB +1.97 %
arco-pro_development-mode + rss memory 561 MiB ± 21.6 MiB 561 MiB ± 15.9 MiB -0.00 %
arco-pro_development-mode_hmr + rss memory 638 MiB ± 51.3 MiB 625 MiB ± 71.3 MiB -1.95 %
arco-pro_production-mode + rss memory 735 MiB ± 26.9 MiB 696 MiB ± 30.4 MiB -5.31 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 741 MiB ± 13.1 MiB 717 MiB ± 29.5 MiB -3.18 %
arco-pro_production-mode_persistent-cold + rss memory 857 MiB ± 36.2 MiB 836 MiB ± 39.1 MiB -2.43 %
arco-pro_production-mode_persistent-hot + rss memory 730 MiB ± 6.4 MiB 691 MiB ± 15.7 MiB -5.30 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 737 MiB ± 60.8 MiB 710 MiB ± 27.4 MiB -3.72 %
large-dyn-imports_development-mode + rss memory 647 MiB ± 9.25 MiB 635 MiB ± 7.74 MiB -1.73 %
large-dyn-imports_production-mode + rss memory 530 MiB ± 6.21 MiB 522 MiB ± 6.37 MiB -1.66 %
threejs_development-mode_10x + rss memory 551 MiB ± 25.1 MiB 542 MiB ± 21.9 MiB -1.62 %
threejs_development-mode_10x_hmr + rss memory 1124 MiB ± 189 MiB 1145 MiB ± 84.3 MiB +1.85 %
threejs_production-mode_10x + rss memory 829 MiB ± 36.8 MiB 834 MiB ± 11.1 MiB +0.50 %
threejs_production-mode_10x_persistent-cold + rss memory 967 MiB ± 35.8 MiB 955 MiB ± 47.7 MiB -1.32 %
threejs_production-mode_10x_persistent-hot + rss memory 868 MiB ± 38.6 MiB 846 MiB ± 24.4 MiB -2.50 %

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on a2132dc Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.