11#![ feature( let_chains) ]
22
3- use std:: { borrow:: Cow , hash:: Hasher } ;
3+ use std:: { borrow:: Cow , hash:: Hasher , path :: PathBuf } ;
44
55use async_trait:: async_trait;
66use rayon:: prelude:: * ;
@@ -241,22 +241,38 @@ impl AssetParserAndGenerator {
241241 compilation : & Compilation ,
242242 contenthash : Option < & str > ,
243243 source_file_name : & str ,
244- ) -> Result < ( String , AssetInfo ) > {
244+ use_output_path : bool ,
245+ ) -> Result < ( String , String , AssetInfo ) > {
245246 // Use [Rule.generator.filename] if it is set, otherwise use [output.assetModuleFilename].
246247 let asset_filename_template = module_generator_options
247248 . and_then ( |x| x. asset_filename ( ) )
248249 . unwrap_or ( & compilation. options . output . asset_module_filename ) ;
250+ let path_data = PathData :: default ( )
251+ . module_id_optional (
252+ ChunkGraph :: get_module_id ( & compilation. module_ids , module. id ( ) ) . map ( |s| s. as_str ( ) ) ,
253+ )
254+ . content_hash_optional ( contenthash)
255+ . hash_optional ( contenthash)
256+ . filename ( source_file_name) ;
257+
258+ let ( mut filename, mut asset_info) =
259+ compilation. get_asset_path_with_info ( asset_filename_template, path_data) ?;
260+ let original_filename = filename. clone ( ) ;
261+
262+ if use_output_path {
263+ let output_path = module_generator_options. and_then ( |x| x. asset_output_path ( ) ) ;
264+
265+ if let Some ( output_path) = output_path {
266+ let ( output_path, another_asset_info) =
267+ compilation. get_asset_path_with_info ( output_path, path_data) ?;
268+ let output_path = PathBuf :: from ( output_path) ;
269+ let file_path = PathBuf :: from ( filename) ;
270+ filename = output_path. join ( file_path) . to_string_lossy ( ) . to_string ( ) ;
271+ asset_info. merge_another_asset ( another_asset_info) ;
272+ }
273+ }
249274
250- compilation. get_asset_path_with_info (
251- asset_filename_template,
252- PathData :: default ( )
253- . module_id_optional (
254- ChunkGraph :: get_module_id ( & compilation. module_ids , module. id ( ) ) . map ( |s| s. as_str ( ) ) ,
255- )
256- . content_hash_optional ( contenthash)
257- . hash_optional ( contenthash)
258- . filename ( source_file_name) ,
259- )
275+ Ok ( ( original_filename, filename, asset_info) )
260276 }
261277
262278 fn get_public_path < F : LocalFilenameFn > (
@@ -444,12 +460,13 @@ impl ParserAndGenerator for AssetParserAndGenerator {
444460 let contenthash = contenthash. rendered ( compilation. options . output . hash_digest_length ) ;
445461
446462 let source_file_name = self . get_source_file_name ( normal_module, compilation) ;
447- let ( filename, mut asset_info) = self . get_asset_module_filename (
463+ let ( original_filename , filename, mut asset_info) = self . get_asset_module_filename (
448464 normal_module,
449465 module_generator_options,
450466 compilation,
451467 Some ( contenthash) ,
452468 & source_file_name,
469+ true ,
453470 ) ?;
454471
455472 let asset_path = if let Some ( public_path) =
@@ -469,13 +486,17 @@ impl ParserAndGenerator for AssetParserAndGenerator {
469486 }
470487 PublicPath :: Auto => public_path. render ( compilation, & filename) ,
471488 } ;
472- serde_json:: to_string ( & format ! ( "{public_path}{filename }" ) )
489+ serde_json:: to_string ( & format ! ( "{public_path}{original_filename }" ) )
473490 . map_err ( |e| error ! ( e. to_string( ) ) ) ?
474491 } else {
475492 generate_context
476493 . runtime_requirements
477494 . insert ( RuntimeGlobals :: PUBLIC_PATH ) ;
478- format ! ( r#"{} + "{}""# , RuntimeGlobals :: PUBLIC_PATH , filename)
495+ format ! (
496+ r#"{} + "{}""# ,
497+ RuntimeGlobals :: PUBLIC_PATH ,
498+ original_filename
499+ )
479500 } ;
480501 asset_info. set_source_filename ( source_file_name) ;
481502
@@ -562,12 +583,13 @@ impl ParserAndGenerator for AssetParserAndGenerator {
562583 data_url_options. dyn_hash ( hasher) ;
563584 } else if parsed_asset_config. is_resource ( ) {
564585 let source_file_name = self . get_source_file_name ( module, compilation) ;
565- let ( filename, _) = self . get_asset_module_filename (
586+ let ( filename, _, _ ) = self . get_asset_module_filename (
566587 module,
567588 module_generator_options,
568589 compilation,
569590 None ,
570591 & source_file_name,
592+ false ,
571593 ) ?;
572594 filename. dyn_hash ( hasher) ;
573595 match module_generator_options. and_then ( |x| x. asset_public_path ( ) ) {
0 commit comments