Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fcea88e

Browse files
authoredAug 23, 2019
feat: source map generation depends on the devtool option (#743)
BREAKING CHANGE: default value of the `sourceMap` option depends on the `devtool` value (`eval`/`false` values don't enable source map generation)
1 parent 33a23f8 commit fcea88e

File tree

5 files changed

+157
-34
lines changed

5 files changed

+157
-34
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ module.exports = {
363363
### `sourceMap`
364364

365365
Type: `Boolean`
366-
Default: `false`
366+
Default: depends on the `compiler.devtool` value
367367

368368
Enables/Disables generation of source maps.
369369

370-
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
370+
By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option, all values enable source map generation except `eval` and `false` value.
371371

372372
**webpack.config.js**
373373

‎src/getSassOptions.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ function getSassOptions(loaderContext, loaderOptions, content) {
4141
options.outputStyle = 'compressed';
4242
}
4343

44+
const useSourceMap =
45+
typeof loaderOptions.sourceMap === 'boolean'
46+
? loaderOptions.sourceMap
47+
: loaderContext.sourceMap;
48+
4449
// opt.sourceMap
4550
// Not using the `this.sourceMap` flag because css source maps are different
4651
// @see https://github.com/webpack/css-loader/pull/40
47-
if (loaderOptions.sourceMap) {
52+
if (useSourceMap) {
4853
// Deliberately overriding the sourceMap option here.
4954
// node-sass won't produce source maps if the data option is used and options.sourceMap is not a string.
5055
// In case it is a string, options.sourceMap should be a path where the source map is written.

‎src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function loader(content) {
6262
render(sassOptions, (error, result) => {
6363
if (error) {
6464
if (error.file) {
65-
this.dependency(error.file);
65+
addNormalizedDependency(error.file);
6666
}
6767

6868
callback(new SassError(error, this.resourcePath));
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,97 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`sourceMap option false (dart-sass) (sass): errors 1`] = `Array []`;
3+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (dart-sass) (sass): errors 1`] = `Array []`;
44

5-
exports[`sourceMap option false (dart-sass) (sass): warnings 1`] = `Array []`;
5+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (dart-sass) (sass): warnings 1`] = `Array []`;
66

7-
exports[`sourceMap option false (dart-sass) (scss): errors 1`] = `Array []`;
7+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (dart-sass) (scss): errors 1`] = `Array []`;
88

9-
exports[`sourceMap option false (dart-sass) (scss): warnings 1`] = `Array []`;
9+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (dart-sass) (scss): warnings 1`] = `Array []`;
1010

11-
exports[`sourceMap option false (node-sass) (sass): errors 1`] = `Array []`;
11+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (node-sass) (sass): errors 1`] = `Array []`;
1212

13-
exports[`sourceMap option false (node-sass) (sass): warnings 1`] = `Array []`;
13+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (node-sass) (sass): warnings 1`] = `Array []`;
1414

15-
exports[`sourceMap option false (node-sass) (scss): errors 1`] = `Array []`;
15+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (node-sass) (scss): errors 1`] = `Array []`;
1616

17-
exports[`sourceMap option false (node-sass) (scss): warnings 1`] = `Array []`;
17+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "false" value (node-sass) (scss): warnings 1`] = `Array []`;
1818

19-
exports[`sourceMap option not specify (dart-sass) (sass): errors 1`] = `Array []`;
19+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (dart-sass) (sass): errors 1`] = `Array []`;
2020

21-
exports[`sourceMap option not specify (dart-sass) (sass): warnings 1`] = `Array []`;
21+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (dart-sass) (sass): warnings 1`] = `Array []`;
2222

23-
exports[`sourceMap option not specify (dart-sass) (scss): errors 1`] = `Array []`;
23+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (dart-sass) (scss): errors 1`] = `Array []`;
2424

25-
exports[`sourceMap option not specify (dart-sass) (scss): warnings 1`] = `Array []`;
25+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (dart-sass) (scss): warnings 1`] = `Array []`;
2626

27-
exports[`sourceMap option not specify (node-sass) (sass): errors 1`] = `Array []`;
27+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (node-sass) (sass): errors 1`] = `Array []`;
2828

29-
exports[`sourceMap option not specify (node-sass) (sass): warnings 1`] = `Array []`;
29+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (node-sass) (sass): warnings 1`] = `Array []`;
3030

31-
exports[`sourceMap option not specify (node-sass) (scss): errors 1`] = `Array []`;
31+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (node-sass) (scss): errors 1`] = `Array []`;
3232

33-
exports[`sourceMap option not specify (node-sass) (scss): warnings 1`] = `Array []`;
33+
exports[`sourceMap option should generate source maps when value has "true" value and the "devtool" option has "source-map" value (node-sass) (scss): warnings 1`] = `Array []`;
3434

35-
exports[`sourceMap option true (dart-sass) (sass): errors 1`] = `Array []`;
35+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (dart-sass) (sass): errors 1`] = `Array []`;
3636

