Skip to content

Commit

Permalink
feat: align AssetGeneratorDataUrlFunction with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Nov 28, 2024
1 parent d249e62 commit a212eef
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/get-runner-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
main:
name: Get Runner Labels
runs-on: [self-hosted, Linux, ci]
runs-on: ubuntu-latest
outputs:
LINUX_RUNNER_LABELS: ${{ steps.run.outputs.LINUX_RUNNER_LABELS }}
MACOS_RUNNER_LABELS: ${{ steps.run.outputs.MACOS_RUNNER_LABELS }}
Expand Down
8 changes: 4 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,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 @@ -1084,11 +1084,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, fmt::Debug, sync::Arc};

use derivative::Derivative;
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 @@ -501,7 +501,7 @@ pub struct RawAssetGeneratorOptions {
pub public_path: Option<JsFilename>,
#[derivative(Debug = "ignore")]
#[napi(
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,
}
Expand All @@ -525,7 +525,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
pub struct RawAssetInlineGeneratorOptions {
#[derivative(Debug = "ignore")]
#[napi(
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,
}
Expand Down Expand Up @@ -561,22 +561,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 @@ -586,7 +590,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 @@ -364,13 +364,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>;

pub enum AssetGeneratorDataUrl {
Options(AssetGeneratorDataUrlOptions),
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 @@ -6,7 +6,7 @@ use async_trait::async_trait;
use rayon::prelude::*;
use rspack_core::{
rspack_sources::{BoxSource, RawSource, 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 @@ -131,18 +131,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 @@ -388,7 +389,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 @@ -409,7 +410,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

0 comments on commit a212eef

Please sign in to comment.