Skip to content

Commit

Permalink
fix: namedChunks / emitAsset js api (#9185)
Browse files Browse the repository at this point in the history
* fix: namedChunks

* fix: emit_asset a absolute path

* chore: case for node_join

* fix: LibManifestPlugin should use intermediate_filesystem

* chore: more test case for node_join

* fix: should convert absolute filename into relative

* fix: clippy

* fix: error in windows

* refactor: node path trait impl

* fix: cargo clippy

* fix: node_join in Utf8Path
  • Loading branch information
SyMind authored Feb 11, 2025
1 parent decbbd7 commit be02514
Show file tree
Hide file tree
Showing 13 changed files with 1,114 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export declare class JsCompilation {
getOptimizationBailout(): Array<JsStatsOptimizationBailout>
getChunks(): JsChunk[]
getNamedChunkKeys(): Array<string>
getNamedChunk(name: string): JsChunk
getNamedChunk(name: string): JsChunk | null
getNamedChunkGroupKeys(): Array<string>
getNamedChunkGroup(name: string): JsChunkGroup
setAssetSource(name: string, source: JsCompatSource): void
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl JsCompilation {
Ok(compilation.named_chunks.keys().cloned().collect::<Vec<_>>())
}

#[napi(ts_return_type = "JsChunk")]
#[napi(ts_return_type = "JsChunk | null")]
pub fn get_named_chunk(&self, name: String) -> Result<Option<JsChunkWrapper>> {
let compilation = self.as_ref()?;

Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rspack_futures::FuturesResults;
use rspack_hook::define_hook;
use rspack_paths::{Utf8Path, Utf8PathBuf};
use rspack_sources::BoxSource;
use rspack_util::node_path::NodePath;
use rustc_hash::FxHashMap as HashMap;
use tracing::instrument;

Expand Down Expand Up @@ -358,7 +359,7 @@ impl Compiler {
) -> Result<()> {
if let Some(source) = asset.get_source() {
let (target_file, query) = filename.split_once('?').unwrap_or((filename, ""));
let file_path = output_path.join(target_file);
let file_path = output_path.node_join(target_file);
self
.output_filesystem
.create_dir_all(
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_dll/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rspack_collections = { workspace = true }
rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_hook = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_externals = { workspace = true }
rspack_util = { workspace = true }

Expand Down
31 changes: 13 additions & 18 deletions crates/rspack_plugin_dll/src/lib_manifest_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::collections::HashSet;
use std::sync::Arc;

use rspack_collections::DatabaseItem;
use rspack_core::{
rspack_sources::{BoxSource, RawStringSource},
ApplyContext, ChunkGraph, Compilation, CompilationAssets, CompilerEmit, CompilerOptions, Context,
EntryDependency, Filename, LibIdentOptions, PathData, Plugin, PluginContext, ProvidedExports,
SourceType,
ApplyContext, ChunkGraph, Compilation, CompilerEmit, CompilerOptions, Context, EntryDependency,
Filename, LibIdentOptions, PathData, Plugin, PluginContext, ProvidedExports, SourceType,
};
use rspack_error::{Error, Result};
use rspack_hook::{plugin, plugin_hook};
use rspack_paths::Utf8Path;
use rustc_hash::FxHashMap as HashMap;

use crate::{
Expand Down Expand Up @@ -56,11 +52,9 @@ impl Plugin for LibManifestPlugin {

#[plugin_hook(CompilerEmit for LibManifestPlugin)]
async fn emit(&self, compilation: &mut Compilation) -> Result<()> {
let mut use_paths: HashSet<String> = HashSet::new();

let chunk_graph = &compilation.chunk_graph;

let mut manifests: CompilationAssets = HashMap::default();
let mut manifests: HashMap<String, String> = HashMap::default();

let module_graph = compilation.get_module_graph();

Expand All @@ -84,12 +78,10 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> {
.chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)),
)?;

if use_paths.contains(&target_path) {
if manifests.contains_key(&target_path) {
return Err(Error::msg("each chunk must have a unique path"));
}

use_paths.insert(target_path.clone());

let name = self.options.name.as_ref().and_then(|name| {
compilation
.get_path(
Expand Down Expand Up @@ -175,13 +167,16 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> {
serde_json::to_string(&manifest).map_err(|e| Error::msg(format!("{e}")))?
};

let asset = Arc::new(RawStringSource::from(manifest_json)) as BoxSource;

manifests.insert(target_path, asset.into());
manifests.insert(target_path, manifest_json);
}

for (filename, asset) in manifests {
compilation.emit_asset(filename, asset);
let intermediate_filesystem = compilation.intermediate_filesystem.as_ref();
for (filename, json) in manifests {
let path = Utf8Path::new(&filename);
if let Some(dir) = path.parent() {
intermediate_filesystem.create_dir_all(dir).await?;
}
intermediate_filesystem.write(path, json.as_bytes()).await?;
}

Ok(())
Expand Down
69 changes: 48 additions & 21 deletions crates/rspack_plugin_html/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{path::PathBuf, sync::LazyLock};
use std::{
borrow::Cow,
path::{Path, PathBuf},
sync::LazyLock,
};

use cow_utils::CowUtils;
use rspack_core::{
Expand All @@ -8,6 +12,7 @@ use rspack_core::{
use rspack_error::{miette, Diagnostic, Result};
use rspack_hook::{plugin, plugin_hook};
use rspack_util::fx_hash::FxDashMap;
use sugar_path::SugarPath;
use swc_html::visit::VisitMutWith;

use crate::{
Expand Down Expand Up @@ -181,26 +186,48 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {

// TODO: parallel generate html
for filename in &config.filename {
let output_file_name = FilenameTemplate::from(
filename
.cow_replace("[templatehash]", "[contenthash]")
.into_owned(),
);

let (template_file_name, html) =
match generate_html(filename, &output_file_name, config, compilation, &hooks).await {
Ok(content) => {
compilation
.file_dependencies
.extend(content.2.into_iter().map(Into::into));
(content.0, content.1)
}
Err(err) => {
let error_msg = err.to_string();
compilation.push_diagnostic(Diagnostic::from(err));
("error.html".to_string(), create_error_html(&error_msg))
}
};
let filename = filename.cow_replace("[templatehash]", "[contenthash]");

// convert absolute filename into relative so that webpack can
// generate it at correct location
let filename = {
let filename_path = Path::new(filename.as_ref());
if filename_path.is_absolute() {
let output_path = &compilation.options.output.path;
Cow::from(
filename_path
.relative(output_path)
.to_string_lossy()
.to_string(),
)
} else {
filename
}
};

let output_file_name = FilenameTemplate::from(filename.to_string());

let (template_file_name, html) = match generate_html(
filename.as_ref(),
&output_file_name,
config,
compilation,
&hooks,
)
.await
{
Ok(content) => {
compilation
.file_dependencies
.extend(content.2.into_iter().map(Into::into));
(content.0, content.1)
}
Err(err) => {
let error_msg = err.to_string();
compilation.push_diagnostic(Diagnostic::from(err));
("error.html".to_string(), create_error_html(&error_msg))
}
};

let mut before_emit_data = hooks
.before_emit
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ itoa = { version = "1.0.14" }
regex = { workspace = true }
ropey = { workspace = true }
rspack_cacheable = { workspace = true }
rspack_paths = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod identifier;
pub mod infallible;
pub mod itoa;
pub mod location;
pub mod node_path;
pub mod number_hash;
pub mod queue;
pub mod size;
Expand Down
Loading

2 comments on commit be02514

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on be02514 Feb 11, 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

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on be02514 Feb 11, 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-11 9d82335) Current Change
10000_big_production-mode_disable-minimize + exec 37.7 s ± 637 ms 39 s ± 1.06 s +3.33 %
10000_development-mode + exec 1.88 s ± 89 ms 1.86 s ± 13 ms -0.88 %
10000_development-mode_hmr + exec 685 ms ± 7.4 ms 689 ms ± 29 ms +0.67 %
10000_production-mode + exec 2.28 s ± 51 ms 2.29 s ± 65 ms +0.69 %
10000_production-mode_persistent-cold + exec 2.47 s ± 72 ms 2.51 s ± 142 ms +1.54 %
10000_production-mode_persistent-hot + exec 1.65 s ± 61 ms 1.71 s ± 281 ms +3.41 %
arco-pro_development-mode + exec 1.78 s ± 145 ms 1.79 s ± 31 ms +0.83 %
arco-pro_development-mode_hmr + exec 389 ms ± 2.1 ms 387 ms ± 1.2 ms -0.48 %
arco-pro_production-mode + exec 3.59 s ± 217 ms 3.68 s ± 107 ms +2.42 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.72 s ± 229 ms 3.64 s ± 149 ms -2.10 %
arco-pro_production-mode_persistent-cold + exec 3.85 s ± 164 ms 3.79 s ± 160 ms -1.49 %
arco-pro_production-mode_persistent-hot + exec 2.36 s ± 99 ms 2.43 s ± 217 ms +3.13 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 69 ms 3.63 s ± 196 ms +0.15 %
large-dyn-imports_development-mode + exec 2.09 s ± 24 ms 2.17 s ± 62 ms +3.83 %
large-dyn-imports_production-mode + exec 2.15 s ± 71 ms 2.15 s ± 23 ms -0.15 %
threejs_development-mode_10x + exec 1.54 s ± 25 ms 1.54 s ± 33 ms -0.05 %
threejs_development-mode_10x_hmr + exec 779 ms ± 7 ms 788 ms ± 7.3 ms +1.21 %
threejs_production-mode_10x + exec 5.2 s ± 32 ms 5.24 s ± 260 ms +0.88 %
threejs_production-mode_10x_persistent-cold + exec 5.27 s ± 65 ms 5.27 s ± 69 ms 0.00 %
threejs_production-mode_10x_persistent-hot + exec 4.53 s ± 220 ms 4.48 s ± 48 ms -1.16 %
10000_big_production-mode_disable-minimize + rss memory 8706 MiB ± 32.4 MiB 8699 MiB ± 38.4 MiB -0.08 %
10000_development-mode + rss memory 649 MiB ± 12.6 MiB 657 MiB ± 19 MiB +1.32 %
10000_development-mode_hmr + rss memory 1344 MiB ± 112 MiB 1330 MiB ± 208 MiB -1.06 %
10000_production-mode + rss memory 621 MiB ± 21.8 MiB 653 MiB ± 30.2 MiB +5.17 %
10000_production-mode_persistent-cold + rss memory 746 MiB ± 24.7 MiB 752 MiB ± 16.4 MiB +0.69 %
10000_production-mode_persistent-hot + rss memory 717 MiB ± 23.3 MiB 747 MiB ± 14.3 MiB +4.20 %
arco-pro_development-mode + rss memory 569 MiB ± 17.5 MiB 570 MiB ± 27.4 MiB +0.20 %
arco-pro_development-mode_hmr + rss memory 643 MiB ± 49.1 MiB 658 MiB ± 52.3 MiB +2.43 %
arco-pro_production-mode + rss memory 714 MiB ± 28.8 MiB 730 MiB ± 26.2 MiB +2.23 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 732 MiB ± 22.4 MiB 734 MiB ± 25.6 MiB +0.28 %
arco-pro_production-mode_persistent-cold + rss memory 853 MiB ± 45.5 MiB 836 MiB ± 50.8 MiB -1.97 %
arco-pro_production-mode_persistent-hot + rss memory 708 MiB ± 27.8 MiB 731 MiB ± 28.4 MiB +3.32 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 727 MiB ± 48.2 MiB 727 MiB ± 39.9 MiB -0.06 %
large-dyn-imports_development-mode + rss memory 644 MiB ± 5.26 MiB 644 MiB ± 4.75 MiB +0.03 %
large-dyn-imports_production-mode + rss memory 526 MiB ± 7.84 MiB 533 MiB ± 8.23 MiB +1.22 %
threejs_development-mode_10x + rss memory 553 MiB ± 16.3 MiB 544 MiB ± 30.5 MiB -1.58 %
threejs_development-mode_10x_hmr + rss memory 1145 MiB ± 140 MiB 1147 MiB ± 36.2 MiB +0.19 %
threejs_production-mode_10x + rss memory 828 MiB ± 50.8 MiB 834 MiB ± 43.1 MiB +0.78 %
threejs_production-mode_10x_persistent-cold + rss memory 960 MiB ± 14.5 MiB 934 MiB ± 50.5 MiB -2.70 %
threejs_production-mode_10x_persistent-hot + rss memory 877 MiB ± 54.8 MiB 866 MiB ± 26.9 MiB -1.32 %

Please sign in to comment.