Skip to content

Commit f667a7b

Browse files
feat: the sourceMap default value depends on the devtool option (#444)
BREAKING CHANGE: the `sourceMap` default value depends on the `compiler.devtool` option
1 parent e966ab9 commit f667a7b

File tree

8 files changed

+428
-182
lines changed

8 files changed

+428
-182
lines changed

README.md

Lines changed: 249 additions & 140 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 59 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
"postcss-nested": "^4.2.3",
7676
"postcss-short": "^5.0.0",
7777
"prettier": "^2.0.5",
78+
"sass": "^1.26.10",
79+
"sass-loader": "^9.0.3",
7880
"standard": "^14.3.4",
7981
"standard-version": "^8.0.2",
8082
"strip-ansi": "^6.0.0",

src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ export default async function loader(content, sourceMap, meta = {}) {
9898

9999
const { parser, syntax, stringifier } = mergedOptions;
100100

101+
const useSourceMap =
102+
typeof options.sourceMap !== 'undefined'
103+
? options.sourceMap
104+
: this.sourceMap;
105+
101106
const postcssOptions = {
102107
from: file,
103-
map: options.sourceMap
108+
map: useSourceMap
104109
? options.sourceMap === 'inline'
105110
? { inline: true, annotation: false }
106111
: { inline: false, annotation: false }
@@ -110,6 +115,11 @@ export default async function loader(content, sourceMap, meta = {}) {
110115
stringifier,
111116
};
112117

118+
if (postcssOptions.map && sourceMap) {
119+
postcssOptions.map.prev =
120+
typeof sourceMap === 'string' ? JSON.parse(sourceMap) : sourceMap;
121+
}
122+
113123
// Loader Exec (Deprecated)
114124
// https://webpack.js.org/api/loaders/#deprecated-context-properties
115125
if (postcssOptions.parser === 'postcss-js') {
@@ -157,15 +167,6 @@ export default async function loader(content, sourceMap, meta = {}) {
157167
content = exec(content, this);
158168
}
159169

160-
if (options.sourceMap && typeof sourceMap === 'string') {
161-
// eslint-disable-next-line no-param-reassign
162-
sourceMap = JSON.parse(sourceMap);
163-
}
164-
165-
if (options.sourceMap && sourceMap) {
166-
postcssOptions.map.prev = sourceMap;
167-
}
168-
169170
let result;
170171

171172
try {

test/fixtures/scss/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import style from './style.scss'
2+
3+
export default style

test/fixtures/scss/style.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color: coral }

test/options/__snapshots__/sourceMap.test.js.snap

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,42 @@ exports[`Options Sourcemap should work Sourcemap - {String}: css 1`] = `
3434
exports[`Options Sourcemap should work Sourcemap - {String}: errors 1`] = `Array []`;
3535

3636
exports[`Options Sourcemap should work Sourcemap - {String}: warnings 1`] = `Array []`;
37+
38+
exports[`Options Sourcemap should work disable Sourcemap - {Boolean}: css 1`] = `
39+
"a { color: rgba(255, 0, 0, 1.0) }
40+
"
41+
`;
42+
43+
exports[`Options Sourcemap should work disable Sourcemap - {Boolean}: errors 1`] = `Array []`;
44+
45+
exports[`Options Sourcemap should work disable Sourcemap - {Boolean}: warnings 1`] = `Array []`;
46+
47+
exports[`Options Sourcemap should work with prev sourceMap: css 1`] = `
48+
"a {
49+
color: coral;
50+
}"
51+
`;
52+
53+
exports[`Options Sourcemap should work with prev sourceMap: errors 1`] = `Array []`;
54+
55+
exports[`Options Sourcemap should work with prev sourceMap: map 1`] = `
56+
Object {
57+
"file": "x",
58+
"mappings": "AAAA;EAAI,YAAA;ACEJ",
59+
"names": Array [],
60+
"sources": Array [
61+
"xxx",
62+
"xxx",
63+
],
64+
"sourcesContent": Array [
65+
"a { color: coral }
66+
",
67+
"a {
68+
color: coral;
69+
}",
70+
],
71+
"version": 3,
72+
}
73+
`;
74+
75+
exports[`Options Sourcemap should work with prev sourceMap: warnings 1`] = `Array []`;

test/options/sourceMap.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
import {
24
compile,
35
getCompiler,
@@ -23,6 +25,22 @@ describe('Options Sourcemap', () => {
2325
expect(getErrors(stats)).toMatchSnapshot('errors');
2426
});
2527

28+
it('should work disable Sourcemap - {Boolean}', async () => {
29+
const compiler = getCompiler(
30+
'./css/index.js',
31+
{ sourceMap: false },
32+
{ devtool: 'source-map' }
33+
);
34+
const stats = await compile(compiler);
35+
36+
const codeFromBundle = getCodeFromBundle('style.css', stats);
37+
38+
expect(codeFromBundle.css).toMatchSnapshot('css');
39+
expect(codeFromBundle.map).toBeUndefined();
40+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
41+
expect(getErrors(stats)).toMatchSnapshot('errors');
42+
});
43+
2644
it('should work Sourcemap - {String}', async () => {
2745
const compiler = getCompiler('./css/index.js', { sourceMap: 'inline' });
2846
const stats = await compile(compiler);
@@ -34,4 +52,50 @@ describe('Options Sourcemap', () => {
3452
expect(getWarnings(stats)).toMatchSnapshot('warnings');
3553
expect(getErrors(stats)).toMatchSnapshot('errors');
3654
});
55+
56+
it('should work with prev sourceMap', async () => {
57+
const compiler = getCompiler(
58+
'./scss/index.js',
59+
{
60+
config: false,
61+
},
62+
{
63+
devtool: 'source-map',
64+
module: {
65+
rules: [
66+
{
67+
test: /\.scss$/i,
68+
use: [
69+
{
70+
loader: require.resolve('../helpers/testLoader'),
71+
options: {},
72+
},
73+
{
74+
loader: path.resolve(__dirname, '../../src'),
75+
options: {
76+
config: false,
77+
},
78+
},
79+
{
80+
loader: 'sass-loader',
81+
options: {
82+
// eslint-disable-next-line global-require
83+
implementation: require('sass'),
84+
},
85+
},
86+
],
87+
},
88+
],
89+
},
90+
}
91+
);
92+
const stats = await compile(compiler);
93+
94+
const codeFromBundle = getCodeFromBundle('style.scss', stats);
95+
96+
expect(codeFromBundle.css).toMatchSnapshot('css');
97+
expect(codeFromBundle.map).toMatchSnapshot('map');
98+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
99+
expect(getErrors(stats)).toMatchSnapshot('errors');
100+
});
37101
});

0 commit comments

Comments
 (0)