@@ -7,84 +7,97 @@ const replaceExt = require('replace-ext');
7
7
const babel = require ( '@babel/core' ) ;
8
8
9
9
function replaceExtension ( fp ) {
10
- return path . extname ( fp ) ? replaceExt ( fp , '.js' ) : fp ;
10
+ return path . extname ( fp ) ? replaceExt ( fp , '.js' ) : fp ;
11
11
}
12
12
13
13
module . exports = function ( opts ) {
14
- opts = opts || { } ;
14
+ opts = opts || { } ;
15
15
16
- return through . obj ( function ( file , enc , cb ) {
17
- if ( file . isNull ( ) ) {
18
- cb ( null , file ) ;
19
- return ;
20
- }
16
+ return through . obj ( function ( file , enc , cb ) {
17
+ if ( file . isNull ( ) ) {
18
+ cb ( null , file ) ;
19
+ return ;
20
+ }
21
21
22
- if ( file . isStream ( ) ) {
23
- cb ( new PluginError ( 'gulp-babel' , 'Streaming not supported' ) ) ;
24
- return ;
25
- }
22
+ if ( file . isStream ( ) ) {
23
+ cb ( new PluginError ( 'gulp-babel' , 'Streaming not supported' ) ) ;
24
+ return ;
25
+ }
26
26
27
- if ( ! supportsCallerOption ( ) ) {
28
- cb ( new PluginError ( 'gulp-babel' , '@babel/core@^7.0.0 is required' ) ) ;
29
- return ;
30
- }
27
+ if ( ! supportsCallerOption ( ) ) {
28
+ cb ( new PluginError ( 'gulp-babel' , '@babel/core@^7.0.0 is required' ) ) ;
29
+ return ;
30
+ }
31
31
32
- const fileOpts = Object . assign ( { } , opts , {
33
- filename : file . path ,
34
- filenameRelative : file . relative ,
35
- sourceMap : Boolean ( file . sourceMap ) ,
36
- sourceFileName : file . relative ,
37
- caller : Object . assign (
38
- { name : 'babel-gulp' } ,
39
- opts . caller
40
- )
41
- } ) ;
42
32
43
- babel . transformAsync ( file . contents . toString ( ) , fileOpts ) . then ( res => {
44
- if ( res ) {
45
- if ( file . sourceMap && res . map ) {
46
- res . map . file = replaceExtension ( file . relative ) ;
47
- applySourceMap ( file , res . map ) ;
48
- }
33
+ const isInputSourceMapPresent = Boolean ( file . sourceMap ) ;
34
+ const defaultOpts = {
35
+ filename : file . path ,
36
+ filenameRelative : file . relative ,
37
+ caller : Object . assign (
38
+ { name : 'babel-gulp' } ,
39
+ opts . caller
40
+ )
41
+ } ;
49
42
50
- file . contents = Buffer . from ( res . code ) ;
51
- file . path = replaceExtension ( file . path ) ;
43
+ if ( isInputSourceMapPresent ) {
44
+ defaultOpts . inputSourceMap = file . sourceMap ;
45
+ }
46
+ else {
47
+ defaultOpts . sourceFileName = file . relative ;
48
+ }
52
49
53
- file . babel = res . metadata ;
54
- }
50
+ const fileOpts = Object . assign ( { } , opts , defaultOpts ) ;
55
51
56
- this . push ( file ) ;
57
- } ) . catch ( error => {
58
- this . emit ( 'error' , new PluginError ( 'gulp-babel' , error , {
59
- fileName : file . path ,
60
- showProperties : false
61
- } ) ) ;
62
- } ) . then (
63
- ( ) => cb ( ) ,
64
- ( ) => cb ( )
65
- ) ;
66
- } ) ;
52
+ babel . transformAsync ( file . contents . toString ( ) , fileOpts ) . then ( res => {
53
+ if ( res ) {
54
+ if ( file . sourceMap && res . map ) {
55
+ if ( isInputSourceMapPresent ) {
56
+ file . sourceMap = res . map ;
57
+ } else {
58
+ res . map . file = replaceExtension ( file . relative ) ;
59
+ applySourceMap ( file , res . map ) ;
60
+ }
61
+ }
62
+
63
+ file . contents = Buffer . from ( res . code ) ;
64
+ file . path = replaceExtension ( file . path ) ;
65
+
66
+ file . babel = res . metadata ;
67
+ }
68
+
69
+ this . push ( file ) ;
70
+ } ) . catch ( error => {
71
+ this . emit ( 'error' , new PluginError ( 'gulp-babel' , error , {
72
+ fileName : file . path ,
73
+ showProperties : false
74
+ } ) ) ;
75
+ } ) . then (
76
+ ( ) => cb ( ) ,
77
+ ( ) => cb ( )
78
+ ) ;
79
+ } ) ;
67
80
} ;
68
81
69
82
// Note: We can remove this eventually, I'm just adding it so that people have
70
83
// a little time to migrate to the newer RCs of @babel /core without getting
71
84
// hard-to-diagnose errors about unknown 'caller' options.
72
85
let supportsCallerOptionFlag ;
73
86
function supportsCallerOption ( ) {
74
- if ( supportsCallerOptionFlag === undefined ) {
75
- try {
76
- // Rather than try to match the Babel version, we just see if it throws
77
- // when passed a 'caller' flag, and use that to decide if it is supported.
78
- babel . loadPartialConfig ( {
79
- caller : undefined ,
80
- babelrc : false ,
81
- configFile : false
82
- } ) ;
83
- supportsCallerOptionFlag = true ;
84
- } catch ( _ ) {
85
- supportsCallerOptionFlag = false ;
86
- }
87
- }
87
+ if ( supportsCallerOptionFlag === undefined ) {
88
+ try {
89
+ // Rather than try to match the Babel version, we just see if it throws
90
+ // when passed a 'caller' flag, and use that to decide if it is supported.
91
+ babel . loadPartialConfig ( {
92
+ caller : undefined ,
93
+ babelrc : false ,
94
+ configFile : false
95
+ } ) ;
96
+ supportsCallerOptionFlag = true ;
97
+ } catch ( _ ) {
98
+ supportsCallerOptionFlag = false ;
99
+ }
100
+ }
88
101
89
- return supportsCallerOptionFlag ;
102
+ return supportsCallerOptionFlag ;
90
103
}
0 commit comments