@@ -50,22 +50,13 @@ impl PathExt for Path {
5050}
5151
5252/// The first one is for chunk name, the second element is used for generate absolute file name
53- pub fn representative_file_name_for_preserve_modules ( path : & Path ) -> ( Cow < str > , Cow < str > ) {
53+ pub fn representative_file_name_for_preserve_modules ( path : & Path ) -> ( Cow < str > , String ) {
5454 let file_name =
5555 path. file_stem ( ) . map_or_else ( || path. to_string_lossy ( ) , |stem| stem. to_string_lossy ( ) ) ;
56- let idx = path. to_string_lossy ( ) . rfind ( file_name. as_ref ( ) ) . expect ( "should contains file_name" ) ;
57- let ab_path = slice_cow_str ( path. to_string_lossy ( ) , 0 , idx + file_name. len ( ) ) ;
56+ let ab_path = path. with_extension ( "" ) . to_string_lossy ( ) . into_owned ( ) ;
5857 ( file_name, ab_path)
5958}
6059
61- #[ inline]
62- fn slice_cow_str ( cow : Cow < str > , start : usize , end : usize ) -> Cow < ' _ , str > {
63- match cow {
64- Cow :: Borrowed ( s) => Cow :: Borrowed ( & s[ start..end] ) ,
65- Cow :: Owned ( s) => Cow :: Owned ( s[ start..end] . to_string ( ) ) ,
66- }
67- }
68-
6960#[ test]
7061fn test_representative_file_name ( ) {
7162 let cwd = Path :: new ( "." ) . join ( "project" ) ;
@@ -78,6 +69,10 @@ fn test_representative_file_name() {
7869 let path = cwd. join ( "vue" ) . join ( "mod.ts" ) ;
7970 assert_eq ! ( path. representative_file_name( ) , "vue" ) ;
8071
72+ let path = cwd. join ( "x.jsx" ) ;
73+ let ( _, ab_path) = representative_file_name_for_preserve_modules ( & path) ;
74+ assert_eq ! ( Path :: new( & ab_path) . file_name( ) . unwrap( ) . to_string_lossy( ) , "x" ) ;
75+
8176 #[ cfg( not( target_os = "windows" ) ) ]
8277 {
8378 let path = cwd. join ( "src" ) . join ( "vue.js" ) ;
0 commit comments