Skip to content

Commit

Permalink
Source maps support (#61)
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
frenzzy authored and NMFR committed Jun 26, 2018
1 parent d084a55 commit 15a41c8
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,36 @@ class OptimizeCssAssetsWebpackPlugin extends LastCallWebpackPlugin {
}

processCss(assetName, asset, assets) {
const css = asset.source();
const css = asset.sourceAndMap ? asset.sourceAndMap() : { source: asset.source() };
const processOptions = Object.assign(
{ from: assetName, to: assetName },
this.options.cssProcessorOptions || {}
this.options.cssProcessorOptions
);

if (processOptions.map && !processOptions.map.prev) {
try {
const mapJson = assets.getAsset(assetName + '.map');
if (mapJson) {
const map = JSON.parse(mapJson);
if (
map &&
(
(map.sources && map.sources.length > 0) ||
(map.mappings && map.mappings.length > 0)
)
) {
processOptions.map = Object.assign({ prev: mapJson }, processOptions.map);
let map = css.map;
if (!map) {
const mapJson = assets.getAsset(assetName + '.map');
if (mapJson) {
map = JSON.parse(mapJson);
}
}
if (
map &&
(
(map.sources && map.sources.length > 0) ||
(map.mappings && map.mappings.length > 0)
)
) {
processOptions.map = Object.assign({ prev: map }, processOptions.map);
}
} catch (err) {
console.warn('OptimizeCssAssetsPlugin.processCss() Error getting previous source map', err);
}
}
return this.options
.cssProcessor.process(css, processOptions)
.cssProcessor.process(css.source, processOptions)
.then(r => {
if (processOptions.map && r.map && r.map.toString) {
assets.setAsset(assetName + '.map', r.map.toString());
Expand Down

0 comments on commit 15a41c8

Please sign in to comment.