Skip to content

Commit

Permalink
fix: source map generation when sourceRoot is present (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Mar 6, 2019
1 parent a49e904 commit e9ce745
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 192 deletions.
67 changes: 27 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"icss-utils": "^4.1.0",
"loader-utils": "^1.2.3",
"camelcase": "^5.2.0",
"normalize-path": "^3.0.0",
"postcss": "^7.0.14",
"postcss-modules-extract-imports": "^2.0.0",
"postcss-modules-local-by-default": "^2.0.6",
Expand Down
57 changes: 25 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getCurrentRequest,
stringifyRequest,
} from 'loader-utils';
import normalizePath from 'normalize-path';

import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
Expand All @@ -42,13 +43,24 @@ export default function loader(content, map, meta) {
/* eslint-disable no-param-reassign */
if (sourceMap) {
if (map) {
// Some loader emit source map as string
if (typeof map === 'string') {
map = JSON.stringify(map);
}

// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map

if (map.file) {
map.file = normalizePath(map.file);
}

if (map.sourceRoot) {
map.sourceRoot = normalizePath(map.sourceRoot);
}

if (map.sources) {
map.sources = map.sources.map((source) => source.replace(/\\/g, '/'));
map.sourceRoot = '';
map.sources = map.sources.map((source) => normalizePath(source));
}
}
} else {
Expand Down Expand Up @@ -120,17 +132,15 @@ export default function loader(content, map, meta) {

postcss(plugins)
.process(content, {
// we need a prefix to avoid path rewriting of PostCSS
from: `/css-loader!${getRemainingRequest(this)
from: getRemainingRequest(this)
.split('!')
.pop()}`,
.pop(),
to: getCurrentRequest(this)
.split('!')
.pop(),
map: options.sourceMap
? {
prev: map,
sourcesContent: true,
inline: false,
annotation: false,
}
Expand All @@ -141,6 +151,14 @@ export default function loader(content, map, meta) {
.warnings()
.forEach((warning) => this.emitWarning(new Warning(warning)));

if (result.map) {
const newMap = result.map.toJSON();

console.log(newMap.file);
console.log(newMap.sourceRoot);
console.log(newMap.sources);
}

const messages = result.messages || [];

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

let newMap = result.map;

if (sourceMap && newMap) {
// Add a SourceMap
newMap = newMap.toJSON();

if (newMap.sources) {
newMap.sources = newMap.sources.map(
(source) =>
source
.split('!')
.pop()
.replace(/\\/g, '/'),
this
);
newMap.sourceRoot = '';
}

newMap.file = newMap.file
.split('!')
.pop()
.replace(/\\/g, '/');
newMap = JSON.stringify(newMap);
}

const runtimeCode = `exports = module.exports = require(${stringifyRequest(
this,
require.resolve('./runtime/api')
)})(${!!sourceMap});\n`;
const importCode =
imports.length > 0 ? `// Imports\n${imports.join('\n')}\n\n` : '';
const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${
newMap ? `,${newMap}` : ''
result.map ? `,${result.map}` : ''
}]);\n\n`;
const exportsCode =
exports.length > 0
Expand Down
Loading

0 comments on commit e9ce745

Please sign in to comment.