Skip to content

Commit 6f97381

Browse files
fix: source map generation
1 parent a49e904 commit 6f97381

File tree

6 files changed

+63
-296
lines changed

6 files changed

+63
-296
lines changed

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"icss-utils": "^4.1.0",
4646
"loader-utils": "^1.2.3",
4747
"camelcase": "^5.2.0",
48+
"normalize-path": "^3.0.0",
4849
"postcss": "^7.0.14",
4950
"postcss-modules-extract-imports": "^2.0.0",
5051
"postcss-modules-local-by-default": "^2.0.6",

src/index.js

+16-32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getCurrentRequest,
1818
stringifyRequest,
1919
} from 'loader-utils';
20+
import normalizePath from 'normalize-path';
2021

2122
import schema from './options.json';
2223
import { importParser, icssParser, urlParser } from './plugins';
@@ -46,9 +47,19 @@ export default function loader(content, map, meta) {
4647
map = JSON.stringify(map);
4748
}
4849

50+
// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
51+
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
52+
53+
if (map.file) {
54+
map.file = normalizePath(map.file);
55+
}
56+
57+
if (map.sourceRoot) {
58+
map.sourceRoot = normalizePath(map.sourceRoot);
59+
}
60+
4961
if (map.sources) {
50-
map.sources = map.sources.map((source) => source.replace(/\\/g, '/'));
51-
map.sourceRoot = '';
62+
map.sources = map.sources.map((source) => normalizePath(source));
5263
}
5364
}
5465
} else {
@@ -120,17 +131,15 @@ export default function loader(content, map, meta) {
120131

121132
postcss(plugins)
122133
.process(content, {
123-
// we need a prefix to avoid path rewriting of PostCSS
124-
from: `/css-loader!${getRemainingRequest(this)
134+
from: getRemainingRequest(this)
125135
.split('!')
126-
.pop()}`,
136+
.pop(),
127137
to: getCurrentRequest(this)
128138
.split('!')
129139
.pop(),
130140
map: options.sourceMap
131141
? {
132142
prev: map,
133-
sourcesContent: true,
134143
inline: false,
135144
annotation: false,
136145
}
@@ -301,39 +310,14 @@ export default function loader(content, map, meta) {
301310
);
302311
});
303312

304-
let newMap = result.map;
305-
306-
if (sourceMap && newMap) {
307-
// Add a SourceMap
308-
newMap = newMap.toJSON();
309-
310-
if (newMap.sources) {
311-
newMap.sources = newMap.sources.map(
312-
(source) =>
313-
source
314-
.split('!')
315-
.pop()
316-
.replace(/\\/g, '/'),
317-
this
318-
);
319-
newMap.sourceRoot = '';
320-
}
321-
322-
newMap.file = newMap.file
323-
.split('!')
324-
.pop()
325-
.replace(/\\/g, '/');
326-
newMap = JSON.stringify(newMap);
327-
}
328-
329313
const runtimeCode = `exports = module.exports = require(${stringifyRequest(
330314
this,
331315
require.resolve('./runtime/api')
332316
)})(${!!sourceMap});\n`;
333317
const importCode =
334318
imports.length > 0 ? `// Imports\n${imports.join('\n')}\n\n` : '';
335319
const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${
336-
newMap ? `,${newMap}` : ''
320+
result.map ? `,${result.map}` : ''
337321
}]);\n\n`;
338322
const exportsCode =
339323
exports.length > 0

test/__snapshots__/sourceMap-option.test.js.snap

+3-110
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ Array [
3434

3535
exports[`sourceMap option false should not generate source map when source map is \`undefined\` from other loader: warnings 1`] = `Array []`;
3636

37-
exports[`sourceMap option false should not generate source map when source map is valid and it is string from other loader: errors 1`] = `Array []`;
38-
39-
exports[`sourceMap option false should not generate source map when source map is valid and it is string from other loader: module (evaluated) 1`] = `
40-
Array [
41-
Array [
42-
1,
43-
".class {
44-
color: red;
45-
}
46-
",
47-
"",
48-
],
49-
]
50-
`;
51-
52-
exports[`sourceMap option false should not generate source map when source map is valid and it is string from other loader: warnings 1`] = `Array []`;
53-
5437
exports[`sourceMap option false should not generate source map: errors 1`] = `Array []`;
5538

