Skip to content

Commit

Permalink
refactor: source map code
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `sources` in source maps are absolute
  • Loading branch information
evilebottnawi committed Sep 4, 2020
1 parent 677c2fe commit 25a16a0
Show file tree
Hide file tree
Showing 27 changed files with 1,988 additions and 595 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -44,7 +44,6 @@
"dependencies": {
"cosmiconfig": "^7.0.0",
"loader-utils": "^2.0.0",
"normalize-path": "^3.0.0",
"postcss": "^7.0.0",
"schema-utils": "^2.7.0"
},
Expand Down Expand Up @@ -80,7 +79,7 @@
"postcss-short": "^5.0.0",
"prettier": "^2.0.5",
"sass": "^1.26.10",
"sass-loader": "^9.0.3",
"sass-loader": "^10.0.1",
"standard": "^14.3.4",
"standard-version": "^8.0.2",
"strip-ansi": "^6.0.0",
Expand Down
59 changes: 23 additions & 36 deletions src/index.js
Expand Up @@ -12,9 +12,8 @@ import {
loadConfig,
getPostcssOptions,
exec,
getSourceMapAbsolutePath,
getSourceMapRelativePath,
normalizeSourceMap,
normalizeSourceMapAfterPostcss,
} from './utils';

/**
Expand All @@ -38,43 +37,40 @@ export default async function loader(content, sourceMap, meta = {}) {
baseDataPath: 'options',
});

const file = this.resourcePath;
const configOptions =
const callback = this.async();
const configOption =
typeof options.postcssOptions === 'undefined' ||
typeof options.postcssOptions.config === 'undefined'
? true
: options.postcssOptions.config;

let loadedConfig = {};

const callback = this.async();

if (configOptions) {
if (configOption) {
const dataForLoadConfig = {
path: path.dirname(file),
path: path.dirname(this.resourcePath),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file),
extname: path.extname(this.resourcePath),
dirname: path.dirname(this.resourcePath),
basename: path.basename(this.resourcePath),
},
options: {},
},
};

if (typeof configOptions.path !== 'undefined') {
dataForLoadConfig.path = path.resolve(configOptions.path);
if (typeof configOption.path !== 'undefined') {
dataForLoadConfig.path = path.resolve(configOption.path);
}

if (typeof configOptions.ctx !== 'undefined') {
dataForLoadConfig.ctx.options = configOptions.ctx;
if (typeof configOption.ctx !== 'undefined') {
dataForLoadConfig.ctx.options = configOption.ctx;
}

dataForLoadConfig.ctx.webpack = this;

try {
loadedConfig = await loadConfig(
configOptions,
configOption,
dataForLoadConfig.ctx,
dataForLoadConfig.path,
this
Expand Down Expand Up @@ -104,18 +100,12 @@ export default async function loader(content, sourceMap, meta = {}) {

if (useSourceMap) {
processOptions.map = { inline: false, annotation: false };
// options.sourceMap === 'inline'
// ? { inline: true, annotation: false }
// : { inline: false, annotation: false };

if (sourceMap) {
const sourceMapNormalized = normalizeSourceMap(sourceMap);

sourceMapNormalized.sources = sourceMapNormalized.sources.map((src) =>
getSourceMapRelativePath(src, path.dirname(file))
processOptions.map.prev = normalizeSourceMap(
sourceMap,
this.resourcePath
);

processOptions.map.prev = sourceMapNormalized;
}
}

Expand All @@ -137,11 +127,11 @@ export default async function loader(content, sourceMap, meta = {}) {
return;
}

result.warnings().forEach((warning) => {
for (const warning of result.warnings()) {
this.emitWarning(new Warning(warning));
});
}

result.messages.forEach((message) => {
for (const message of result.messages) {
if (message.type === 'dependency') {
this.addDependency(message.file);
}
Expand All @@ -154,16 +144,13 @@ export default async function loader(content, sourceMap, meta = {}) {
message.info
);
}
});
}

const map = result.map ? result.map.toJSON() : null;
// eslint-disable-next-line no-undefined
let map = result.map ? result.map.toJSON() : undefined;

if (map && useSourceMap) {
if (typeof map.file !== 'undefined') {
delete map.file;
}

map.sources = map.sources.map((src) => getSourceMapAbsolutePath(src, file));
map = normalizeSourceMapAfterPostcss(map, this.resourcePath);
}

const ast = {
Expand Down
12 changes: 4 additions & 8 deletions src/options.json
Expand Up @@ -75,12 +75,8 @@
]
},
"plugins": {
"description": "Set PostCSS Plugins (https://github.com/postcss/postcss-loader#plugins)",
"anyOf": [
{ "type": "array" },
{ "type": "object" },
{ "instanceof": "Function" }
]
"description": "Sets PostCSS Plugins (https://github.com/postcss/postcss-loader#plugins)",
"anyOf": [{ "type": "array" }, { "type": "object" }]
}
}
},
Expand All @@ -90,13 +86,13 @@
]
},
"exec": {
"description": "Enable PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)",
"description": "Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)",
"type": "boolean"
},
"sourceMap": {
"description": "Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)",
"type": "boolean"
}
},
"additionalProperties": true
"additionalProperties": false
}

0 comments on commit 25a16a0

Please sign in to comment.