From 5e0565d1199bcf42c238657f48460727680a2864 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sun, 2 Sep 2018 16:29:51 -0700 Subject: [PATCH] Manually fix 'inline' sourcemaps so they work with Webpack. (#671) --- src/index.js | 10 +++++++++ test/sourcemaps.test.js | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/index.js b/src/index.js index f36bf496..e54bb602 100644 --- a/src/index.js +++ b/src/index.js @@ -128,6 +128,16 @@ async function loader(source, inputSourceMap, overrides) { }); } + if (options.sourceMaps === "inline") { + // Babel has this weird behavior where if you set "inline", we + // inline the sourcemap, and set 'result.map = null'. This results + // in bad behavior from Babel since the maps get put into the code, + // which Webpack does not expect, and because the map we return to + // Webpack is null, which is also bad. To avoid that, we override the + // behavior here so "inline" just behaves like 'true'. + options.sourceMaps = true; + } + const { cacheDirectory = null, cacheIdentifier = JSON.stringify({ diff --git a/test/sourcemaps.test.js b/test/sourcemaps.test.js index d15f5f4e..7239886e 100644 --- a/test/sourcemaps.test.js +++ b/test/sourcemaps.test.js @@ -73,6 +73,53 @@ test.cb("should output webpack's sourcemap", t => { }); }); +test.cb("should output webpack's sourcemap properly when set 'inline'", t => { + const config = Object.assign({}, globalConfig, { + devtool: "source-map", + output: { + path: t.context.directory, + }, + module: { + rules: [ + { + test: /\.jsx?/, + loader: babelLoader + "?presets[]=@babel/env&sourceMap=inline", + exclude: /node_modules/, + }, + ], + }, + }); + + webpack(config, (err, stats) => { + t.is(err, null); + t.is(stats.compilation.errors.length, 0); + t.is(stats.compilation.warnings.length, 0); + + fs.readdir(t.context.directory, (err, files) => { + t.is(err, null); + + const map = files.filter(file => file.indexOf(".map") !== -1); + + t.true(map.length > 0); + + if (map.length > 0) { + fs.readFile(path.resolve(t.context.directory, map[0]), (err, data) => { + t.is(err, null); + + const mapObj = JSON.parse(data.toString()); + + t.is(mapObj.sources[1], "webpack:///./test/fixtures/basic.js"); + + // Ensure that the map contains the original code, not the + // compiled src. + t.is(mapObj.sourcesContent[1].indexOf("__esModule"), -1); + t.end(); + }); + } + }); + }); +}); + test.cb("should output webpack's devtoolModuleFilename option", t => { const config = Object.assign({}, globalConfig, { devtool: "source-map",