Skip to content

Commit 05a6c8e

Browse files
committed
fix: source map consumer could be synchronious
1 parent ed53dcb commit 05a6c8e

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Diff for: src/webpack/index.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const { SourceNode, SourceMapConsumer } = require('source-map');
5+
const {SourceNode, SourceMapConsumer} = require('source-map');
66
const loaderUtils = require('loader-utils');
77
const makeIdentitySourceMap = require('./makeIdentitySourceMap');
88
const patch = require('./patch');
@@ -11,27 +11,28 @@ let tagCommonJSExportsSource = null;
1111

1212
function transform(source, map) {
1313
const callback = this.async();
14+
const resourcePath = this.resourcePath;
1415

1516
if (process.env.NODE_ENV === 'production') {
1617
return callback(null, source, map);
1718
}
1819
if (source && source.types && source.types.IfStatement) {
1920
throw new Error(
2021
'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.',
2728
);
2829
}
2930

3031
if (this.cacheable) {
3132
this.cacheable();
3233
}
3334

34-
const options = Object.assign({ withPatch: true }, loaderUtils.getOptions(this));
35+
const options = Object.assign({withPatch: true}, loaderUtils.getOptions(this));
3536
if (options.withPatch) {
3637
source = patch(source);
3738
}
@@ -56,24 +57,31 @@ function transform(source, map) {
5657

5758
// Parameterize the helper with the current filename.
5859
const separator = '\n\n;';
59-
const appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(this.resourcePath));
60+
const appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(resourcePath));
6061

6162
if (this.sourceMap === false) {
6263
return callback(null, [source, appendText].join(separator));
6364
}
6465

6566
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
6768
}
6869
const sourceMapConsumer = new SourceMapConsumer(map);
69-
sourceMapConsumer.then(consumedMap => {
70+
71+
const onSourceMapReady = consumedMap => {
7072
const node = new SourceNode(null, null, null, [
7173
SourceNode.fromStringWithSourceMap(source, consumedMap),
72-
new SourceNode(null, null, this.resourcePath, appendText),
74+
new SourceNode(null, null, resourcePath, appendText),
7375
]).join(separator);
7476
const result = node.toStringWithSourceMap();
7577
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+
}
7785
}
7886

7987
transform.patch = patch;

0 commit comments

Comments
 (0)