Skip to content

Commit

Permalink
refactor: allow passing function type to assets generator.filename (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xc2 authored Jul 18, 2024
1 parent 748a372 commit 03b3641
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 65 deletions.
4 changes: 2 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ export interface RawAssetGeneratorDataUrlOptions {

export interface RawAssetGeneratorOptions {
emit?: boolean
filename?: string
filename?: JsFilename
publicPath?: string
dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)
}
Expand All @@ -771,7 +771,7 @@ export interface RawAssetParserOptions {

export interface RawAssetResourceGeneratorOptions {
emit?: boolean
filename?: string
filename?: JsFilename
publicPath?: string
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use derivative::Derivative;
use napi::bindgen_prelude::Either3;
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::RawRegex;
use rspack_binding_values::{JsFilename, RawRegex};
use rspack_core::{
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetGeneratorDataUrlOptions,
AssetGeneratorOptions, AssetInlineGeneratorOptions, AssetParserDataUrl,
Expand Down Expand Up @@ -483,7 +483,7 @@ impl From<RawGeneratorOptions> for GeneratorOptions {
#[napi(object, object_to_js = false)]
pub struct RawAssetGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<String>,
pub filename: Option<JsFilename>,
pub public_path: Option<String>,
#[derivative(Debug = "ignore")]
#[napi(
Expand Down Expand Up @@ -527,10 +527,10 @@ impl From<RawAssetInlineGeneratorOptions> for AssetInlineGeneratorOptions {
}

#[derive(Debug, Default)]
#[napi(object)]
#[napi(object, object_to_js = false)]
pub struct RawAssetResourceGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<String>,
pub filename: Option<JsFilename>,
pub public_path: Option<String>,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import png from "../_images/file.png";
import png1 from "../_images/file.png?custom1";
import png2 from "../_images/file.png?custom2";
import jpeg2 from "../_images/file.jpg?custom2";

it("should change filenames", () => {
expect(png).toEqual("images/failure.png");
expect(png1).toEqual("custom-images/success1.png");
expect(png2).toEqual("custom-images/success2.png");
expect(jpeg2).toEqual("images/failure2.jpg");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* this test case is in addition to webpack-test/configCases/asset-modules/assetModuleFilename
*/
/** @type {import("@rspack/core").Configuration} */
module.exports = {
mode: "development",
output: {
assetModuleFilename: "images/failure[ext]"
},
module: {
rules: [
{
test: /\.(png|jpg)$/,
type: "asset/resource",
rules: [
{
resourceQuery: "?custom1",
generator: {
filename: "custom-images/success1[ext]"
}
},

{
resourceQuery: "?custom2",
generator: {
filename: ({ filename }) => {
if (filename.endsWith(".png?custom2")) {
return "custom-images/success2[ext]";
}
return "images/failure2[ext]";
}
}
}
]
}
]
}
};
Loading

2 comments on commit 03b3641

@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-07-18 ebcd277) Current Change
10000_development-mode + exec 2.25 s ± 26 ms 2.26 s ± 19 ms +0.13 %
10000_development-mode_hmr + exec 697 ms ± 3.8 ms 702 ms ± 11 ms +0.78 %
10000_production-mode + exec 2.83 s ± 30 ms 2.87 s ± 29 ms +1.56 %
arco-pro_development-mode + exec 1.91 s ± 57 ms 1.9 s ± 54 ms -0.22 %
arco-pro_development-mode_hmr + exec 434 ms ± 3.2 ms 435 ms ± 4 ms +0.33 %
arco-pro_production-mode + exec 3.46 s ± 74 ms 3.5 s ± 72 ms +1.30 %
threejs_development-mode_10x + exec 1.75 s ± 20 ms 1.76 s ± 24 ms +0.55 %
threejs_development-mode_10x_hmr + exec 859 ms ± 7.4 ms 864 ms ± 8.9 ms +0.62 %
threejs_production-mode_10x + exec 5.73 s ± 41 ms 5.75 s ± 58 ms +0.32 %

@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 ✅ success
_selftest ✅ success
nx ✅ success
rspress ✅ success
rsbuild ✅ success
examples ❌ failure

Please sign in to comment.