Skip to content
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
39 changes: 35 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class JsCompilation {
getBuildDependencies(): Array<string>
pushDiagnostic(severity: "error" | "warning", title: string, message: string): void
getStats(): JsStats
getAssetPath(filename: string, data: PathData): string
getAssetPathWithInfo(filename: string, data: PathData): PathWithInfo
getPath(filename: string, data: PathData): string
getPathWithInfo(filename: string, data: PathData): PathWithInfo
addFileDependencies(deps: Array<string>): void
addContextDependencies(deps: Array<string>): void
addMissingDependencies(deps: Array<string>): void
Expand Down Expand Up @@ -102,10 +106,9 @@ export interface JsAsset {
}

export interface JsAssetInfo {
/**
* if the asset can be long term cached forever (contains a hash)
* whether the asset is minimized
*/
/** if the asset can be long term cached forever (contains a hash) */
immutable: boolean
/** whether the asset is minimized */
minimized: boolean
/**
* the value(s) of the full hash used for this asset
Expand Down Expand Up @@ -326,6 +329,27 @@ export interface JsStatsWarning {
formatted: string
}

export interface NodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
}

export interface PathData {
filename?: string
query?: string
fragment?: string
hash?: string
contentHash?: string
runtime?: string
url?: string
}

export interface PathWithInfo {
path: string
info: JsAssetInfo
}

export interface RawAssetParserDataUrlOption {
maxSize?: number
}
Expand Down Expand Up @@ -804,3 +828,10 @@ export interface SchemeAndJsResourceData {
scheme: string
}

export interface ThreadsafeNodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
removeDirAll: (...args: any[]) => any
}

4 changes: 3 additions & 1 deletion crates/node_binding/src/js_values/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl From<JsAssetInfoRelated> for rspack_core::AssetInfoRelated {
#[napi(object)]
pub struct JsAssetInfo {
/// if the asset can be long term cached forever (contains a hash)
// pub immutable: bool,
pub immutable: bool,
/// whether the asset is minimized
pub minimized: bool,
/// the value(s) of the full hash used for this asset
Expand Down Expand Up @@ -43,6 +43,7 @@ pub struct JsAssetInfo {
impl From<JsAssetInfo> for rspack_core::AssetInfo {
fn from(i: JsAssetInfo) -> Self {
Self {
immutable: i.immutable,
minimized: i.minimized,
development: i.development,
hot_module_replacement: i.hot_module_replacement,
Expand Down Expand Up @@ -70,6 +71,7 @@ impl From<rspack_core::AssetInfoRelated> for JsAssetInfoRelated {
impl From<rspack_core::AssetInfo> for JsAssetInfo {
fn from(info: rspack_core::AssetInfo) -> Self {
Self {
immutable: info.immutable,
minimized: info.minimized,
development: info.development,
hot_module_replacement: info.hot_module_replacement,
Expand Down
41 changes: 40 additions & 1 deletion crates/node_binding/src/js_values/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use rspack_identifier::Identifier;
use rspack_napi_shared::NapiResultExt;

use super::module::ToJsModule;
use super::PathWithInfo;
use crate::{
js_values::{chunk::JsChunk, module::JsModule},
js_values::{chunk::JsChunk, module::JsModule, PathData},
CompatSource, JsAsset, JsAssetInfo, JsChunkGroup, JsCompatSource, JsStats, ToJsCompatSource,
};

Expand Down Expand Up @@ -313,6 +314,44 @@ impl JsCompilation {
})?))
}

#[napi]
pub fn get_asset_path(&self, filename: String, data: PathData) -> String {
self.inner.get_asset_path(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
}

#[napi]
pub fn get_asset_path_with_info(&self, filename: String, data: PathData) -> PathWithInfo {
self
.inner
.get_asset_path_with_info(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
.into()
}

#[napi]
pub fn get_path(&self, filename: String, data: PathData) -> String {
self.inner.get_path(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
}

#[napi]
pub fn get_path_with_info(&self, filename: String, data: PathData) -> PathWithInfo {
self
.inner
.get_path_with_info(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
.into()
}

#[napi]
pub fn add_file_dependencies(&mut self, deps: Vec<String>) {
self
Expand Down
3 changes: 3 additions & 0 deletions crates/node_binding/src/js_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod compilation;
mod hooks;
mod module;
mod normal_module_factory;
mod path_data;
mod source;
mod stats;

Expand All @@ -13,6 +14,8 @@ pub use chunk::*;
pub use chunk_group::*;
pub use compilation::*;
pub use hooks::*;
pub use module::*;
pub use normal_module_factory::*;
pub use path_data::*;
pub use source::*;
pub use stats::*;
46 changes: 46 additions & 0 deletions crates/node_binding/src/js_values/path_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::path::Path;

use super::JsAssetInfo;

#[napi(object)]
pub struct PathData {
pub filename: Option<String>,
pub query: Option<String>,
pub fragment: Option<String>,
pub hash: Option<String>,
pub content_hash: Option<String>,
pub runtime: Option<String>,
pub url: Option<String>,
}

impl PathData {
pub fn as_core_path_data(&self) -> rspack_core::PathData {
rspack_core::PathData {
filename: self.filename.as_deref().map(Path::new),
query: self.query.as_deref(),
fragment: self.fragment.as_deref(),
chunk: None,
module: None,
hash: self.hash.as_deref(),
content_hash: self.content_hash.as_deref(),
chunk_graph: None,
runtime: self.runtime.as_deref(),
url: self.url.as_deref(),
}
}
}

#[napi(object)]
pub struct PathWithInfo {
pub path: String,
pub info: JsAssetInfo,
}

impl From<(String, rspack_core::AssetInfo)> for PathWithInfo {
fn from(value: (String, rspack_core::AssetInfo)) -> Self {
Self {
path: value.0,
info: value.1.into(),
}
}
}
6 changes: 6 additions & 0 deletions crates/rspack/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ fn rspack(fixture_path: PathBuf) {
test_fixture(&fixture_path);
}

#[fixture("tests/fixtures/code-splitting")]
fn rspack2(fixture_path: PathBuf) {
enable_tracing_by_env();
test_fixture(&fixture_path);
}

#[fixture("tests/samples/**/test.config.json")]
fn samples(fixture_path: PathBuf) {
enable_tracing_by_env();
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ impl Chunk {
.expect("Should set id before calling expect_id")
}

pub fn name_for_filename_template(&self) -> Option<String> {
pub fn name_for_filename_template(&self) -> Option<&str> {
if self.name.is_some() {
self.name.clone()
self.name.as_deref()
} else {
self.id.clone()
self.id.as_deref()
}
}

Expand Down
48 changes: 43 additions & 5 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ use crate::{
ChunkGroup, ChunkGroupUkey, ChunkHashArgs, ChunkKind, ChunkUkey, CleanQueue, CleanTask,
CleanTaskResult, CodeGenerationResult, CodeGenerationResults, CompilerOptions, ContentHashArgs,
DependencyId, EntryDependency, EntryItem, EntryOptions, Entrypoint, FactorizeQueue,
FactorizeTask, FactorizeTaskResult, Module, ModuleGraph, ModuleIdentifier, ModuleType,
NormalModuleAstOrSource, ProcessAssetsArgs, ProcessDependenciesQueue, ProcessDependenciesResult,
ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory, RuntimeGlobals,
RuntimeModule, RuntimeSpec, SharedPluginDriver, Stats, TaskResult, WorkerTask,
FactorizeTask, FactorizeTaskResult, Filename, Module, ModuleGraph, ModuleIdentifier, ModuleType,
NormalModuleAstOrSource, PathData, ProcessAssetsArgs, ProcessDependenciesQueue,
ProcessDependenciesResult, ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory,
RuntimeGlobals, RuntimeModule, RuntimeSpec, SharedPluginDriver, Stats, TaskResult, WorkerTask,
};

#[derive(Debug)]
Expand Down Expand Up @@ -1416,6 +1416,40 @@ impl Compilation {
.runtime_modules
.insert(runtime_module_identifier, module);
}

pub fn get_path<'b, 'a: 'b>(&'a self, filename: &Filename, mut data: PathData<'b>) -> String {
if data.hash.is_none() {
data.hash = Some(&self.hash);
}
filename.render(data, None)
}

pub fn get_path_with_info<'b, 'a: 'b>(
&'a self,
filename: &Filename,
mut data: PathData<'b>,
) -> (String, AssetInfo) {
let mut info = AssetInfo::default();
if data.hash.is_none() {
data.hash = Some(&self.hash);
}
let path = filename.render(data, Some(&mut info));
(path, info)
}

pub fn get_asset_path(&self, filename: &Filename, data: PathData) -> String {
filename.render(data, None)
}

pub fn get_asset_path_with_info(
&self,
filename: &Filename,
data: PathData,
) -> (String, AssetInfo) {
let mut info = AssetInfo::default();
let path = filename.render(data, Some(&mut info));
(path, info)
}
}

pub type CompilationAssets = HashMap<String, CompilationAsset>;
Expand Down Expand Up @@ -1465,7 +1499,7 @@ impl CompilationAsset {
#[derive(Debug, Default, Clone)]
pub struct AssetInfo {
/// if the asset can be long term cached forever (contains a hash)
// pub immutable: bool,
pub immutable: bool,
/// whether the asset is minimized
pub minimized: bool,
/// the value(s) of the full hash used for this asset
Expand Down Expand Up @@ -1519,6 +1553,10 @@ impl AssetInfo {
pub fn set_content_hash(&mut self, v: String) {
self.content_hash.insert(v);
}

pub fn set_immutable(&mut self, v: bool) {
self.immutable = v;
}
}

#[derive(Debug, Default, Clone)]
Expand Down
5 changes: 1 addition & 4 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,7 @@ where
);

// TODO: should use `get_path_info` to get filename.
let chunk = self
.compilation
.chunk_by_ukey
.get(&entry.path_options.chunk_ukey);
let chunk = self.compilation.chunk_by_ukey.get(&ukey);
let id = chunk.map_or(String::new(), |c| c.expect_id().to_string());
self.compilation.emit_asset(id + ".hot-update.js", asset);
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ where
asset: &CompilationAsset,
) -> Result<()> {
if let Some(source) = asset.get_source() {
let filename = filename
.split_once('?')
.map(|(filename, _query)| filename)
.unwrap_or(filename);
let file_path = Path::new(&output_path).join(filename);
self
.output_filesystem
Expand Down
Loading