Skip to content

Commit

Permalink
feat!: align AssetGeneratorDataUrlFunction with webpack (#8614)
Browse files Browse the repository at this point in the history
feat: align AssetGeneratorDataUrlFunction with webpack
  • Loading branch information
inottn authored Dec 16, 2024
1 parent 0606731 commit fb2869d
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 88 deletions.
8 changes: 4 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,9 +1113,9 @@ export interface RawAliasOptionItem {
redirect: Array<string | false>
}

export interface RawAssetGeneratorDataUrlFnArgs {
export interface RawAssetGeneratorDataUrlFnCtx {
filename: string
content: string
module: JsModule
}

export interface RawAssetGeneratorDataUrlOptions {
Expand All @@ -1127,11 +1127,11 @@ export interface RawAssetGeneratorOptions {
emit?: boolean
filename?: JsFilename
publicPath?: "auto" | JsFilename
dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
}

export interface RawAssetInlineGeneratorOptions {
dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
}

export interface RawAssetParserDataUrl {
Expand Down
32 changes: 19 additions & 13 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::fmt::Formatter;
use std::{collections::HashMap, sync::Arc};

use derive_more::Debug;
use napi::bindgen_prelude::Either3;
use napi::bindgen_prelude::{Buffer, Either3};
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::JsFilename;
use rspack_binding_values::{JsFilename, JsModuleWrapper};
use rspack_core::{
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetGeneratorDataUrlOptions,
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetGeneratorDataUrlOptions,
AssetGeneratorOptions, AssetInlineGeneratorOptions, AssetParserDataUrl,
AssetParserDataUrlOptions, AssetParserOptions, AssetResourceGeneratorOptions,
CssAutoGeneratorOptions, CssAutoParserOptions, CssGeneratorOptions, CssModuleGeneratorOptions,
Expand Down Expand Up @@ -500,7 +500,7 @@ pub struct RawAssetGeneratorOptions {
pub public_path: Option<JsFilename>,
#[debug(skip)]
#[napi(
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,
}
Expand All @@ -523,7 +523,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
pub struct RawAssetInlineGeneratorOptions {
#[debug(skip)]
#[napi(
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,
}
Expand Down Expand Up @@ -559,22 +559,26 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {

type RawAssetGeneratorDataUrl = Either<
RawAssetGeneratorDataUrlOptions,
ThreadsafeFunction<RawAssetGeneratorDataUrlFnArgs, String>,
ThreadsafeFunction<(Buffer, RawAssetGeneratorDataUrlFnCtx), String>,
>;
struct RawAssetGeneratorDataUrlWrapper(RawAssetGeneratorDataUrl);

#[derive(Debug, Clone)]
#[napi(object)]
pub struct RawAssetGeneratorDataUrlFnArgs {
pub struct RawAssetGeneratorDataUrlFnCtx {
pub filename: String,
pub content: String,
#[napi(ts_type = "JsModule")]
pub module: JsModuleWrapper,
}

impl From<AssetGeneratorDataUrlFnArgs> for RawAssetGeneratorDataUrlFnArgs {
fn from(value: AssetGeneratorDataUrlFnArgs) -> Self {
impl From<AssetGeneratorDataUrlFnCtx<'_>> for RawAssetGeneratorDataUrlFnCtx {
fn from(value: AssetGeneratorDataUrlFnCtx) -> Self {
Self {
filename: value.filename,
content: value.content,
module: JsModuleWrapper::new(
value.module,
value.compilation.id(),
Some(value.compilation),
),
}
}
}
Expand All @@ -584,7 +588,9 @@ impl From<RawAssetGeneratorDataUrlWrapper> for AssetGeneratorDataUrl {
use pollster::block_on;
match value.0 {
Either::A(a) => Self::Options(a.into()),
Either::B(b) => Self::Func(Arc::new(move |ctx| block_on(b.call(ctx.into())))),
Either::B(b) => Self::Func(Arc::new(move |source, ctx| {
block_on(b.call((source.into(), ctx.into())))
})),
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rspack_regex::RspackRegex;
use rspack_util::{try_all, try_any, MergeFrom};
use rustc_hash::FxHashMap as HashMap;

use crate::{Filename, Module, ModuleType, PublicPath, Resolve};
use crate::{Compilation, Filename, Module, ModuleType, PublicPath, Resolve};

#[derive(Debug)]
pub struct ParserOptionsMap(HashMap<String, ParserOptions>);
Expand Down Expand Up @@ -382,13 +382,14 @@ pub struct AssetGeneratorOptions {
pub data_url: Option<AssetGeneratorDataUrl>,
}

pub struct AssetGeneratorDataUrlFnArgs {
pub struct AssetGeneratorDataUrlFnCtx<'a> {
pub filename: String,
pub content: String,
pub module: &'a dyn Module,
pub compilation: &'a Compilation,
}

pub type AssetGeneratorDataUrlFn =
Arc<dyn Fn(AssetGeneratorDataUrlFnArgs) -> Result<String> + Sync + Send>;
Arc<dyn Fn(Vec<u8>, AssetGeneratorDataUrlFnCtx) -> Result<String> + Sync + Send>;

#[cacheable]
pub enum AssetGeneratorDataUrl {
Expand Down
25 changes: 14 additions & 11 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rayon::prelude::*;
use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_core::{
rspack_sources::{BoxSource, RawBufferSource, RawStringSource, SourceExt},
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetInfo, AssetParserDataUrl,
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetInfo, AssetParserDataUrl,
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey, CodeGenerationDataAssetInfo,
CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, CompilationRenderManifest,
CompilerOptions, Filename, GenerateContext, GeneratorOptions, LocalFilenameFn, Module,
Expand Down Expand Up @@ -135,18 +135,19 @@ impl AssetParserAndGenerator {
resource_data: &ResourceData,
data_url: Option<&AssetGeneratorDataUrl>,
source: &BoxSource,
module: &dyn Module,
compilation: &Compilation,
) -> Option<String> {
let func_args = AssetGeneratorDataUrlFnArgs {
filename: resource_data
.resource_path
.as_deref()
.map(|p| p.as_str().to_string())
.unwrap_or_default(),
content: source.source().into_owned(),
let func_ctx = AssetGeneratorDataUrlFnCtx {
filename: resource_data.resource.clone(),
module,
compilation,
};

if let Some(AssetGeneratorDataUrl::Func(data_url)) = data_url {
return Some(data_url(func_args).expect("call data_url function failed"));
return Some(
data_url(source.buffer().to_vec(), func_ctx).expect("call data_url function failed"),
);
}
None
}
Expand Down Expand Up @@ -397,7 +398,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
fn generate(
&self,
source: &BoxSource,
module: &dyn rspack_core::Module,
module: &dyn Module,
generate_context: &mut GenerateContext,
) -> Result<BoxSource> {
let compilation = generate_context.compilation;
Expand All @@ -418,7 +419,9 @@ impl ParserAndGenerator for AssetParserAndGenerator {

let encoded_source: String;

if let Some(custom_data_url) = self.get_data_url(resource_data, data_url, source) {
if let Some(custom_data_url) =
self.get_data_url(resource_data, data_url, source, module, compilation)
{
encoded_source = custom_data_url;
} else {
let mimetype = self.get_mimetype(resource_data, data_url)?;
Expand Down
Loading

2 comments on commit fb2869d

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

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

@rspack-bot
Copy link

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 (2024-12-16 2f4992b) Current Change
10000_big_production-mode_disable-minimize + exec 38.4 s ± 586 ms 38 s ± 531 ms -0.97 %
10000_development-mode + exec 1.89 s ± 64 ms 1.84 s ± 24 ms -2.48 %
10000_development-mode_hmr + exec 690 ms ± 23 ms 682 ms ± 24 ms -1.08 %
10000_production-mode + exec 2.49 s ± 47 ms 2.43 s ± 62 ms -2.53 %
arco-pro_development-mode + exec 1.77 s ± 76 ms 1.78 s ± 47 ms +0.46 %
arco-pro_development-mode_hmr + exec 379 ms ± 1.4 ms 378 ms ± 1.8 ms -0.26 %
arco-pro_production-mode + exec 3.33 s ± 128 ms 3.33 s ± 108 ms +0.05 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.41 s ± 149 ms 3.35 s ± 85 ms -1.68 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.34 s ± 90 ms 3.36 s ± 53 ms +0.52 %
threejs_development-mode_10x + exec 1.62 s ± 14 ms 1.62 s ± 29 ms +0.30 %
threejs_development-mode_10x_hmr + exec 787 ms ± 10 ms 792 ms ± 37 ms +0.61 %
threejs_production-mode_10x + exec 5.42 s ± 77 ms 5.4 s ± 150 ms -0.44 %
10000_big_production-mode_disable-minimize + rss memory 9499 MiB ± 255 MiB 9513 MiB ± 369 MiB +0.14 %
10000_development-mode + rss memory 629 MiB ± 18.3 MiB 672 MiB ± 27.8 MiB +6.97 %
10000_development-mode_hmr + rss memory 1515 MiB ± 164 MiB 1426 MiB ± 472 MiB -5.82 %
10000_production-mode + rss memory 590 MiB ± 18 MiB 651 MiB ± 39.4 MiB +10.28 %
arco-pro_development-mode + rss memory 570 MiB ± 26.4 MiB 586 MiB ± 38.8 MiB +2.89 %
arco-pro_development-mode_hmr + rss memory 640 MiB ± 39 MiB 625 MiB ± 41.7 MiB -2.28 %
arco-pro_production-mode + rss memory 690 MiB ± 48.5 MiB 742 MiB ± 64.6 MiB +7.52 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 736 MiB ± 73.2 MiB 742 MiB ± 33.4 MiB +0.83 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 728 MiB ± 34.9 MiB 740 MiB ± 25.5 MiB +1.69 %
threejs_development-mode_10x + rss memory 590 MiB ± 19.2 MiB 635 MiB ± 26.4 MiB +7.49 %
threejs_development-mode_10x_hmr + rss memory 1125 MiB ± 168 MiB 1201 MiB ± 175 MiB +6.74 %
threejs_production-mode_10x + rss memory 896 MiB ± 52.9 MiB 947 MiB ± 78.8 MiB +5.66 %

Please sign in to comment.