Skip to content

Commit

Permalink
fix: source map consumer could be synchronious
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jun 10, 2019
1 parent ed53dcb commit 05a6c8e
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/webpack/index.js
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs');
const path = require('path');
const { SourceNode, SourceMapConsumer } = require('source-map');
const {SourceNode, SourceMapConsumer} = require('source-map');
const loaderUtils = require('loader-utils');
const makeIdentitySourceMap = require('./makeIdentitySourceMap');
const patch = require('./patch');
Expand All @@ -11,27 +11,28 @@ let tagCommonJSExportsSource = null;

function transform(source, map) {
const callback = this.async();
const resourcePath = this.resourcePath;

if (process.env.NODE_ENV === 'production') {
return callback(null, source, map);
}
if (source && source.types && source.types.IfStatement) {
throw new Error(
'React Hot Loader: You are erroneously trying to use a Webpack loader ' +
'as a Babel plugin. Replace "react-hot-loader/webpack" with ' +
'"react-hot-loader/babel" in the "plugins" section of your .babelrc file. ' +
'While we recommend the above, if you prefer not to use Babel, ' +
'you may remove "react-hot-loader/webpack" from the "plugins" section of ' +
'your .babelrc file altogether, and instead add "react-hot-loader/webpack" ' +
'to the "loaders" section of your Webpack configuration.',
'as a Babel plugin. Replace "react-hot-loader/webpack" with ' +
'"react-hot-loader/babel" in the "plugins" section of your .babelrc file. ' +
'While we recommend the above, if you prefer not to use Babel, ' +
'you may remove "react-hot-loader/webpack" from the "plugins" section of ' +
'your .babelrc file altogether, and instead add "react-hot-loader/webpack" ' +
'to the "loaders" section of your Webpack configuration.',
);
}

if (this.cacheable) {
this.cacheable();
}

const options = Object.assign({ withPatch: true }, loaderUtils.getOptions(this));
const options = Object.assign({withPatch: true}, loaderUtils.getOptions(this));
if (options.withPatch) {
source = patch(source);
}
Expand All @@ -56,24 +57,31 @@ function transform(source, map) {

// Parameterize the helper with the current filename.
const separator = '\n\n;';
const appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(this.resourcePath));
const appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(resourcePath));

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

if (!map) {
map = makeIdentitySourceMap(source, this.resourcePath); // eslint-disable-line no-param-reassign
map = makeIdentitySourceMap(source, resourcePath); // eslint-disable-line no-param-reassign
}
const sourceMapConsumer = new SourceMapConsumer(map);
sourceMapConsumer.then(consumedMap => {

const onSourceMapReady = consumedMap => {
const node = new SourceNode(null, null, null, [
SourceNode.fromStringWithSourceMap(source, consumedMap),
new SourceNode(null, null, this.resourcePath, appendText),
new SourceNode(null, null, resourcePath, appendText),
]).join(separator);
const result = node.toStringWithSourceMap();
callback(null, result.code, result.map.toJSON() || undefined);
});
};

if (sourceMapConsumer.then) {
sourceMapConsumer.then(onSourceMapReady);
} else {
onSourceMapReady(sourceMapConsumer);
}
}

transform.patch = patch;
Expand Down

0 comments on commit 05a6c8e

Please sign in to comment.