Skip to content

Commit e5c25af

Browse files
authored
Merge pull request #374 from jtangelder/refactor/source-map
Improve source map support
2 parents ca7c4a1 + ce198d1 commit e5c25af

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

lib/loader.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function sassLoader(content) {
2727
const resourcePath = this.resourcePath;
2828

2929
function addNormalizedDependency(file) {
30-
// node-sass returns UNIX-style paths
30+
// node-sass returns POSIX paths
3131
self.dependency(path.normalize(file));
3232
}
3333

@@ -57,8 +57,12 @@ function sassLoader(content) {
5757
result.map.file = resourcePath;
5858
// The first source is 'stdin' according to libsass because we've used the data input
5959
// Now let's override that value with the correct relative path
60-
result.map.sources[0] = path.relative(self.options.context, resourcePath);
61-
result.map.sourceRoot = path.relative(self.options.context, process.cwd());
60+
result.map.sources[0] = path.basename(resourcePath);
61+
result.map.sourceRoot = path.dirname(resourcePath);
62+
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
63+
// This fixes an error on windows where the source-map module cannot resolve the source maps.
64+
// @see https://github.com/jtangelder/sass-loader/issues/366#issuecomment-279460722
65+
result.map.sources = result.map.sources.map(path.normalize);
6266
} else {
6367
result.map = null;
6468
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"scripts": {
77
"create-spec": "node test/tools/runCreateSpec.js",
88
"pretest": "node test/tools/runCreateSpec.js",
9-
"test": "mocha -R spec",
9+
"test": "mocha -R spec -t 10000",
1010
"posttest": "eslint --fix lib test",
1111
"test-bootstrap-sass": "webpack-dev-server --config test/bootstrapSass/webpack.config.js --content-base ./test/bootstrapSass",
12-
"test-source-map": "webpack-dev-server --config test/sourceMap/webpack.config.js --content-base ./test/sourceMap",
12+
"test-source-map": "webpack-dev-server --config test/sourceMap/webpack.config.js --content-base ./test/sourceMap --inline",
1313
"test-watch": "webpack --config test/watch/webpack.config.js",
1414
"test-extract-text": "webpack --config test/extractText/webpack.config.js",
1515
"test-hmr": "webpack-dev-server --config test/hmr/webpack.config.js --content-base ./test/hmr --hot --inline"

test/index.test.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe("sass-loader", () => {
113113
]
114114
}]
115115
}
116-
}, (err) => err ? reject(err) : resolve());
116+
}, err => err ? reject(err) : resolve());
117117
})
118118
.then(() => {
119119
const expectedCss = readCss("scss", "imports");
@@ -137,6 +137,34 @@ describe("sass-loader", () => {
137137
})
138138
);
139139
});
140+
describe("source maps", () => {
141+
it("should compile without errors", () =>
142+
new Promise((resolve, reject) => {
143+
runWebpack({
144+
entry: path.join(__dirname, "scss", "imports.scss"),
145+
// We know that setting a custom context can confuse webpack when resolving source maps
146+
context: path.join(__dirname, "scss"),
147+
output: {
148+
filename: "bundle.source-maps.compile-without-errors.js"
149+
},
150+
devtool: "source-map",
151+
module: {
152+
rules: [{
153+
test: /\.scss$/,
154+
use: [
155+
{ loader: "css-loader", options: {
156+
sourceMap: true
157+
} },
158+
{ loader: pathToSassLoader, options: {
159+
sourceMap: true
160+
} }
161+
]
162+
}]
163+
}
164+
}, err => err ? reject(err) : resolve());
165+
})
166+
);
167+
});
140168
describe("errors", () => {
141169
it("should throw an error in synchronous loader environments", () => {
142170
try {
@@ -209,8 +237,8 @@ function runWebpack(baseConfig, done) {
209237

210238
webpack(webpackConfig, (webpackErr, stats) => {
211239
const err = webpackErr ||
212-
(stats.hasErrors && stats.compilation.errors[0]) ||
213-
(stats.hasWarnings && stats.compilation.warnings[0]);
240+
(stats.hasErrors() && stats.compilation.errors[0]) ||
241+
(stats.hasWarnings() && stats.compilation.warnings[0]);
214242

215243
done(err || null);
216244
});

test/scss/imports.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
@import "~animate.css/animate";
1414
/* @import url(http://example.com/something/from/the/interwebs); */
1515
@import url(http://example.com/something/from/the/interwebs);
16+
/* scoped import @import "language"; */
17+
.scoped-import {
18+
@import "language";
19+
}

test/sourceMap/entry.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/sourceMap/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<head>
44
<title></title>
55
<meta charset="utf-8"/>
6-
<script async src="/bundle.sourcemap.js"></script>
6+
<script async src="/bundle.js"></script>
77
</head>
8-
<body>
8+
<body class="scoped-import">
99
<h1 class="another-scss-module box">Open the developer tools, dude!</h1>
1010
</body>
1111
</html>

test/sourceMap/webpack.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const path = require("path");
44
const sassLoader = require.resolve("../../lib/loader");
55

66
module.exports = {
7-
entry: path.resolve(__dirname, "./entry.js"),
7+
entry: path.resolve(__dirname, "..", "scss", "imports.scss"),
88
output: {
9-
path: path.resolve(__dirname, "../output"),
10-
filename: "bundle.sourceMap.js"
9+
filename: "bundle.js"
1110
},
1211
devtool: "source-map",
1312
module: {

0 commit comments

Comments
 (0)