Skip to content

Commit

Permalink
feat: support filename function (web-infra-dev#5957)
Browse files Browse the repository at this point in the history
* feat: support filename function

* fix: clippy

* chore: rename ChunkPathData to JsChunkPathData

* refactor: GetChunkFilenameRuntimeModule::generate to avoid the need for ByRef

* chore: hide pointers in ThreadsafeFunction Debug output

* refactor: remove ThreadSafeFunctionWithRef

* chore: remove rspack_napi tests

* perf: use FxHashMap instead of std HashMap

* docs: update docs for output.*filename
  • Loading branch information
branchseer authored Mar 21, 2024
1 parent 923e999 commit 67df93d
Show file tree
Hide file tree
Showing 110 changed files with 1,429 additions and 931 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

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

24 changes: 16 additions & 8 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export class JsCompilation {
pushDiagnostic(severity: "error" | "warning", title: string, message: string): void
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): 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
getAssetPath(filename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string), data: PathData): string
getAssetPathWithInfo(filename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string), data: PathData): PathWithInfo
getPath(filename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string), data: PathData): string
getPathWithInfo(filename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string), data: PathData): PathWithInfo
addFileDependencies(deps: Array<string>): void
addContextDependencies(deps: Array<string>): void
addMissingDependencies(deps: Array<string>): void
Expand Down Expand Up @@ -275,6 +275,13 @@ export interface JsChunkGroup {
name?: string
}

export interface JsChunkPathData {
id?: string
name?: string
hash?: string
contentHash?: Record<string, string>
}

export interface JsCodegenerationResult {
sources: Record<string, string>
}
Expand Down Expand Up @@ -573,6 +580,7 @@ export interface PathData {
runtime?: string
url?: string
id?: string
chunk?: JsChunkPathData
}

export interface PathWithInfo {
Expand Down Expand Up @@ -1051,11 +1059,11 @@ export interface RawOutputOptions {
wasmLoading: string
enabledWasmLoadingTypes: Array<string>
webassemblyModuleFilename: string
filename: string
chunkFilename: string
filename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
chunkFilename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
crossOriginLoading: RawCrossOriginLoading
cssFilename: string
cssChunkFilename: string
cssFilename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
cssChunkFilename: string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
hotUpdateMainFilename: string
hotUpdateChunkFilename: string
hotUpdateGlobal: string
Expand Down
2 changes: 1 addition & 1 deletion crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl AsyncSeries3<Compilation, ModuleIdentifier, ChunkUkey> for CompilationRunti
module: JsRuntimeModule {
source: Some(
module
.generate(compilation)
.generate(compilation)?
.to_js_compat_source()
.unwrap_or_else(|err| panic!("Failed to generate runtime module source: {err}")),
),
Expand Down
15 changes: 10 additions & 5 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use napi_derive::napi;
use rspack_binding_values::JsFilename;
use rspack_core::{
CrossOriginLoading, LibraryCustomUmdObject, LibraryName, LibraryNonUmdObject, LibraryOptions,
};
Expand Down Expand Up @@ -147,7 +148,7 @@ impl From<RawCrossOriginLoading> for CrossOriginLoading {

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[napi(object)]
#[napi(object, object_to_js = false)]
pub struct RawOutputOptions {
pub path: String,
pub clean: bool,
Expand All @@ -156,11 +157,15 @@ pub struct RawOutputOptions {
pub wasm_loading: String,
pub enabled_wasm_loading_types: Vec<String>,
pub webassembly_module_filename: String,
pub filename: String,
pub chunk_filename: String,
#[napi(ts_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
pub filename: JsFilename,
#[napi(ts_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
pub chunk_filename: JsFilename,
pub cross_origin_loading: RawCrossOriginLoading,
pub css_filename: String,
pub css_chunk_filename: String,
#[napi(ts_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
pub css_filename: JsFilename,
#[napi(ts_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
pub css_chunk_filename: JsFilename,
pub hot_update_main_filename: String,
pub hot_update_chunk_filename: String,
pub hot_update_global: String,
Expand Down
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,3 +14,4 @@ rspack_error = { path = "../rspack_error" }
rspack_identifier = { path = "../rspack_identifier" }
rspack_napi = { path = "../rspack_napi" }
rustc-hash = { workspace = true }
serde = { workspace = true }
4 changes: 2 additions & 2 deletions crates/rspack_binding_values/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ impl JsChunk {
id_name_hints: Vec::from_iter(id_name_hints.clone()),
filename_template: filename_template
.as_ref()
.map(|tpl| tpl.template().to_string()),
.and_then(|f| Some(f.template()?.to_string())),
css_filename_template: css_filename_template
.as_ref()
.map(|tpl| tpl.template().to_string()),
.and_then(|f| Some(f.template()?.to_string())),
files,
runtime,
hash: hash.as_ref().map(|d| d.encoded().to_string()),
Expand Down
59 changes: 35 additions & 24 deletions crates/rspack_binding_values/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::module::ToJsModule;
use super::PathWithInfo;
use crate::utils::callbackify;
use crate::JsStatsOptimizationBailout;
use crate::LocalJsFilename;
use crate::{
chunk::JsChunk, module::JsModule, CompatSource, JsAsset, JsAssetInfo, JsChunkGroup,
JsCompatSource, JsStats, PathData, ToJsCompatSource,
Expand Down Expand Up @@ -356,41 +357,51 @@ impl JsCompilation {
}

#[napi]
pub fn get_asset_path(&self, filename: String, data: PathData) -> String {
self.0.get_asset_path(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
pub fn get_asset_path(
&self,
#[napi(ts_arg_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
filename: LocalJsFilename,
data: PathData,
) -> napi::Result<String> {
self
.0
.get_asset_path(&filename.into(), data.as_core_path_data())
}

#[napi]
pub fn get_asset_path_with_info(&self, filename: String, data: PathData) -> PathWithInfo {
self
pub fn get_asset_path_with_info(
&self,
#[napi(ts_arg_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
filename: LocalJsFilename,
data: PathData,
) -> napi::Result<PathWithInfo> {
let path_and_asset_info = self
.0
.get_asset_path_with_info(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
.into()
.get_asset_path_with_info(&filename.into(), data.as_core_path_data())?;
Ok(path_and_asset_info.into())
}

#[napi]
pub fn get_path(&self, filename: String, data: PathData) -> String {
self.0.get_path(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
pub fn get_path(
&self,
#[napi(ts_arg_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
filename: LocalJsFilename,
data: PathData,
) -> napi::Result<String> {
self.0.get_path(&filename.into(), data.as_core_path_data())
}

#[napi]
pub fn get_path_with_info(&self, filename: String, data: PathData) -> PathWithInfo {
self
pub fn get_path_with_info(
&self,
#[napi(ts_arg_type = "string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)")]
filename: LocalJsFilename,
data: PathData,
) -> napi::Result<PathWithInfo> {
let path_and_asset_info = self
.0
.get_path_with_info(
&rspack_core::Filename::from(filename),
data.as_core_path_data(),
)
.into()
.get_path_with_info(&filename.into(), data.as_core_path_data())?;
Ok(path_and_asset_info.into())
}

#[napi]
Expand Down
Loading

0 comments on commit 67df93d

Please sign in to comment.