diff --git a/src/webpack/index.js b/src/webpack/index.js index d736bc92d..b545e2b1a 100644 --- a/src/webpack/index.js +++ b/src/webpack/index.js @@ -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'); @@ -11,6 +11,7 @@ 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); @@ -18,12 +19,12 @@ function transform(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.', ); } @@ -31,7 +32,7 @@ function transform(source, map) { 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); } @@ -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;