Skip to content

Commit 4caa070

Browse files
authored
feat: support binary option of asset generator (#10301)
* feat: support binary of asset generator options * feat: support binary of asset generator options * feat: support binary of asset generator options
1 parent 2d8c558 commit 4caa070

File tree

13 files changed

+162
-17
lines changed

13 files changed

+162
-17
lines changed

crates/node_binding/binding.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,10 +1455,12 @@ export interface RawAssetGeneratorOptions {
14551455
publicPath?: "auto" | JsFilename
14561456
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
14571457
importMode?: "url" | "preserve"
1458+
binary?: boolean
14581459
}
14591460

14601461
export interface RawAssetInlineGeneratorOptions {
14611462
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
1463+
binary?: boolean
14621464
}
14631465

14641466
export interface RawAssetParserDataUrl {
@@ -1480,6 +1482,7 @@ export interface RawAssetResourceGeneratorOptions {
14801482
outputPath?: JsFilename
14811483
publicPath?: "auto" | JsFilename
14821484
importMode?: "url" | "preserve"
1485+
binary?: boolean
14831486
}
14841487

14851488
export interface RawBannerPluginOptions {

crates/node_binding/src/raw_options/raw_module/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ pub struct RawAssetGeneratorOptions {
567567

568568
#[napi(ts_type = r#""url" | "preserve""#)]
569569
pub import_mode: Option<String>,
570+
pub binary: Option<bool>,
570571
}
571572

572573
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -580,6 +581,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
580581
.data_url
581582
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
582583
import_mode: value.import_mode.map(|n| n.into()),
584+
binary: value.binary,
583585
}
584586
}
585587
}
@@ -592,6 +594,7 @@ pub struct RawAssetInlineGeneratorOptions {
592594
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
593595
)]
594596
pub data_url: Option<RawAssetGeneratorDataUrl>,
597+
pub binary: Option<bool>,
595598
}
596599

597600
impl From<RawAssetInlineGeneratorOptions> for AssetInlineGeneratorOptions {
@@ -600,6 +603,7 @@ impl From<RawAssetInlineGeneratorOptions> for AssetInlineGeneratorOptions {
600603
data_url: value
601604
.data_url
602605
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
606+
binary: value.binary,
603607
}
604608
}
605609
}
@@ -614,6 +618,7 @@ pub struct RawAssetResourceGeneratorOptions {
614618
pub public_path: Option<JsFilename>,
615619
#[napi(ts_type = r#""url" | "preserve""#)]
616620
pub import_mode: Option<String>,
621+
pub binary: Option<bool>,
617622
}
618623

619624
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -624,6 +629,7 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
624629
output_path: value.output_path.map(|i| i.into()),
625630
public_path: value.public_path.map(|i| i.into()),
626631
import_mode: value.import_mode.map(|i| i.into()),
632+
binary: value.binary,
627633
}
628634
}
629635
}

crates/rspack_core/src/module.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ pub struct BuildContext {
4545
pub fs: Arc<dyn ReadableFileSystem>,
4646
}
4747

