Skip to content

Commit

Permalink
fix: generate absolute sources for source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 24, 2020
1 parent a838317 commit 887522c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Expand Up @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/utils.js
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions test/sourceMap-options.test.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 887522c

Please sign in to comment.