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

fix: should use chunk in path data of compilation.getPath #8032

Merged
merged 3 commits into from
Oct 9, 2024
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 Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rspack_binding_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ napi-derive = { workspace = true }
rspack_collections = { version = "0.1.0", path = "../rspack_collections" }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_hash = { version = "0.1.0", path = "../rspack_hash" }
rspack_napi = { version = "0.1.0", path = "../rspack_napi" }
rspack_plugin_html = { version = "0.1.0", path = "../rspack_plugin_html" }
rspack_plugin_runtime = { version = "0.1.0", path = "../rspack_plugin_runtime" }
Expand Down
14 changes: 10 additions & 4 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,10 @@ impl JsCompilation {
filename: LocalJsFilename,
data: JsPathData,
) -> napi::Result<String> {
let chunk = data.chunk.as_ref().map(|c| c.to_chunk(self.0));
self
.0
.get_asset_path(&filename.into(), data.as_core_path_data())
.get_asset_path(&filename.into(), data.as_core_path_data(chunk.as_ref()))
}

#[napi]
Expand All @@ -398,15 +399,19 @@ impl JsCompilation {
filename: LocalJsFilename,
data: JsPathData,
) -> napi::Result<PathWithInfo> {
let chunk = data.chunk.as_ref().map(|c| c.to_chunk(self.0));
let path_and_asset_info = self
.0
.get_asset_path_with_info(&filename.into(), data.as_core_path_data())?;
.get_asset_path_with_info(&filename.into(), data.as_core_path_data(chunk.as_ref()))?;
Ok(path_and_asset_info.into())
}

#[napi]
pub fn get_path(&self, filename: LocalJsFilename, data: JsPathData) -> napi::Result<String> {
self.0.get_path(&filename.into(), data.as_core_path_data())
let chunk = data.chunk.as_ref().map(|c| c.to_chunk(self.0));
self
.0
.get_path(&filename.into(), data.as_core_path_data(chunk.as_ref()))
}

#[napi]
Expand All @@ -415,9 +420,10 @@ impl JsCompilation {
filename: LocalJsFilename,
data: JsPathData,
) -> napi::Result<PathWithInfo> {
let chunk = data.chunk.as_ref().map(|c| c.to_chunk(self.0));
let path_and_asset_info = self
.0
.get_path_with_info(&filename.into(), data.as_core_path_data())?;
.get_path_with_info(&filename.into(), data.as_core_path_data(chunk.as_ref()))?;
Ok(path_and_asset_info.into())
}

Expand Down
49 changes: 47 additions & 2 deletions crates/rspack_binding_values/src/path_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;

use napi::Either;
use napi_derive::napi;
use rspack_core::{Chunk, Compilation, SourceType};
use rspack_hash::RspackHashDigest;

use super::JsAssetInfo;

Expand Down Expand Up @@ -35,9 +37,48 @@ pub struct JsChunkPathData {
pub id: Option<String>,
pub name: Option<String>,
pub hash: Option<String>,
// TODO: support custom content hash type
pub content_hash: Option<Either<String, HashMap<String, String>>>,
}

impl JsChunkPathData {
pub fn to_chunk(&self, compilation: &Compilation) -> Chunk {
let mut chunk = rspack_core::Chunk::new(self.name.clone(), rspack_core::ChunkKind::Normal);
chunk.id = self.id.clone();
chunk.hash = self
.hash
.clone()
.map(|s| RspackHashDigest::new(s.into(), &compilation.options.output.hash_digest));

chunk.rendered_hash = self.hash.as_ref().map(|d| {
if d.len() < compilation.options.output.hash_digest_length {
d.as_str().into()
} else {
d[..compilation.options.output.hash_digest_length].into()
}
});
if let Some(hash) = self.content_hash.as_ref() {
match hash {
Either::A(hash) => {
chunk.content_hash.insert(
SourceType::Unknown,
RspackHashDigest::new(hash.clone().into(), &compilation.options.output.hash_digest),
);
}
Either::B(map) => {
for (key, hash) in map {
chunk.content_hash.insert(
SourceType::from(key.as_str()),
RspackHashDigest::new(hash.clone().into(), &compilation.options.output.hash_digest),
);
}
}
}
}
chunk
}
}

impl<'a> From<&'a rspack_core::Chunk> for JsChunkPathData {
fn from(chunk: &'a rspack_core::Chunk) -> Self {
Self {
Expand All @@ -56,10 +97,14 @@ impl<'a> From<&'a rspack_core::Chunk> for JsChunkPathData {
}

impl JsPathData {
pub fn as_core_path_data(&self) -> rspack_core::PathData {
pub fn as_core_path_data<'a>(
&'a self,
chunk: Option<&'a rspack_core::Chunk>,
) -> rspack_core::PathData<'a> {
rspack_core::PathData {
filename: self.filename.as_deref(),
chunk: None,
chunk,
// TODO: support custom module
module: None,
hash: self.hash.as_deref(),
content_hash: self.content_hash.as_deref(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const pluginName = "plugin";

class Plugin {
apply(compiler) {
let called = false;
compiler.hooks.compilation.tap(pluginName, compilation => {
called = true;
expect(compilation.getPath("[id]-[name]-[chunkhash]", {
chunk: {
name: "chunkname",
id: "chunkid",
hash: "chunkhash",
}
})).toBe("chunkid-chunkname-chunkhash");
});
compiler.hooks.done.tap(pluginName, stats => {
let json = stats.toJson();
expect(json.errors.length === 0);
expect(called).toBe(true);
});
}
}

/**@type {import("@rspack/core").Configuration}*/
module.exports = {
context: __dirname,
plugins: [new Plugin()]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("../../../../dist").TConfigCaseConfig} */
module.exports = {
noTests: true,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const pluginName = "plugin";

class Plugin {
apply(compiler) {
let called = false;
compiler.hooks.compilation.tap(pluginName, compilation => {
compilation.hooks.processAssets.tap(pluginName, () => {
called = true;
const mainChunk = Array.from(compilation.chunks).find(chunk => chunk.name === "main");
expect(compilation.getPath("[id]-[name]-[chunkhash]", {
chunk: mainChunk
})).toBe(`${mainChunk.id}-${mainChunk.name}-${mainChunk.renderedHash}`);
});
});
compiler.hooks.done.tap(pluginName, stats => {
let json = stats.toJson();
expect(json.errors.length === 0);
expect(called).toBe(true);
});
}
}

/**@type {import("@rspack/core").Configuration}*/
module.exports = {
context: __dirname,
plugins: [new Plugin()]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("../../../../dist").TConfigCaseConfig} */
module.exports = {
noTests: true,
}
Loading
Loading