5639
exports[`sourceMap option false should not generate source map: module (evaluated) 1`] = `
@@ -68,23 +51,6 @@ Array [
6851

6952
exports[`sourceMap option false should not generate source map: warnings 1`] = `Array []`;
7053

71-
exports[`sourceMap option false should not generate sourceMap when source map is valid from other loader: errors 1`] = `Array []`;
72-
73-
exports[`sourceMap option false should not generate sourceMap when source map is valid from other loader: module (evaluated) 1`] = `
74-
Array [
75-
Array [
76-
1,
77-
".class {
78-
color: red;
79-
}
80-
",
81-
"",
82-
],
83-
]
84-
`;
85-
86-
exports[`sourceMap option false should not generate sourceMap when source map is valid from other loader: warnings 1`] = `Array []`;
87-
8854
exports[`sourceMap option true should generate source map when source map is \`null\` from other loader: errors 1`] = `Array []`;
8955

9056
exports[`sourceMap option true should generate source map when source map is \`null\` from other loader: module (evaluated) 1`] = `
@@ -100,9 +66,8 @@ Array [
10066
"file": "basic.css",
10167
"mappings": "AAAA;EACE,UAAU;AACZ",
10268
"names": Array [],
103-
"sourceRoot": "",
10469
"sources": Array [
105-
"/replaced/original/path/",
70+
"basic.css",
10671
],
10772
"sourcesContent": Array [
10873
".class {
@@ -133,9 +98,8 @@ Array [
13398
"file": "basic.css",
13499
"mappings": "AAAA;EACE,UAAU;AACZ",
135100
"names": Array [],
136-
"sourceRoot": "",
137101
"sources": Array [
138-
"/replaced/original/path/",
102+
"basic.css",
139103
],
140104
"sourcesContent": Array [
141105
".class {
@@ -151,76 +115,6 @@ Array [
151115

152116
exports[`sourceMap option true should generate source map when source map is \`undefined\` from other loader: warnings 1`] = `Array []`;
153117

154-
exports[`sourceMap option true should generate source map when source map is valid and it is string from other loader: errors 1`] = `Array []`;
155-
156-
exports[`sourceMap option true should generate source map when source map is valid and it is string from other loader: module (evaluated) 1`] = `
157-
Array [
158-
Array [
159-
1,
160-
".class {
161-
color: red;
162-
}
163-
",
164-
"",
165-
Object {
166-
"file": "basic.css",
167-
"mappings": "AAAA;ECCE,UAAU;AACZ",
168-
"names": Array [],
169-
"sourceRoot": "",
170-
"sources": Array [
171-
"/replaced/original/path/",
172-
"/replaced/original/path/",
173-
],
174-
"sourcesContent": Array [
175-
".class { a: b c d; }",
176-
".class {
177-
color: red;
178-
}
179-
",
180-
],
181-
"version": 3,
182-
},
183-
],
184-
]
185-
`;
186-
187-
exports[`sourceMap option true should generate source map when source map is valid and it is string from other loader: warnings 1`] = `Array []`;
188-
189-
exports[`sourceMap option true should generate source map when source map is valid from other loader: errors 1`] = `Array []`;
190-
191-
exports[`sourceMap option true should generate source map when source map is valid from other loader: module (evaluated) 1`] = `
192-
Array [
193-
Array [
194-
1,
195-
".class {
196-
color: red;
197-
}
198-
",
199-
"",
200-
Object {
201-
"file": "basic.css",
202-
"mappings": "AAAA;ECCE,UAAU;AACZ",
203-
"names": Array [],
204-
"sourceRoot": "",
205-
"sources": Array [
206-
"/replaced/original/path/",
207-
"/replaced/original/path/",
208-
],
209-
"sourcesContent": Array [
210-
".class { a: b c d; }",
211-
".class {
212-
color: red;
213-
}
214-
",
215-
],
216-
"version": 3,
217-
},
218-
],
219-
]
220-
`;
221-
222-
exports[`sourceMap option true should generate source map when source map is valid from other loader: warnings 1`] = `Array []`;
223-
224118
exports[`sourceMap option true should generate source map: errors 1`] = `Array []`;
225119

226120
exports[`sourceMap option true should generate source map: module (evaluated) 1`] = `
@@ -236,9 +130,8 @@ Array [
236130
"file": "basic.css",
237131
"mappings": "AAAA;EACE,UAAU;AACZ",
238132
"names": Array [],
239-
"sourceRoot": "",
240133
"sources": Array [
241-
"/replaced/original/path/",
134+
"basic.css",
242135
],
243136
"sourcesContent": Array [
244137
".class {

0 commit comments

Comments
 (0)