Skip to content

Commit

Permalink
add clean css
Browse files Browse the repository at this point in the history
  • Loading branch information
keelii committed Feb 26, 2016
1 parent 7d97531 commit 438ba09
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Component = require('../lib/component');
const Sprite = require('../lib/sprite');
const pngquant = require('../lib/pngquant');
const rebasePath = require('../lib/rebasePath');
const cleanCSS = require('../lib/cleanCSS');

const fse = require('fs-extra');
const async = require('async');
Expand Down Expand Up @@ -40,6 +41,9 @@ Processor.sass = function (config, input, callback) {
base: config._SOURCE_ROOT,
prefix: config.production + utils.dirToPath(config._PRD_PREFIX)
}))
.pipe(cleanCSS({
enabled: config._isPrd
}))
.pipe(vfs.dest(config._DEST_ROOT))
.on('end', callback);
};
Expand Down
30 changes: 30 additions & 0 deletions lib/cleanCSS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const path = require('path');
const map = require('map-stream');
const CleanCSS = require('clean-css');
const _ = require('lodash');
const utils = require('../lib/utils');

function cleanCSS(file, option, cb) {
let content = String(file.contents);
let defaults = {};

if (!option.enabled) {
return cb(null, file);
}

new CleanCSS(_.defaultsDeep(option, defaults)).minify(content, function (err, result) {
if (err) {
return cb(err);
}

file.contents = new Buffer(result.styles);
cb(null, file);
});
}

module.exports = function(option) {
return map(function(file, cb) {
cleanCSS(file, option, cb);
});
};
12 changes: 10 additions & 2 deletions lib/rebasePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function rebase(file, option, cb) {
return cb(null, file);
}

let urls = [];
_.uniq(matches).forEach((match) => {
if (!utils.isAbsUrl(match) && !utils.isDataUri(match)) {
// 去掉多余字符如:url(,),",'
Expand All @@ -28,11 +29,18 @@ function rebase(file, option, cb) {
let absDir = path.resolve(dirname, items);
let relPath = utils.dirToPath(path.relative(option.base, absDir));

content = content.replace(match, `url(${option.prefix}/${relPath})`);
// console.log(option.prefix + '/' + relPath);
urls.push({
path: items,
production: `${option.prefix}/${relPath}`
});
}
});

urls.forEach(function (url) {
let urlRE = new RegExp(url.path, 'gi');
content = content.replace(urlRE, url.production);
});

file.contents = new Buffer(content);

cb(null, file);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wooo",
"version": "0.0.8",
"version": "0.0.9",
"author": "keelii <[email protected]>",
"description": "FE Build tool with easy cli.",
"license": "MIT",
Expand Down Expand Up @@ -29,6 +29,7 @@
"async": "^1.5.2",
"chalk": "^1.1.1",
"chokidar": "^1.4.2",
"clean-css": "^3.4.9",
"fs-extra": "^0.26.5",
"globby": "^4.0.0",
"imagemin-pngquant": "^4.2.2",
Expand Down

0 comments on commit 438ba09

Please sign in to comment.