Skip to content

Commit e9ce745

Browse files
fix: source map generation when sourceRoot is present (#901)
1 parent a49e904 commit e9ce745

8 files changed

+362
-192
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

+25-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';
@@ -42,13 +43,24 @@ export default function loader(content, map, meta) {
4243
/* eslint-disable no-param-reassign */
4344
if (sourceMap) {
4445
if (map) {
46+
// Some loader emit source map as string
4547
if (typeof map === 'string') {
4648
map = JSON.stringify(map);
4749
}
4850

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

121133
postcss(plugins)
122134
.process(content, {
123-
// we need a prefix to avoid path rewriting of PostCSS
124-
from: `/css-loader!${getRemainingRequest(this)
135+
from: getRemainingRequest(this)
125136
.split('!')
126-
.pop()}`,
137+
.pop(),
127138
to: getCurrentRequest(this)
128139
.split('!')
129140
.pop(),
130141
map: options.sourceMap
131142
? {
132143
prev: map,
133-
sourcesContent: true,
134144
inline: false,
135145
annotation: false,
136146
}
@@ -141,6 +151,14 @@ export default function loader(content, map, meta) {
141151
.warnings()
142152
.forEach((warning) => this.emitWarning(new Warning(warning)));
143153

154+
if (result.map) {
155+
const newMap = result.map.toJSON();
156+
157+
console.log(newMap.file);
158+
console.log(newMap.sourceRoot);
159+
console.log(newMap.sources);
160+
}
161+
144162
const messages = result.messages || [];
145163

146164
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
@@ -301,39 +319,14 @@ export default function loader(content, map, meta) {
301319
);
302320
});
303321

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-
329322
const runtimeCode = `exports = module.exports = require(${stringifyRequest(
330323
this,
331324
require.resolve('./runtime/api')
332325
)})(${!!sourceMap});\n`;
333326
const importCode =
334327
imports.length > 0 ? `// Imports\n${imports.join('\n')}\n\n` : '';
335328
const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${
336-
newMap ? `,${newMap}` : ''
329+
result.map ? `,${result.map}` : ''
337330
}]);\n\n`;
338331
const exportsCode =
339332
exports.length > 0

0 commit comments

Comments
 (0)