Skip to content

Commit 4d0538b

Browse files
committed
Use last-call-webpack-plugin
Bring back cssnano to dependencies (from peerDependencies).
1 parent 3b217bc commit 4d0538b

File tree

3 files changed

+21
-87
lines changed

3 files changed

+21
-87
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ A Webpack plugin to optimize \ minimize CSS assets.
66

77
It will search for CSS assets during the Webpack build and will optimize \ minimize the CSS (by default it uses [cssnano](http://github.com/ben-eb/cssnano) but a custom CSS processor can be specified).
88

9-
Note: To use the default CSS processor, [cssnano](http://github.com/ben-eb/cssnano) must be explicitly installed (since it's a peer dependency).
10-
119
### Solves [extract-text-webpack-plugin](http://github.com/webpack/extract-text-webpack-plugin) CSS duplication problem:
1210

1311
Since [extract-text-webpack-plugin](http://github.com/webpack/extract-text-webpack-plugin) only bundles (merges) text chunks, if its used to bundle CSS, the bundle might have duplicate entries (chunks can be duplicate free but when merged, duplicate CSS can be created).
@@ -19,12 +17,6 @@ Using npm:
1917
$ npm install --save-dev optimize-css-assets-webpack-plugin
2018
```
2119

22-
If you are not going to specify a CSS processor you will also need to install [cssnano](http://github.com/ben-eb/cssnano) (since it's a peer dependency):
23-
24-
```shell
25-
$ npm install --save-dev cssnano
26-
```
27-
2820
## Configuration:
2921

3022
The plugin can receive the following options (all of them are optional):

index.js

+19-74
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var _ = require('underscore');
2-
var webpackSources = require('webpack-sources');
1+
var LastCallWebpackPlugin = require('last-call-webpack-plugin');
32

43
function OptimizeCssAssetsPlugin(options) {
54
this.options = options || {};
@@ -19,84 +18,30 @@ function OptimizeCssAssetsPlugin(options) {
1918
if (this.options.canPrint === undefined) {
2019
this.options.canPrint = true;
2120
}
22-
};
23-
24-
OptimizeCssAssetsPlugin.prototype.print = function() {
25-
if (this.options.canPrint) {
26-
console.log.apply(console, arguments);
27-
}
28-
};
2921

30-
OptimizeCssAssetsPlugin.prototype.processCss = function(css, assetName) {
31-
return this.options.cssProcessor.process(css, Object.assign({ to: assetName }, this.options.cssProcessorOptions));
22+
var self = this;
23+
this.lastCallInstance = new LastCallWebpackPlugin({
24+
assetProcessors: [
25+
{
26+
regExp: this.options.assetNameRegExp,
27+
processor: function (assetName, asset) {
28+
return self.processCss(assetName, asset);
29+
},
30+
}
31+
],
32+
canPrint: this.options.canPrint
33+
});
3234
};
3335

34-
OptimizeCssAssetsPlugin.prototype.createCssAsset = function(css, originalAsset) {
35-
return new webpackSources.RawSource(css);
36+
OptimizeCssAssetsPlugin.prototype.processCss = function(assetName, asset) {
37+
var css = asset.source();
38+
return this.options
39+
.cssProcessor.process(css, Object.assign({ to: assetName }, this.options.cssProcessorOptions))
40+
.then(r => r.css);
3641
};
3742

3843
OptimizeCssAssetsPlugin.prototype.apply = function(compiler) {
39-
40-
var self = this;
41-
42-
compiler.plugin('emit', function(compilation, compileCallback) {
43-
44-
self.print('\nStarting to optimize CSS...');
45-
46-
var assets = compilation.assets;
47-
48-
var cssAssetNames = _.filter(
49-
_.keys(assets),
50-
function(assetName) {
51-
return assetName.match(self.options.assetNameRegExp);
52-
}
53-
);
54-
55-
var hasErrors = false;
56-
var promises = [];
57-
58-
_.each(
59-
cssAssetNames,
60-
function(assetName) {
61-
62-
self.print('Processing ' + assetName + '...');
63-
64-
var asset = assets[assetName];
65-
66-
var originalCss = asset.source();
67-
68-
var promise = self
69-
.processCss(originalCss, assetName)
70-
.then(
71-
function (result) {
72-
73-
if (hasErrors) {
74-
self.print('Skiping ' + assetName + ' because of an error.');
75-
return;
76-
}
77-
78-
var processedCss = result.css;
79-
80-
assets[assetName] = self.createCssAsset(processedCss, asset);
81-
82-
self.print('Processed ' + assetName + ', before: ' + originalCss.length + ', after: ' + processedCss.length + ', ratio: ' + (Math.round(((processedCss.length * 100) / originalCss.length) * 100) / 100) + '%');
83-
84-
}
85-
).catch(function(err) {
86-
hasErrors = true;
87-
self.print('Error processing file: ' + assetName);
88-
throw err;
89-
}
90-
);
91-
92-
promises.push(promise);
93-
}
94-
);
95-
96-
Promise.all(promises)
97-
.then(function () { compileCallback(); })
98-
.catch(compileCallback);
99-
});
44+
return this.lastCallInstance.apply(compiler);
10045
};
10146

10247
module.exports = OptimizeCssAssetsPlugin;

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
"author": "Nuno Rodrigues",
55
"description": "A Webpack plugin to optimize \\ minimize CSS assets.",
66
"dependencies": {
7-
"underscore": "^1.8.3",
8-
"webpack-sources": "^0.1.0"
9-
},
10-
"peerDependencies": {
11-
"cssnano": ">=3.4.0"
7+
"cssnano": "^3.4.0",
8+
"last-call-webpack-plugin": "^2.0.1"
129
},
1310
"main": "index.js",
1411
"homepage": "http://github.com/NMFR/optimize-css-assets-webpack-plugin",

0 commit comments

Comments
 (0)