Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Backport source map fix to v3
Browse files Browse the repository at this point in the history
The fix was in this PR: NMFR#61

Fixes NMFR#53

For those of us stuck on Webpack 3
  • Loading branch information
jasononeil committed Jun 29, 2018
1 parent 52c0699 commit e5f4754
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,35 @@ function OptimizeCssAssetsPlugin(options) {
};

OptimizeCssAssetsPlugin.prototype.processCss = function(assetName, asset, assets) {
var css = asset.source();
var css = asset.sourceAndMap ? asset.sourceAndMap() : { source: asset.source() };
var processOptions = Object.assign(
{ from: assetName, to: assetName },
this.options.cssProcessorOptions || {}
this.options.cssProcessorOptions
);
if (processOptions.map && !processOptions.map.prev) {
try {
var mapJson = assets.getAsset(assetName + '.map');
if (mapJson) {
var 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 e5f4754

Please sign in to comment.