@@ -48,6 +48,7 @@ const DEFAULT_ENCODING: &str = "base64";
4848enum DataUrlOptions {
4949 Inline ( bool ) ,
5050 Source ,
51+ Bytes ,
5152 Auto ( Option < AssetParserDataUrl > ) ,
5253}
5354
@@ -60,6 +61,7 @@ const ASSET_RESOURCE: bool = false;
6061#[ derive( Debug , Clone ) ]
6162pub enum CanonicalizedDataUrlOption {
6263 Source ,
64+ Bytes ,
6365 Asset ( IsInline ) ,
6466}
6567
@@ -68,6 +70,10 @@ impl CanonicalizedDataUrlOption {
6870 matches ! ( self , CanonicalizedDataUrlOption :: Source )
6971 }
7072
73+ pub fn is_bytes ( & self ) -> bool {
74+ matches ! ( self , CanonicalizedDataUrlOption :: Bytes )
75+ }
76+
7177 pub fn is_inline ( & self ) -> bool {
7278 matches ! ( self , CanonicalizedDataUrlOption :: Asset ( ASSET_INLINE ) )
7379 }
@@ -118,6 +124,14 @@ impl AssetParserAndGenerator {
118124 }
119125 }
120126
127+ pub fn with_bytes ( ) -> Self {
128+ Self {
129+ emit : false ,
130+ data_url : DataUrlOptions :: Bytes ,
131+ parsed_asset_config : None ,
132+ }
133+ }
134+
121135 fn decode_data_uri_content ( encoding : & str , content : & str , source : & BoxSource ) -> Vec < u8 > {
122136 if encoding == "base64"
123137 && let Some ( cleaned) = base64:: clean_base64 ( content)
@@ -372,7 +386,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
372386 if self
373387 . parsed_asset_config
374388 . as_ref ( )
375- . is_some_and ( |x| x. is_source ( ) || x. is_inline ( ) )
389+ . is_some_and ( |x| x. is_source ( ) || x. is_inline ( ) || x . is_bytes ( ) )
376390 || !self . emit
377391 {
378392 if source_types. is_empty ( ) {
@@ -420,7 +434,9 @@ impl ParserAndGenerator for AssetParserAndGenerator {
420434
421435 let parsed_size = self . parsed_asset_config . as_ref ( ) . map ( |config| {
422436 match config {
423- CanonicalizedDataUrlOption :: Source => original_source_size,
437+ CanonicalizedDataUrlOption :: Source | CanonicalizedDataUrlOption :: Bytes => {
438+ original_source_size
439+ }
424440 CanonicalizedDataUrlOption :: Asset ( meta) => {
425441 match * meta {
426442 ASSET_INLINE => {
@@ -465,6 +481,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
465481
466482 self . parsed_asset_config = match & self . data_url {
467483 DataUrlOptions :: Source => Some ( CanonicalizedDataUrlOption :: Source ) ,
484+ DataUrlOptions :: Bytes => Some ( CanonicalizedDataUrlOption :: Bytes ) ,
468485 DataUrlOptions :: Inline ( val) => Some ( CanonicalizedDataUrlOption :: Asset ( * val) ) ,
469486 DataUrlOptions :: Auto ( option) => {
470487 let limit_size = parse_context
@@ -524,7 +541,30 @@ impl ParserAndGenerator for AssetParserAndGenerator {
524541
525542 match generate_context. requested_source_type {
526543 SourceType :: JavaScript | SourceType :: CssUrl => {
527- let exported_content = if parsed_asset_config. is_inline ( ) {
544+ let exported_content = if parsed_asset_config. is_bytes ( ) {
545+ let mut encoded_source = base64:: encode_to_string ( source. buffer ( ) ) ;
546+ if generate_context. requested_source_type == SourceType :: CssUrl {
547+ encoded_source = format ! ( "data:application/octet-stream;base64,{}" , encoded_source) ;
548+ generate_context
549+ . data
550+ . insert ( CodeGenerationDataUrl :: new ( encoded_source. clone ( ) ) ) ;
551+ serde_json:: to_string ( & encoded_source) . to_rspack_result ( ) ?
552+ } else {
553+ generate_context
554+ . runtime_requirements
555+ . insert ( RuntimeGlobals :: REQUIRE_SCOPE ) ;
556+ generate_context
557+ . runtime_requirements
558+ . insert ( RuntimeGlobals :: TO_BINARY ) ;
559+ format ! (
560+ "{}({})" ,
561+ compilation
562+ . runtime_template
563+ . render_runtime_globals( & RuntimeGlobals :: TO_BINARY ) ,
564+ serde_json:: to_string( & encoded_source) . to_rspack_result( ) ?
565+ )
566+ }
567+ } else if parsed_asset_config. is_inline ( ) {
528568 let resource_data: & ResourceData = normal_module. resource_resolved_data ( ) ;
529569 let data_url = module_generator_options. and_then ( |x| x. asset_data_url ( ) ) ;
530570 let encoded_source: String ;
@@ -899,6 +939,11 @@ impl Plugin for AssetPlugin {
899939 Box :: new ( move |_, _| Box :: new ( AssetParserAndGenerator :: with_source ( ) ) ) ,
900940 ) ;
901941
942+ ctx. register_parser_and_generator_builder (
943+ rspack_core:: ModuleType :: AssetBytes ,
944+ Box :: new ( move |_, _| Box :: new ( AssetParserAndGenerator :: with_bytes ( ) ) ) ,
945+ ) ;
946+
902947 Ok ( ( ) )
903948 }
904949}
0 commit comments