diff --git a/src/index.js b/src/index.js index 9932b012..e2027374 100644 --- a/src/index.js +++ b/src/index.js @@ -23,8 +23,6 @@ const pkg = require("../package.json"); const cache = require("./cache"); const transform = require("./transform"); -const relative = require("./utils/relative"); - const loaderUtils = require("loader-utils"); function subscribe(subscriber, metadata, context) { @@ -77,19 +75,21 @@ async function loader(source, inputSourceMap, overrides) { ); } - // Set babel-loader's default options. - const { - sourceRoot = process.cwd(), - sourceMap = this.sourceMap, - sourceFileName = relative(sourceRoot, filename), - } = loaderOptions; - const programmaticOptions = Object.assign({}, loaderOptions, { filename, inputSourceMap: inputSourceMap || undefined, - sourceRoot, - sourceMap, - sourceFileName, + + // Set the default sourcemap behavior based on Webpack's mapping flag, + // but allow users to override if they want. + sourceMap: + loaderOptions.sourceMap === undefined + ? this.sourceMap + : loaderOptions.sourceMap, + + // Ensure that Webpack will get a full absolute path in the sourcemap + // so that it can properly map the module back to its internal cached + // modules. + sourceFileName: filename, }); // Remove loader related options delete programmaticOptions.cacheDirectory; diff --git a/src/utils/relative.js b/src/utils/relative.js deleted file mode 100644 index 1321e02d..00000000 --- a/src/utils/relative.js +++ /dev/null @@ -1,14 +0,0 @@ -const path = require("path"); - -module.exports = function relative(root, file) { - const rootPath = root.replace(/\\/g, "/").split("/")[1]; - const filePath = file.replace(/\\/g, "/").split("/")[1]; - - // If the file is in a completely different root folder - // use the absolute path of the file - if (rootPath && rootPath !== filePath) { - return file; - } - - return path.relative(root, file); -}; diff --git a/test/sourcemaps.test.js b/test/sourcemaps.test.js index 0b451ae5..10f470fa 100644 --- a/test/sourcemaps.test.js +++ b/test/sourcemaps.test.js @@ -72,3 +72,47 @@ test.cb("should output webpack's sourcemap", t => { }); }); }); + +test.cb("should output webpack's devtoolModuleFilename option", t => { + const config = Object.assign({}, globalConfig, { + devtool: "source-map", + output: { + path: t.context.directory, + devtoolModuleFilenameTemplate: "==[absolute-resource-path]==", + }, + module: { + rules: [ + { + test: /\.jsx?/, + loader: babelLoader + "?presets[]=@babel/env", + 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); + + // The full absolute path is included in the sourcemap properly + t.not(data.toString().indexOf(`==${globalConfig.entry}==`), -1); + + t.end(); + }); + } + }); + }); +}); diff --git a/test/utils/relative.test.js b/test/utils/relative.test.js deleted file mode 100644 index 96183b2d..00000000 --- a/test/utils/relative.test.js +++ /dev/null @@ -1,37 +0,0 @@ -import test from "ava"; -import os from "os"; -import relative from "../../lib/utils/relative.js"; - -if (os.platform() === "win32") { - test("should get correct relative path - depth 0 - windows", t => { - t.is(relative("C:\\the\\root", "C:\\the\\root\\one.js"), "one.js"); - }); - - test("should get correct relative path - depth 1 - windows", t => { - t.is(relative("C:\\the\\root", "C:\\the\\rootone.js"), "..\\rootone.js"); - }); - - test("should get correct relative path - depth 2 - windows", t => { - t.is(relative("C:\\the\\root", "C:\\therootone.js"), "C:\\therootone.js"); - }); - - test("should get correct relative path with main root - depth 0 - windows", t => { - t.is(relative("C:\\", "C:\\the\\root\\one.js"), "the\\root\\one.js"); - }); -} else { - test("should get correct relative path - depth 0", t => { - t.is(relative("/the/root", "/the/root/one.js"), "one.js"); - }); - - test("should get correct relative path - depth 1", t => { - t.is(relative("/the/root", "/the/rootone.js"), "../rootone.js"); - }); - - test("should get correct relative path - depth 2", t => { - t.is(relative("/the/root", "/therootone.js"), "/therootone.js"); - }); - - test("should get correct relative path with main root - depth 0", t => { - t.is(relative("/", "/the/root/one.js"), "the/root/one.js"); - }); -}