Skip to content

Commit 98441ff

Browse files
fix: respect the map option and source maps (#458)
1 parent ba88040 commit 98441ff

File tree

5 files changed

+202
-20
lines changed

5 files changed

+202
-20
lines changed

package-lock.json

+15-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"postcss-short": "^5.0.0",
8080
"prettier": "^2.0.5",
8181
"sass": "^1.26.10",
82-
"sass-loader": "^10.0.1",
82+
"sass-loader": "^10.0.2",
8383
"standard": "^14.3.4",
8484
"standard-version": "^8.0.2",
8585
"strip-ansi": "^6.0.0",

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ export default async function loader(content, sourceMap) {
107107
this.resourcePath
108108
);
109109
}
110+
} else if (sourceMap && typeof processOptions.map !== 'undefined') {
111+
if (typeof processOptions.map === 'boolean') {
112+
processOptions.map = { inline: true };
113+
}
114+
115+
processOptions.map.prev = sourceMap;
110116
}
111117

112118
let result;

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

+51-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): css 1`] = `
3+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): css 1`] = `
4+
"a {
5+
color: coral;
6+
}
7+
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLnNjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFBSSxZQUFBO0FBRUoiLCJmaWxlIjoic3R5bGUuc2NzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogY29yYWwgfVxuIl19 */"
8+
`;
9+
10+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;
11+
12+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;
13+
14+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): css 1`] = `
15+
"a {
16+
color: coral;
17+
}
18+
/*# sourceMappingURL=style.scss.map */"
19+
`;
20+
21+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;
22+
23+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): source map 1`] = `
24+
Object {
25+
"file": "style.scss",
26+
"mappings": "AAAA;EAAI,YAAA;AAEJ",
27+
"names": Array [],
28+
"sourceRoot": "",
29+
"sources": Array [
30+
"../style.scss",
31+
],
32+
"sourcesContent": Array [
33+
"a { color: coral }
34+
",
35+
],
36+
"version": 3,
37+
}
38+
`;
39+
40+
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;
41+
42+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): css 1`] = `
443
"a {
544
color: coral;
645
}
746
"
847
`;
948

10-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): errors 1`] = `Array []`;
49+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): errors 1`] = `Array []`;
1150

12-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): source map 1`] = `
51+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): source map 1`] = `
1352
Object {
1453
"mappings": "AAAA;EAAI,YAAA;AAEJ",
1554
"names": Array [],
@@ -25,17 +64,17 @@ Object {
2564
}
2665
`;
2766

28-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): warnings 1`] = `Array []`;
67+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): warnings 1`] = `Array []`;
2968

30-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): css 1`] = `
69+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): css 1`] = `
3170
"a {
3271
color: coral;
3372
}"
3473
`;
3574

36-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): errors 1`] = `Array []`;
75+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;
3776

38-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): source map 1`] = `
77+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): source map 1`] = `
3978
Object {
4079
"mappings": "AAAA;EAAI,YAAA;AAEJ",
4180
"names": Array [],
@@ -51,9 +90,9 @@ Object {
5190
}
5291
`;
5392

54-
exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): warnings 1`] = `Array []`;
93+
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;
5594

56-
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: css 1`] = `
95+
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: css 1`] = `
5796
"a {
5897
color: black;
5998
}
@@ -100,9 +139,9 @@ a {
100139
"
101140
`;
102141

103-
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: errors 1`] = `Array []`;
142+
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: errors 1`] = `Array []`;
104143

105-
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: source map 1`] = `
144+
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: source map 1`] = `
106145
Object {
107146
"file": "style.css",
108147
"mappings": "AAAA;EACE,YAAY;AACd;;AAEA;EACE,UAAU;AACZ;;AAEA;EACE,YAAY;AACd;;AAEA;EACE,WAAW;AACb;;AAEA;EACE,4BAA4B;EAC5B,mBAAmB;AACrB;;AAEA;EACE,4BAA4B;EAC5B,mBAAmB;AACrB;;AAEA;EACE;IACE,YAAY;;IAEZ;MACE,WAAW;IACb;;IAEA;MACE,YAAY;IACd;EACF;;EAEA;IACE,cAAc;EAChB;AACF",
@@ -161,7 +200,7 @@ a {
161200
}
162201
`;
163202

164-
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: warnings 1`] = `Array []`;
203+
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: warnings 1`] = `Array []`;
165204

166205
exports[`"sourceMap" option should generate source maps when value has "true" value and the "devtool" option has "false" value: css 1`] = `
167206
"a {

test/options/sourceMap.test.js

+129-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('"sourceMap" option', () => {
108108
expect(getErrors(stats)).toMatchSnapshot('errors');
109109
});
110110

