From 887522c9e71e3835a705e881acb3dca1d14156c8 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 24 Aug 2020 13:44:07 +0300 Subject: [PATCH] fix: generate absolute sources for source maps --- src/index.js | 9 ++++++--- src/utils.js | 7 ++++--- test/sourceMap-options.test.js | 12 +++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 17435c4b..03cab154 100644 --- a/src/index.js +++ b/src/index.js @@ -67,13 +67,16 @@ function loader(content) { // eslint-disable-next-line no-param-reassign delete result.map.file; + // eslint-disable-next-line no-param-reassign + result.sourceRoot = ''; + // node-sass returns POSIX paths, that's why we need to transform them back to native paths. // This fixes an error on windows where the source-map module cannot resolve the source maps. // @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722 // eslint-disable-next-line no-param-reassign - result.map.sourceRoot = path.normalize(result.map.sourceRoot); - // eslint-disable-next-line no-param-reassign - result.map.sources = result.map.sources.map(path.normalize); + result.map.sources = result.map.sources.map((source) => + path.resolve(this.rootContext, path.normalize(source)) + ); } result.stats.includedFiles.forEach((includedFile) => { diff --git a/src/utils.js b/src/utils.js index 0464278d..9eef6dba 100644 --- a/src/utils.js +++ b/src/utils.js @@ -150,11 +150,12 @@ function getSassOptions(loaderContext, loaderOptions, content, implementation) { // But since we're using the data option, the source map will not actually be written, but // all paths in sourceMap.sources will be relative to that path. // Pretty complicated... :( - options.sourceMap = path.join(process.cwd(), '/sass.css.map'); - options.sourceMapRoot = process.cwd(); + options.sourceMap = true; + options.outFile = path.join(loaderContext.rootContext, 'style.css.map'); + // options.sourceMapRoot = process.cwd(); options.sourceMapContents = true; options.omitSourceMapUrl = true; - options.sourceMapEmbed = false; + // options.sourceMapEmbed = false; } const { resourcePath } = loaderContext; diff --git a/test/sourceMap-options.test.js b/test/sourceMap-options.test.js index f2a68b75..21925d48 100644 --- a/test/sourceMap-options.test.js +++ b/test/sourceMap-options.test.js @@ -42,7 +42,9 @@ describe('sourceMap option', () => { sourceMap.sourceRoot = ''; sourceMap.sources = sourceMap.sources.map((source) => - source.replace(/\\/g, '/') + path + .relative(path.resolve(__dirname, '..'), source) + .replace(/\\/g, '/') ); expect(css).toMatchSnapshot('css'); @@ -124,7 +126,9 @@ describe('sourceMap option', () => { sourceMap.sourceRoot = ''; sourceMap.sources = sourceMap.sources.map((source) => - source.replace(/\\/g, '/') + path + .relative(path.resolve(__dirname, '..'), source) + .replace(/\\/g, '/') ); expect(css).toMatchSnapshot('css'); @@ -157,7 +161,9 @@ describe('sourceMap option', () => { sourceMap.sourceRoot = ''; sourceMap.sources = sourceMap.sources.map((source) => - source.replace(/\\/g, '/') + path + .relative(path.resolve(__dirname, '..'), source) + .replace(/\\/g, '/') ); expect(css).toMatchSnapshot('css');