Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow passing function type to assets generator.filename #7206

Merged
merged 1 commit into from
Jul 18, 2024
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
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
Loading