37-
exports[`sourceMap option true (dart-sass) (sass): warnings 1`] = `Array []`;
37+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (dart-sass) (sass): warnings 1`] = `Array []`;
3838

39-
exports[`sourceMap option true (dart-sass) (scss): errors 1`] = `Array []`;
39+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (dart-sass) (scss): errors 1`] = `Array []`;
4040

41-
exports[`sourceMap option true (dart-sass) (scss): warnings 1`] = `Array []`;
41+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (dart-sass) (scss): warnings 1`] = `Array []`;
4242

43-
exports[`sourceMap option true (node-sass) (sass): errors 1`] = `Array []`;
43+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (node-sass) (sass): errors 1`] = `Array []`;
4444

45-
exports[`sourceMap option true (node-sass) (sass): warnings 1`] = `Array []`;
45+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (node-sass) (sass): warnings 1`] = `Array []`;
4646

47-
exports[`sourceMap option true (node-sass) (scss): errors 1`] = `Array []`;
47+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (node-sass) (scss): errors 1`] = `Array []`;
4848

49-
exports[`sourceMap option true (node-sass) (scss): warnings 1`] = `Array []`;
49+
exports[`sourceMap option should generate source maps when value is not specify and the "devtool" option has "source-map" value (node-sass) (scss): warnings 1`] = `Array []`;
50+
51+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (dart-sass) (sass): errors 1`] = `Array []`;
52+
53+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (dart-sass) (sass): warnings 1`] = `Array []`;
54+
55+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (dart-sass) (scss): errors 1`] = `Array []`;
56+
57+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (dart-sass) (scss): warnings 1`] = `Array []`;
58+
59+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (node-sass) (sass): errors 1`] = `Array []`;
60+
61+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (node-sass) (sass): warnings 1`] = `Array []`;
62+
63+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (node-sass) (scss): errors 1`] = `Array []`;
64+
65+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "false" value (node-sass) (scss): warnings 1`] = `Array []`;
66+
67+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (dart-sass) (sass): errors 1`] = `Array []`;
68+
69+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (dart-sass) (sass): warnings 1`] = `Array []`;
70+
71+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (dart-sass) (scss): errors 1`] = `Array []`;
72+
73+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (dart-sass) (scss): warnings 1`] = `Array []`;
74+
75+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (node-sass) (sass): errors 1`] = `Array []`;
76+
77+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (node-sass) (sass): warnings 1`] = `Array []`;
78+
79+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (node-sass) (scss): errors 1`] = `Array []`;
80+
81+
exports[`sourceMap option should not generate source maps when value has "false" and the "devtool" option has "source-map" value (node-sass) (scss): warnings 1`] = `Array []`;
82+
83+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (dart-sass) (sass): errors 1`] = `Array []`;
84+
85+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (dart-sass) (sass): warnings 1`] = `Array []`;
86+
87+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (dart-sass) (scss): errors 1`] = `Array []`;
88+
89+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (dart-sass) (scss): warnings 1`] = `Array []`;
90+
91+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (node-sass) (sass): errors 1`] = `Array []`;
92+
93+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (node-sass) (sass): warnings 1`] = `Array []`;
94+
95+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (node-sass) (scss): errors 1`] = `Array []`;
96+
97+
exports[`sourceMap option should not generate source maps when value is not specify and the "devtool" option has "false" value (node-sass) (scss): warnings 1`] = `Array []`;

‎test/sourceMap-options.test.js

