Skip to content

Commit

Permalink
feat: add emit option for asset modules (#6255)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Apr 16, 2024
1 parent 98e4a72 commit 2320840
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ export interface RawAssetGeneratorDataUrlOptions {
}

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

export interface RawAssetResourceGeneratorOptions {
emit?: boolean
filename?: string
publicPath?: string
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ impl From<RawGeneratorOptions> for GeneratorOptions {
#[serde(rename_all = "camelCase")]
#[napi(object, object_to_js = false)]
pub struct RawAssetGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<String>,
pub public_path: Option<String>,
#[derivative(Debug = "ignore")]
Expand All @@ -533,6 +534,7 @@ pub struct RawAssetGeneratorOptions {
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
fn from(value: RawAssetGeneratorOptions) -> Self {
Self {
emit: value.emit,
filename: value.filename.map(|i| i.into()),
public_path: value.public_path.map(|i| i.into()),
data_url: value
Expand Down Expand Up @@ -569,13 +571,15 @@ impl From<RawAssetInlineGeneratorOptions> for AssetInlineGeneratorOptions {
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawAssetResourceGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<String>,
pub public_path: Option<String>,
}

impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
fn from(value: RawAssetResourceGeneratorOptions) -> Self {
Self {
emit: value.emit,
filename: value.filename.map(|i| i.into()),
public_path: value.public_path.map(|i| i.into()),
}
Expand Down
9 changes: 9 additions & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ impl GeneratorOptions {
.and_then(|x| x.data_url.as_ref())
})
}

pub fn asset_emit(&self, module_type: &ModuleType) -> Option<bool> {
self
.get_asset(module_type)
.and_then(|x| x.emit)
.or_else(|| self.get_asset_resource(module_type).and_then(|x| x.emit))
}
}

#[derive(Debug, Clone, MergeFrom)]
Expand All @@ -262,12 +269,14 @@ pub struct AssetInlineGeneratorOptions {

#[derive(Debug, Clone, MergeFrom)]
pub struct AssetResourceGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<Filename>,
pub public_path: Option<PublicPath>,
}

#[derive(Debug, Clone, MergeFrom)]
pub struct AssetGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<Filename>,
pub public_path: Option<PublicPath>,
pub data_url: Option<AssetGeneratorDataUrl>,
Expand Down
32 changes: 26 additions & 6 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,39 @@ impl CanonicalizedDataUrlOption {

#[derive(Debug)]
pub struct AssetParserAndGenerator {
emit: bool,
data_url: DataUrlOptions,
parsed_asset_config: Option<CanonicalizedDataUrlOption>,
}

impl AssetParserAndGenerator {
pub fn with_auto(option: Option<AssetParserDataUrl>) -> Self {
pub fn with_auto(option: Option<AssetParserDataUrl>, emit: bool) -> Self {
Self {
emit,
data_url: DataUrlOptions::Auto(option),
parsed_asset_config: None,
}
}

pub fn with_inline() -> Self {
Self {
emit: false,
data_url: DataUrlOptions::Inline(true),
parsed_asset_config: None,
}
}

pub fn with_resource() -> Self {
pub fn with_resource(emit: bool) -> Self {
Self {
emit,
data_url: DataUrlOptions::Inline(false),
parsed_asset_config: None,
}
}

pub fn with_source() -> Self {
Self {
emit: false,
data_url: DataUrlOptions::Source,
parsed_asset_config: None,
}
Expand Down Expand Up @@ -210,7 +215,7 @@ const DEFAULT_MAX_SIZE: u32 = 8096;
impl ParserAndGenerator for AssetParserAndGenerator {
fn source_types(&self) -> &[SourceType] {
if let Some(config) = self.parsed_asset_config.as_ref() {
if config.is_source() || config.is_inline() {
if config.is_source() || config.is_inline() || !self.emit {
ASSET_SOURCE_MODULE_SOURCE_TYPE_LIST
} else {
ASSET_MODULE_SOURCE_TYPE_LIST
Expand Down Expand Up @@ -589,23 +594,38 @@ impl Plugin for AssetPlugin {

ctx.context.register_parser_and_generator_builder(
rspack_core::ModuleType::Asset,
Box::new(move |p, _| {
let data_url_condition = p
Box::new(move |parser_options, generator_options| {
let data_url_condition = parser_options
.and_then(|x| x.get_asset(&ModuleType::Asset))
.and_then(|x| x.data_url_condition.clone());

let emit: Option<bool> = generator_options
.and_then(|x| x.get_asset(&ModuleType::Asset))
.and_then(|x| x.emit);

Box::new(AssetParserAndGenerator::with_auto(
data_url_condition.clone(),
emit.unwrap_or(true),
))
}),
);

ctx.context.register_parser_and_generator_builder(
rspack_core::ModuleType::AssetInline,
Box::new(|_, _| Box::new(AssetParserAndGenerator::with_inline())),
);

ctx.context.register_parser_and_generator_builder(
rspack_core::ModuleType::AssetResource,
Box::new(move |_, _| Box::new(AssetParserAndGenerator::with_resource())),
Box::new(move |_, generator_options| {
let emit = generator_options
.and_then(|x| x.get_asset_resource(&ModuleType::AssetResource))
.and_then(|x| x.emit);

Box::new(AssetParserAndGenerator::with_resource(emit.unwrap_or(true)))
}),
);

ctx.context.register_parser_and_generator_builder(
rspack_core::ModuleType::AssetSource,
Box::new(move |_, _| Box::new(AssetParserAndGenerator::with_source())),
Expand Down
20 changes: 19 additions & 1 deletion packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}>], z.ZodUnknown>, z.ZodString>]>>;
emit: z.ZodOptional<z.ZodBoolean>;
filename: z.ZodOptional<z.ZodString>;
publicPath: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"auto">, z.ZodString]>>;
}, "strict", z.ZodTypeAny, {
Expand All @@ -3373,6 +3374,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
}, {
Expand All @@ -3383,6 +3385,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
}>>;
Expand Down Expand Up @@ -3424,12 +3427,15 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
}>>;
"asset/resource": z.ZodOptional<z.ZodObject<{
emit: z.ZodOptional<z.ZodBoolean>;
filename: z.ZodOptional<z.ZodString>;
publicPath: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"auto">, z.ZodString]>>;
}, "strict", z.ZodTypeAny, {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
}, {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
}>>;
Expand Down Expand Up @@ -3478,6 +3484,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -3491,6 +3498,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -3517,6 +3525,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -3530,6 +3539,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand Down Expand Up @@ -3583,6 +3593,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -3596,6 +3607,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand Down Expand Up @@ -3649,6 +3661,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -3662,6 +3675,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand Down Expand Up @@ -4034,6 +4048,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -4047,6 +4062,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand Down Expand Up @@ -4419,6 +4435,7 @@ export const rspackOptions: z.ZodObject<{
filename: string;
content: string;
}, ...args_1: unknown[]) => string) | undefined;
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand All @@ -4432,6 +4449,7 @@ export const rspackOptions: z.ZodObject<{
}, ...args_1: unknown[]) => string) | undefined;
} | undefined;
"asset/resource"?: {
emit?: boolean | undefined;
filename?: string | undefined;
publicPath?: string | undefined;
} | undefined;
Expand Down Expand Up @@ -4994,7 +5012,7 @@ export type WorkerPublicPath = z.infer<typeof workerPublicPath>;
// dist/builtin-plugin/SwcJsMinimizerPlugin.d.ts:43:5 - (ae-forgotten-export) The symbol "ToSnakeCaseProperties" needs to be exported by the entry point index.d.ts
// dist/builtin-plugin/SwcJsMinimizerPlugin.d.ts:45:5 - (ae-forgotten-export) The symbol "MinifyConditions" needs to be exported by the entry point index.d.ts
// dist/config/adapterRuleUse.d.ts:145:5 - (ae-forgotten-export) The symbol "PitchLoaderDefinitionFunction" needs to be exported by the entry point index.d.ts
// dist/config/zod.d.ts:5264:5 - (ae-forgotten-export) The symbol "oldBuiltins" needs to be exported by the entry point index.d.ts
// dist/config/zod.d.ts:5304:5 - (ae-forgotten-export) The symbol "oldBuiltins" needs to be exported by the entry point index.d.ts
// dist/exports.d.ts:103:5 - (ae-forgotten-export) The symbol "ContainerPlugin" needs to be exported by the entry point index.d.ts
// dist/exports.d.ts:104:5 - (ae-forgotten-export) The symbol "ContainerReferencePlugin" needs to be exported by the entry point index.d.ts
// dist/exports.d.ts:105:5 - (ae-forgotten-export) The symbol "ModuleFederationPlugin" needs to be exported by the entry point index.d.ts
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ function getRawAssetResourceGeneratorOptions(
options: AssetResourceGeneratorOptions
): RawAssetResourceGeneratorOptions {
return {
emit: options.emit,
filename: options.filename,
publicPath: options.publicPath
};
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ export type AssetInlineGeneratorOptions = z.infer<
>;

const assetResourceGeneratorOptions = z.strictObject({
emit: z.boolean().optional(),
filename: filenameTemplate.optional(),
publicPath: publicPath.optional()
});
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import url from "../_images/file.png";
import url2 from "../_images/file.jpg";
import fs from "fs";
import path from "path";

it("should not output asset when emit is false", () => {
expect(url).toEqual("images/file.png");
expect(url2).toEqual("images/file.jpg");

expect(fs.existsSync(path.join(__STATS__.outputPath, url))).toBe(false);
expect(fs.existsSync(path.join(__STATS__.outputPath, url2))).toBe(true);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
output: {
assetModuleFilename: "images/file[ext]"
},
module: {
rules: [
{
test: /\.png$/,
type: "asset/resource",
generator: {
emit: false
}
},
{
test: /\.jpg$/,
type: "asset/resource"
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import url from "../_images/file.png";
import url2 from "../_images/file.jpg";
import fs from "fs";
import path from "path";

it("should not output asset when emit is false", () => {
expect(url).toEqual("images/file.png");
expect(url2).toEqual("images/file.jpg");

expect(fs.existsSync(path.join(__STATS__.outputPath, url))).toBe(false);
expect(fs.existsSync(path.join(__STATS__.outputPath, url2))).toBe(false);
});
Loading

2 comments on commit 2320840

@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-04-15 73a9832) Current Change
10000_development-mode + exec 2.67 s ± 33 ms 2.7 s ± 30 ms +1.05 %
10000_development-mode_hmr + exec 685 ms ± 12 ms 692 ms ± 5.8 ms +0.97 %
10000_production-mode + exec 2.5 s ± 30 ms 2.54 s ± 27 ms +1.21 %
arco-pro_development-mode + exec 2.53 s ± 93 ms 2.51 s ± 69 ms -0.87 %
arco-pro_development-mode_hmr + exec 430 ms ± 4.5 ms 430 ms ± 1.4 ms +0.03 %
arco-pro_development-mode_hmr_intercept-plugin + exec 442 ms ± 2.2 ms 442 ms ± 4.1 ms +0.09 %
arco-pro_development-mode_intercept-plugin + exec 3.27 s ± 75 ms 3.28 s ± 83 ms +0.41 %
arco-pro_production-mode + exec 4 s ± 95 ms 4.04 s ± 86 ms +1.03 %
arco-pro_production-mode_intercept-plugin + exec 4.76 s ± 59 ms 4.82 s ± 76 ms +1.19 %
threejs_development-mode_10x + exec 2.05 s ± 18 ms 2.06 s ± 28 ms +0.38 %
threejs_development-mode_10x_hmr + exec 768 ms ± 20 ms 773 ms ± 4.2 ms +0.67 %
threejs_production-mode_10x + exec 5.18 s ± 38 ms 5.21 s ± 59 ms +0.61 %

@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, self-hosted, Linux, ci ✅ success
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ✅ success
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

Please sign in to comment.