Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: source map generation #901

Merged
merged 1 commit into from Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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