Skip to content

Commit

Permalink
refactor: sourcemap processing
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Aug 20, 2020
1 parent 8353353 commit 46d525f
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 58 deletions.
172 changes: 172 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -76,6 +76,7 @@
"file-loader": "^6.0.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"less-loader": "^6.2.0",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"mini-css-extract-plugin": "^0.9.0",
Expand All @@ -88,6 +89,8 @@
"standard-version": "^8.0.2",
"strip-ansi": "^6.0.0",
"style-loader": "^1.2.1",
"stylus": "^0.54.8",
"stylus-loader": "^3.0.2",
"url-loader": "^4.1.0",
"webpack": "^4.44.0"
},
Expand Down
22 changes: 20 additions & 2 deletions src/index.js
Expand Up @@ -2,6 +2,8 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import path from 'path';

import { getOptions, stringifyRequest } from 'loader-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
Expand All @@ -26,6 +28,7 @@ import {
getModulesPlugins,
normalizeSourceMap,
sort,
getSourceMapRelativePath,
} from './utils';

export default async function loader(content, map, meta) {
Expand Down Expand Up @@ -151,6 +154,14 @@ export default async function loader(content, map, meta) {
}
}

const sourceMap = map ? normalizeSourceMap(map) : null;

if (sourceMap) {
sourceMap.sources = sourceMap.sources.map((src) =>
getSourceMapRelativePath(src, path.dirname(this.resourcePath))
);
}

let result;

try {
Expand All @@ -160,7 +171,7 @@ export default async function loader(content, map, meta) {
map: options.sourceMap
? {
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
prev: map ? normalizeSourceMap(map) : null,
prev: sourceMap,
inline: false,
annotation: false,
}
Expand Down Expand Up @@ -198,7 +209,14 @@ export default async function loader(content, map, meta) {
}

const importCode = getImportCode(imports, options);
const moduleCode = getModuleCode(result, api, replacements, options);
const moduleCode = getModuleCode(
result,
api,
replacements,
options,
this.resourcePath,
this.rootContext
);
const exportCode = getExportCode(exports, replacements, options);

callback(null, `${importCode}${moduleCode}${exportCode}`);
Expand Down

0 comments on commit 46d525f

Please sign in to comment.