2
2
3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
- const { SourceNode, SourceMapConsumer } = require ( 'source-map' ) ;
5
+ const { SourceNode, SourceMapConsumer} = require ( 'source-map' ) ;
6
6
const loaderUtils = require ( 'loader-utils' ) ;
7
7
const makeIdentitySourceMap = require ( './makeIdentitySourceMap' ) ;
8
8
const patch = require ( './patch' ) ;
@@ -11,27 +11,28 @@ let tagCommonJSExportsSource = null;
11
11
12
12
function transform ( source , map ) {
13
13
const callback = this . async ( ) ;
14
+ const resourcePath = this . resourcePath ;
14
15
15
16
if ( process . env . NODE_ENV === 'production' ) {
16
17
return callback ( null , source , map ) ;
17
18
}
18
19
if ( source && source . types && source . types . IfStatement ) {
19
20
throw new Error (
20
21
'React Hot Loader: You are erroneously trying to use a Webpack loader ' +
21
- 'as a Babel plugin. Replace "react-hot-loader/webpack" with ' +
22
- '"react-hot-loader/babel" in the "plugins" section of your .babelrc file. ' +
23
- 'While we recommend the above, if you prefer not to use Babel, ' +
24
- 'you may remove "react-hot-loader/webpack" from the "plugins" section of ' +
25
- 'your .babelrc file altogether, and instead add "react-hot-loader/webpack" ' +
26
- 'to the "loaders" section of your Webpack configuration.' ,
22
+ 'as a Babel plugin. Replace "react-hot-loader/webpack" with ' +
23
+ '"react-hot-loader/babel" in the "plugins" section of your .babelrc file. ' +
24
+ 'While we recommend the above, if you prefer not to use Babel, ' +
25
+ 'you may remove "react-hot-loader/webpack" from the "plugins" section of ' +
26
+ 'your .babelrc file altogether, and instead add "react-hot-loader/webpack" ' +
27
+ 'to the "loaders" section of your Webpack configuration.' ,
27
28
) ;
28
29
}
29
30
30
31
if ( this . cacheable ) {
31
32
this . cacheable ( ) ;
32
33
}
33
34
34
- const options = Object . assign ( { withPatch : true } , loaderUtils . getOptions ( this ) ) ;
35
+ const options = Object . assign ( { withPatch : true } , loaderUtils . getOptions ( this ) ) ;
35
36
if ( options . withPatch ) {
36
37
source = patch ( source ) ;
37
38
}
@@ -56,24 +57,31 @@ function transform(source, map) {
56
57
57
58
// Parameterize the helper with the current filename.
58
59
const separator = '\n\n;' ;
59
- const appendText = tagCommonJSExportsSource . replace ( / _ _ F I L E N A M E _ _ / g, JSON . stringify ( this . resourcePath ) ) ;
60
+ const appendText = tagCommonJSExportsSource . replace ( / _ _ F I L E N A M E _ _ / g, JSON . stringify ( resourcePath ) ) ;
60
61
61
62
if ( this . sourceMap === false ) {
62
63
return callback ( null , [ source , appendText ] . join ( separator ) ) ;
63
64
}
64
65
65
66
if ( ! map ) {
66
- map = makeIdentitySourceMap ( source , this . resourcePath ) ; // eslint-disable-line no-param-reassign
67
+ map = makeIdentitySourceMap ( source , resourcePath ) ; // eslint-disable-line no-param-reassign
67
68
}
68
69
const sourceMapConsumer = new SourceMapConsumer ( map ) ;
69
- sourceMapConsumer . then ( consumedMap => {
70
+
71
+ const onSourceMapReady = consumedMap => {
70
72
const node = new SourceNode ( null , null , null , [
71
73
SourceNode . fromStringWithSourceMap ( source , consumedMap ) ,
72
- new SourceNode ( null , null , this . resourcePath , appendText ) ,
74
+ new SourceNode ( null , null , resourcePath , appendText ) ,
73
75
] ) . join ( separator ) ;
74
76
const result = node . toStringWithSourceMap ( ) ;
75
77
callback ( null , result . code , result . map . toJSON ( ) || undefined ) ;
76
- } ) ;
78
+ } ;
79
+
80
+ if ( sourceMapConsumer . then ) {
81
+ sourceMapConsumer . then ( onSourceMapReady ) ;
82
+ } else {
83
+ onSourceMapReady ( sourceMapConsumer ) ;
84
+ }
77
85
}
78
86
79
87
transform . patch = patch ;
0 commit comments