48-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
49-
pub enum BuildExtraDataType {
50-
CssParserAndGenerator,
51-
AssetParserAndGenerator,
52-
JavaScriptParserAndGenerator,
53-
}
54-
5548
#[cacheable]
5649
#[derive(Debug, Clone)]
5750
pub struct BuildInfo {

crates/rspack_core/src/normal_module.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,18 @@ impl Module for NormalModule {
495495
.await?;
496496
self.add_diagnostics(ds);
497497

498-
let content = if self.module_type().is_binary() {
498+
let is_binary = self
499+
.generator_options
500+
.as_ref()
501+
.and_then(|g| match g {
502+
GeneratorOptions::Asset(g) => g.binary,
503+
GeneratorOptions::AssetInline(g) => g.binary,
504+
GeneratorOptions::AssetResource(g) => g.binary,
505+
_ => None,
506+
})
507+
.unwrap_or(self.module_type().is_binary());
508+
509+
let content = if is_binary {
499510
Content::Buffer(loader_result.content.into_bytes())
500511
} else {
501512
Content::String(loader_result.content.into_string_lossy())

crates/rspack_core/src/options/module.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,14 @@ impl GeneratorOptions {
463463
#[derive(Debug, Clone, MergeFrom)]
464464
pub struct AssetInlineGeneratorOptions {
465465
pub data_url: Option<AssetGeneratorDataUrl>,
466+
pub binary: Option<bool>,
466467
}
467468

468469
impl From<AssetGeneratorOptions> for AssetInlineGeneratorOptions {
469470
fn from(value: AssetGeneratorOptions) -> Self {
470471
Self {
471472
data_url: value.data_url,
473+
binary: value.binary,
472474
}
473475
}
474476
}
@@ -520,6 +522,7 @@ pub struct AssetResourceGeneratorOptions {
520522
pub output_path: Option<Filename>,
521523
pub public_path: Option<PublicPath>,
522524
pub import_mode: Option<AssetGeneratorImportMode>,
525+
pub binary: Option<bool>,
523526
}
524527

525528
impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -530,6 +533,7 @@ impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
530533
output_path: value.output_path,
531534
public_path: value.public_path,
532535
import_mode: value.import_mode,
536+
binary: value.binary,
533537
}
534538
}
535539
}
@@ -543,6 +547,7 @@ pub struct AssetGeneratorOptions {
543547
pub public_path: Option<PublicPath>,
544548
pub data_url: Option<AssetGeneratorDataUrl>,
545549
pub import_mode: Option<AssetGeneratorImportMode>,
550+
pub binary: Option<bool>,
546551
}
547552

548553
pub struct AssetGeneratorDataUrlFnCtx<'a> {

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use async_trait::async_trait;
77
use rayon::prelude::*;
88
use rspack_cacheable::{cacheable, cacheable_dyn};
99
use rspack_core::{
10-
rspack_sources::{BoxSource, RawBufferSource, RawStringSource, SourceExt},
10+
rspack_sources::{BoxSource, RawStringSource, SourceExt},
1111
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetGeneratorImportMode, AssetInfo,
1212
AssetParserDataUrl, BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey,
1313
CodeGenerationDataAssetInfo, CodeGenerationDataFilename, CodeGenerationDataUrl,
@@ -632,7 +632,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
632632
"Inline or Source asset does not have source type `asset`"
633633
))
634634
} else {
635-
Ok(RawBufferSource::from(source.buffer().to_vec()).boxed())
635+
Ok(source.clone().boxed())
636636
}
637637
}
638638
_ => panic!(

crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use crate::{
2929
ModuleFilenameTemplateFn, ModuleOrSource,
3030
};
3131

32+
static SCHEMA_SOURCE_REGEXP: LazyLock<Regex> =
33+
LazyLock::new(|| Regex::new(r"^(data|https?):").expect("failed to compile SCHEMA_SOURCE_REGEXP"));
34+
3235
static CSS_EXTENSION_DETECT_REGEXP: LazyLock<Regex> = LazyLock::new(|| {
3336
Regex::new(r"\.css($|\?)").expect("failed to compile CSS_EXTENSION_DETECT_REGEXP")
3437
});
@@ -276,6 +279,12 @@ impl SourceMapDevToolPlugin {
276279
ModuleFilenameTemplate::String(s) => module_source_names
277280
.into_par_iter()
278281
.map(|module_or_source| {
282+
if let ModuleOrSource::Source(source) = module_or_source {
283+
if SCHEMA_SOURCE_REGEXP.is_match(source) {
284+
return (module_or_source, source.to_string());
285+
}
286+
}
287+
279288
let source_name = ModuleFilenameHelpers::create_filename_of_string_template(
280289
module_or_source,
281290
compilation,
@@ -290,6 +299,12 @@ impl SourceMapDevToolPlugin {
290299
let features = module_source_names
291300
.into_iter()
292301
.map(|module_or_source| async move {
302+
if let ModuleOrSource::Source(source) = module_or_source {
303+
if SCHEMA_SOURCE_REGEXP.is_match(source) {
304+
return Ok((module_or_source, source.to_string()));
305+
}
306+
}
307+
293308
let source_name = ModuleFilenameHelpers::create_filename_of_fn_template(
294309
module_or_source,
295310
compilation,

packages/rspack/src/config/adapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,8 @@ function getRawAssetInlineGeneratorOptions(
711711
return {
712712
dataUrl: options.dataUrl
713713
? getRawAssetGeneratorDataUrl(options.dataUrl)
714-
: undefined
714+
: undefined,
715+
binary: options.binary
715716
};
716717
}
717718

@@ -723,7 +724,8 @@ function getRawAssetResourceGeneratorOptions(
723724
filename: options.filename,
724725
outputPath: options.outputPath,
725726
publicPath: options.publicPath,
726-
importMode: options.importMode
727+
importMode: options.importMode,
728+
binary: options.binary
727729
};
728730
}
729731

packages/rspack/src/config/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,10 @@ export type AssetGeneratorDataUrl =
11711171
export type AssetInlineGeneratorOptions = {
11721172
/** Only for modules with module type 'asset' or 'asset/inline'. */
11731173
dataUrl?: AssetGeneratorDataUrl;
1174+
/**
1175+
* Whether or not this asset module should be considered binary. This can be set to 'false' to treat this asset module as text.
1176+
*/
1177+
binary?: boolean;
11741178
};
11751179

11761180
/** Emit the asset in the specified folder relative to 'output.path'. */
@@ -1208,6 +1212,11 @@ export type AssetResourceGeneratorOptions = {
12081212
* @default "url"
12091213
*/
12101214
importMode?: AssetModuleImportMode;
1215+
1216+
/**
1217+
* Whether or not this asset module should be considered binary. This can be set to 'false' to treat this asset module as text.
1218+
*/
1219+
binary?: boolean;
12111220
};
12121221

12131222
/** Generator options for asset modules. */

tests/webpack-test/configCases/asset-modules/keep-source-maps/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
it("should write asset file to output directory", function() {
1+
it("should write asset file to output directory", function () {
22
const fs = require("fs");
33
const path = require("path");
44
const source = fs.readFileSync(path.join(__dirname, "asset.css"), "utf-8");
55
expect(source).toMatch("/*# sourceMappingURL=asset.css.map*/");
66
});
77

8-
it("should write sourcemap file relative to fileContext", function() {
8+
it("should write sourcemap file relative to fileContext", function () {
99
const fs = require("fs");
10-
const path = require("path");
10+
const path = require("path");
1111
expect(fs.existsSync(path.join(__dirname, "asset.css.map"))).toBe(true);
1212
const source = JSON.parse(fs.readFileSync(path.join(__dirname, "asset.css.map"), "utf-8"));
1313
expect(source.sources[0]).toBe("webpack:///asset.scss");

0 commit comments

Comments
 (0)