Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UglifyJS Webpack Plugin also uses sourceAndMap function if exists

const processOptions = Object.assign(
{ from: assetName, to: assetName },
this.options.cssProcessorOptions || {}
this.options.cssProcessorOptions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From MDN:

Note that Object.assign() does not throw on null or undefined source values.

);

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From PostCSS docs:

prev string, object, boolean or function: source map content from a previous processing step (for example, Sass compilation). PostCSS will try to read the previous source map automatically (based on comments within the source CSS), but you can use this option to identify it manually. If desired, you can omit the previous map with prev: false.

}
} 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