File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change 1
- const transform = require ( '@babel/core' ) . transform ;
1
+ const transformSync = require ( '@babel/core' ) . transformSync ;
2
2
3
- // use babel to translate native es module into AMD module
4
- module . exports = function es ( fileName , fileContents ) {
5
- return transform ( fileContents , {
3
+ /**
4
+ * Use babel to translate native es module into AMD module
5
+ * @param {string } fileName
6
+ * @param {string } fileContents
7
+ * @param {{} | boolean } inputSourceMap
8
+ * @returns {{ code: string, map: string, ast: any } }
9
+ */
10
+ module . exports = function es ( fileName , fileContents , inputSourceMap ) {
11
+ return transformSync ( fileContents , {
6
12
babelrc : false ,
7
- plugins : [ [ '@babel/plugin-transform-modules-amd' , { loose : true } ] ]
8
- } ) . code ;
13
+ plugins : [ [ '@babel/plugin-transform-modules-amd' , { loose : true } ] ] ,
14
+ inputSourceMap : inputSourceMap
15
+ } ) ;
9
16
} ;
Original file line number Diff line number Diff line change @@ -230,7 +230,17 @@ exports.BundledSource = class {
230
230
} catch {
231
231
// file is not in amd/cjs format, try native es module
232
232
try {
233
- contents = esTransform ( modulePath , this . contents ) ;
233
+ /** @type {{} | boolean } */
234
+ let inputSourceMap = false ;
235
+ if ( this . file . sourceMap ) {
236
+ inputSourceMap = typeof this . file . sourceMap === 'string' ? JSON . parse ( this . file . sourceMap ) : this . file . sourceMap ;
237
+ }
238
+ const result = esTransform ( modulePath , this . contents , inputSourceMap ) ;
239
+
240
+ contents = result . code ;
241
+ if ( result . map ) {
242
+ this . file . sourceMap = result . map ; // save updated source map
243
+ }
234
244
} catch ( e ) {
235
245
logger . error ( 'Could not convert to AMD module, skipping ' + modulePath ) ;
236
246
logger . error ( 'Error was: ' + e ) ;
You can’t perform that action at this time.
0 commit comments