+76-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,28 @@ describe('sourceMap option', () => {
2222
syntaxStyles.forEach((syntax) => {
2323
const [implementationName] = implementation.info.split('\t');
2424

25-
it(`not specify (${implementationName}) (${syntax})`, async () => {
25+
it(`should generate source maps when value is not specify and the "devtool" option has "source-map" value (${implementationName}) (${syntax})`, async () => {
26+
const testId = getTestId('language', syntax);
27+
const options = {
28+
implementation: getImplementationByName(implementationName),
29+
};
30+
const stats = await compile(testId, {
31+
devtool: 'source-map',
32+
loader: { options },
33+
});
34+
const { css, sourceMap } = getCodeFromBundle(stats);
35+
36+
expect(css).toBeDefined();
37+
expect(sourceMap).toBeDefined();
38+
39+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
40+
expect(stats.compilation.errors).toMatchSnapshot('errors');
41+
});
42+
43+
it(`should not generate source maps when value is not specify and the "devtool" option has "false" value (${implementationName}) (${syntax})`, async () => {
2644
const testId = getTestId('language', syntax);
2745
const options = {
2846
implementation: getImplementationByName(implementationName),
29-
// eslint-disable-next-line no-undefined
30-
sourceMap: undefined,
3147
};
3248
const stats = await compile(testId, { loader: { options } });
3349
const { css, sourceMap } = getCodeFromBundle(stats);
@@ -39,7 +55,26 @@ describe('sourceMap option', () => {
3955
expect(stats.compilation.errors).toMatchSnapshot('errors');
4056
});
4157

42-
it(`false (${implementationName}) (${syntax})`, async () => {
58+
it(`should not generate source maps when value has "false" and the "devtool" option has "source-map" value (${implementationName}) (${syntax})`, async () => {
59+
const testId = getTestId('language', syntax);
60+
const options = {
61+
implementation: getImplementationByName(implementationName),
62+
sourceMap: false,
63+
};
64+
const stats = await compile(testId, {
65+
devtool: 'source-map',
66+
loader: { options },
67+
});
68+
const { css, sourceMap } = getCodeFromBundle(stats);
69+
70+
expect(css).toBeDefined();
71+
expect(sourceMap).toBeUndefined();
72+
73+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
74+
expect(stats.compilation.errors).toMatchSnapshot('errors');
75+
});
76+
77+
it(`should not generate source maps when value has "false" and the "devtool" option has "false" value (${implementationName}) (${syntax})`, async () => {
4378
const testId = getTestId('language', syntax);
4479
const options = {
4580
implementation: getImplementationByName(implementationName),
@@ -55,15 +90,50 @@ describe('sourceMap option', () => {
5590
expect(stats.compilation.errors).toMatchSnapshot('errors');
5691
});
5792

58-
it(`true (${implementationName}) (${syntax})`, async () => {
93+
it(`should generate source maps when value has "true" value and the "devtool" option has "source-map" value (${implementationName}) (${syntax})`, async () => {
5994
expect.assertions(9);
6095

6196
const testId = getTestId('language', syntax);
6297
const options = {
6398
implementation: getImplementationByName(implementationName),
6499
sourceMap: true,
65100
};
66-
const stats = await compile(testId, { loader: { options } });
101+
const stats = await compile(testId, {
102+
devtool: 'source-map',
103+
loader: { options },
104+
});
105+
const { css, sourceMap } = getCodeFromBundle(stats);
106+
107+
expect(css).toBeDefined();
108+
expect(sourceMap).toBeDefined();
109+
110+
expect(sourceMap.file).toBeUndefined();
111+
expect(sourceMap.sourceRoot).toBeDefined();
112+
113+
expect(sourceMap.sources).toHaveLength(2);
114+
115+
sourceMap.sources.forEach((sourcePath) => {
116+
expect(
117+
fs.existsSync(path.resolve(sourceMap.sourceRoot, sourcePath))
118+
).toBe(true);
119+
});
120+
121+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
122+
expect(stats.compilation.errors).toMatchSnapshot('errors');
123+
});
124+
125+
it(`should generate source maps when value has "true" value and the "devtool" option has "false" value (${implementationName}) (${syntax})`, async () => {
126+
expect.assertions(9);
127+
128+
const testId = getTestId('language', syntax);
129+
const options = {
130+
implementation: getImplementationByName(implementationName),
131+
sourceMap: true,
132+
};
133+
const stats = await compile(testId, {
134+
devtool: false,
135+
loader: { options },
136+
});
67137
const { css, sourceMap } = getCodeFromBundle(stats);
68138

69139
expect(css).toBeDefined();

0 commit comments

Comments
 (0)
Please sign in to comment.