Skip to content

Commit 12d82b7

Browse files
committed
Initial implementation.
1 parent 1ce83cc commit 12d82b7

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
jsconfig.json

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Optimize CSS Assets Webpack Plugin
2+
3+
A Webpack plugin to optimize \ minimize CSS assets.

index.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var _ = require('underscore');
4+
var cssnano = require('cssnano');
5+
6+
function OptimizeCssAssetsPlugin() {};
7+
8+
OptimizeCssAssetsPlugin.prototype.apply = function(compiler) {
9+
compiler.plugin('after-emit', function (compilation, callback) {
10+
console.log('');
11+
console.log('Starting to optimize CSS...');
12+
var outputPath = compiler.options.output.path;
13+
var stats = compilation.getStats().toJson();
14+
var assetsByChunkName = stats.assetsByChunkName;
15+
var files = _.flatten(_.values(assetsByChunkName));
16+
var cssFiles = _.filter(files, function(fn){ return fn && fn.match && fn.match(/\.css$/g); });
17+
var counter = 0;
18+
function checkFinish() {
19+
if (cssFiles.length >= counter++) {
20+
console.log('CSS optimize ended.');
21+
callback();
22+
}
23+
};
24+
_.each(
25+
cssFiles,
26+
function(filename) {
27+
console.log('Processing ' + filename);
28+
var filePath = path.join(outputPath, filename);
29+
var css = fs.readFileSync(filePath, 'utf8');
30+
cssnano.process(css, {discardComments: {removeAll: true}}).then(
31+
function (result) {
32+
fs.writeFileSync(filePath, result.css);
33+
console.log('Processing ' + filename + ' ended');
34+
checkFinish();
35+
}, function(err) {
36+
console.log('Error processing file: ' + filename);
37+
console.log(err);
38+
checkFinish();
39+
}
40+
);
41+
}
42+
);
43+
});
44+
};
45+
46+
module.exports = OptimizeCssAssetsPlugin;

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "optimize-css-assets-webpack-plugin",
3+
"version": "1.0.0",
4+
"author": "Nuno Rodrigues",
5+
"description": "A Webpack plugin to optimize \\ minimize CSS assets.",
6+
"dependencies": {
7+
"cssnano": "^3.4.0",
8+
"underscore": "^1.8.3"
9+
},
10+
"main": "index.js",
11+
"homepage": "http://github.com/NMFR/optimize-css-assets-webpack-plugin",
12+
"repository": {
13+
"type": "git",
14+
"url": "http://github.com/NMFR/optimize-css-assets-webpack-plugin.git"
15+
},
16+
"license": "MIT"
17+
}

0 commit comments

Comments
 (0)