diff --git a/cli/build.js b/cli/build.js index 909e3ba..f6f1043 100644 --- a/cli/build.js +++ b/cli/build.js @@ -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'); @@ -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); }; diff --git a/lib/cleanCSS.js b/lib/cleanCSS.js new file mode 100644 index 0000000..1faf7b6 --- /dev/null +++ b/lib/cleanCSS.js @@ -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); + }); +}; \ No newline at end of file diff --git a/lib/rebasePath.js b/lib/rebasePath.js index d5c9357..919405f 100644 --- a/lib/rebasePath.js +++ b/lib/rebasePath.js @@ -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(,),",' @@ -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); diff --git a/package.json b/package.json index a19087c..f840a12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wooo", - "version": "0.0.8", + "version": "0.0.9", "author": "keelii ", "description": "FE Build tool with easy cli.", "license": "MIT", @@ -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",