111-
it('should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value', async () => {
111+
it('should generate source maps when value has "false" value, but the "postcssOptions.map" has values', async () => {
112112
const compiler = getCompiler(
113113
'./css/index.js',
114114
{
@@ -147,7 +147,133 @@ describe('"sourceMap" option', () => {
147147
expect(getErrors(stats)).toMatchSnapshot('errors');
148148
});
149149

150-
it('should generate source maps when previous loader return source maps ("sass-loader")', async () => {
150+
it('should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader")', async () => {
151+
const compiler = getCompiler(
152+
'./scss/index.js',
153+
{},
154+
{
155+
devtool: false,
156+
module: {
157+
rules: [
158+
{
159+
test: /\.scss$/i,
160+
use: [
161+
{
162+
loader: require.resolve('../helpers/testLoader'),
163+
options: {},
164+
},
165+
{
166+
loader: path.resolve(__dirname, '../../src'),
167+
options: {
168+
postcssOptions: {
169+
map: true,
170+
},
171+
},
172+
},
173+
{
174+
loader: 'sass-loader',
175+
options: {
176+
// eslint-disable-next-line global-require
177+
implementation: require('sass'),
178+
sassOptions: {
179+
sourceMap: true,
180+
outFile: path.join(
181+
__dirname,
182+
'../fixtures/scss/style.css.map'
183+
),
184+
sourceMapContents: true,
185+
omitSourceMapUrl: true,
186+
sourceMapEmbed: false,
187+
},
188+
},
189+
},
190+
],
191+
},
192+
],
193+
},
194+
}
195+
);
196+
const stats = await compile(compiler);
197+
const { css, sourceMap } = getCodeFromBundle('style.scss', stats);
198+
199+
expect(css).toMatchSnapshot('css');
200+
expect(sourceMap).toBeUndefined();
201+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
202+
expect(getErrors(stats)).toMatchSnapshot('errors');
203+
});
204+
205+
it('should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader")', async () => {
206+
const compiler = getCompiler(
207+
'./scss/index.js',
208+
{},
209+
{
210+
devtool: false,
211+
module: {
212+
rules: [
213+
{
214+
test: /\.scss$/i,
215+
use: [
216+
{
217+
loader: require.resolve('../helpers/testLoader'),
218+
options: {},
219+
},
220+
{
221+
loader: path.resolve(__dirname, '../../src'),
222+
options: {
223+
postcssOptions: {
224+
map: {
225+
inline: false,
226+
sourcesContent: true,
227+
annotation: true,
228+
},
229+
},
230+
},
231+
},
232+
{
233+
loader: 'sass-loader',
234+
options: {
235+
// eslint-disable-next-line global-require
236+
implementation: require('sass'),
237+
sassOptions: {
238+
sourceMap: true,
239+
outFile: path.join(
240+
__dirname,
241+
'../fixtures/scss/style.css.map'
242+
),
243+
sourceMapContents: true,
244+
omitSourceMapUrl: true,
245+
sourceMapEmbed: false,
246+
},
247+
},
248+
},
249+
],
250+
},
251+
],
252+
},
253+
}
254+
);
255+
const stats = await compile(compiler);
256+
const { css, sourceMap } = getCodeFromBundle('style.scss', stats);
257+
258+
sourceMap.sourceRoot = '';
259+
sourceMap.sources = sourceMap.sources.map((source) => {
260+
expect(path.isAbsolute(source)).toBe(false);
261+
expect(
262+
fs.existsSync(path.resolve(__dirname, '../fixtures/scss', source))
263+
).toBe(true);
264+
265+
return path
266+
.relative(path.resolve(__dirname, '..'), source)
267+
.replace(/\\/g, '/');
268+
});
269+
270+
expect(css).toMatchSnapshot('css');
271+
expect(sourceMap).toMatchSnapshot('source map');
272+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
273+
expect(getErrors(stats)).toMatchSnapshot('errors');
274+
});
275+
276+
it('should generate source maps when previous loader returns source maps ("sass-loader")', async () => {
151277
const compiler = getCompiler(
152278
'./scss/index.js',
153279
{},
@@ -201,7 +327,7 @@ describe('"sourceMap" option', () => {
201327
expect(getErrors(stats)).toMatchSnapshot('errors');
202328
});
203329

204-
it('should generate source maps when previous loader return source maps ("less-loader")', async () => {
330+
it('should generate source maps when previous loader returns source maps ("less-loader")', async () => {
205331
const compiler = getCompiler(
206332
'./less/index.js',
207333
{

0 commit comments

Comments
 (0)