@@ -103,14 +103,39 @@ function buildFile(file, silent) {
103103 } (copy)\n`,
104104 ) ;
105105 } else {
106- const options = Object . assign ( { } , transformOptions ) ;
106+ const options = Object . assign ( { } , transformOptions , {
107+ sourceMaps : true ,
108+ sourceFileName : path . basename ( file ) ,
109+ } ) ;
107110
108111 let { code, map} = babel . transformFileSync ( file , options ) ;
109112
110- if ( ! file . endsWith ( '.d.ts' ) && map . sources . length > 0 ) {
111- code = `${ code } \n\n//# sourceMappingURL=${ destPath } .map` ;
112- map . sources = [ path . relative ( path . dirname ( destPath ) , file ) ] ;
113- fs . writeFileSync ( `${ destPath } .map` , JSON . stringify ( map ) ) ;
113+ if ( ! file . endsWith ( '.d.ts' ) ) {
114+ const outDir = path . dirname ( destPath ) ;
115+ const outFile = path . basename ( destPath ) ;
116+ const mapFileName = `${ outFile } .map` ;
117+ const mapPath = path . join ( outDir , mapFileName ) ;
118+
119+ // Build a minimal map if Babel didn't provide one
120+ if ( ! map || ! map . version ) {
121+ map = {
122+ version : 3 ,
123+ file : outFile ,
124+ sources : [ path . relative ( outDir , file ) . replace ( / \\ / g, '/' ) ] ,
125+ sourcesContent : [ fs . readFileSync ( file , 'utf8' ) ] ,
126+ names : [ ] ,
127+ mappings : '' , // empty mappings is valid; keeps tools happy
128+ } ;
129+ } else {
130+ // Normalize/override key fields for consistency
131+ map . file = outFile ;
132+ map . sources = [
133+ path . relative ( outDir , file ) . replace ( / \\ / g, '/' ) ,
134+ ] ;
135+ }
136+
137+ code = `${ code } \n\n//# sourceMappingURL=${ mapFileName } ` ;
138+ fs . writeFileSync ( mapPath , JSON . stringify ( map ) ) ;
114139 }
115140
116141 fs . writeFileSync ( destPath , code ) ;
0 commit comments