-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var path = require('path'), | ||
fs = require('fs'), | ||
minimize = require('./utils').minimize; | ||
|
||
module.exports = { | ||
// each method is the imitation of the usage of some minimizer | ||
minimizers: { | ||
CSSO: minimize(require('csso'), 'justDoIt'), | ||
cleancss: minimize(new require('clean-css')(), 'minify'), | ||
cssshrink: minimize(require('cssshrink'), 'shrink'), | ||
}, | ||
// input and output | ||
paths: { | ||
toRawCSS: path.join('css', 'raw'), | ||
toMinCSS: path.join('css', 'min') | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,58 @@ | ||
var fs = require('fs'), | ||
path = require('path'), | ||
csso = require('csso'), | ||
cleancss = new require('clean-css')(), | ||
cssshrink = require('cssshrink'); | ||
bench = require('./config'), | ||
utils = require('./utils'), | ||
minimizers = bench.minimizers, | ||
toRawCSS = bench.paths.toRawCSS, | ||
toMinCSS = bench.paths.toMinCSS; | ||
|
||
require('colors'); | ||
|
||
function mkdirs(paths) { | ||
paths.forEach(function(path) { | ||
fs.mkdir(path, function(err) { | ||
if (err && err.code !== 'EEXIST') throw err; | ||
}); | ||
}); | ||
} | ||
|
||
function addSpaces(name) { | ||
var spaces = ''; | ||
for (var i = 0; i < 34 - name.length; i++) { | ||
spaces += ' '; | ||
} | ||
|
||
return spaces; | ||
} | ||
// constants | ||
var maxSize = Number.MAX_VALUE, | ||
maxTime = Number.MAX_VALUE; | ||
|
||
// create paths | ||
mkdirs(['css/min', 'css/min/csso', 'css/min/cleancss', 'css/min/cssshrink']); | ||
var listOfMinimizers = Object.keys(minimizers); | ||
|
||
var toRawCSS = path.join('css', 'raw'), | ||
toCSSOOtput = path.join('css', 'min', 'csso'), | ||
toCleanCssOutput = path.join('css', 'min', 'cleancss'), | ||
toCssShrinkOutput = path.join('css', 'min', 'cssshrink'); | ||
// makes dirs | ||
listOfMinimizers.forEach(function(minimizer) { | ||
utils.mkdirs([toMinCSS, path.join(toMinCSS, minimizer)]); | ||
}); | ||
|
||
// Ok! Let's GO! | ||
var cssFiles = fs.readdirSync(toRawCSS); | ||
|
||
cssFiles.forEach(function(cssFile) { | ||
console.log( | ||
'---> '.bold.green + cssFile + addSpaces(cssFile) + | ||
' --> ' + (fs.statSync(path.join(toRawCSS, cssFile)).size + '').bold + ' b' | ||
); | ||
|
||
var css = fs.readFileSync(path.join(toRawCSS, cssFile), 'utf-8'); | ||
|
||
var start = 0, | ||
end = 0; | ||
|
||
start = Date.now(); | ||
var minCSSO = csso.justDoIt(css); | ||
end = Date.now(); | ||
var timeCSSO = end - start; | ||
|
||
start = Date.now(); | ||
var minCleanCss = cleancss.minify(css); | ||
end = Date.now(); | ||
var timeCleanCss = end - start; | ||
|
||
start = Date.now(); | ||
var minCssShrink = cssshrink.shrink(css); | ||
end = Date.now(); | ||
var timeCssShrink = end - start; | ||
|
||
fs.writeFileSync(path.join(toCSSOOtput, cssFile), minCSSO); | ||
fs.writeFileSync(path.join(toCleanCssOutput, cssFile), minCleanCss); | ||
fs.writeFileSync(path.join(toCssShrinkOutput, cssFile), minCssShrink); | ||
|
||
var fileSizeCSSO = fs.statSync(path.join(toCSSOOtput, cssFile)).size, | ||
fileSizeCleanCss = fs.statSync(path.join(toCleanCssOutput, cssFile)).size, | ||
fileSizeCssShrink = fs.statSync(path.join(toCssShrinkOutput, cssFile)).size; | ||
|
||
var minSize = Math.min(fileSizeCSSO, fileSizeCleanCss, fileSizeCssShrink), | ||
minTime = Math.min(timeCSSO, timeCleanCss, timeCssShrink); | ||
var pathToFile = path.join(toRawCSS, cssFile); | ||
|
||
// logs the test file | ||
console.log( | ||
' > was minimized by CSSO --> ' + | ||
(minSize === fileSizeCSSO ? (fileSizeCSSO + '').bold.green : fileSizeCSSO) + ' b | ' + | ||
(minTime === timeCSSO ? (timeCSSO + '').bold.green : timeCSSO) + ' ms' | ||
'---> '.bold.green + cssFile + utils.addSpaces(cssFile, 40) + | ||
' --> ' + (fs.statSync(pathToFile).size + '').bold + ' b' | ||
); | ||
|
||
console.log( | ||
' > was minimized by clean-css --> ' + | ||
(minSize === fileSizeCleanCss ? (fileSizeCleanCss + '').bold.green : fileSizeCleanCss) + ' b | ' + | ||
(minTime === timeCleanCss ? (timeCleanCss + '').bold.green : timeCleanCss) + ' ms' | ||
); | ||
// minimizes the file by different minimizers which are specified in 'config.js' | ||
var res = {}; | ||
listOfMinimizers.forEach(function(minimizer) { | ||
res[minimizer] = minimizers[minimizer](pathToFile, path.join(toMinCSS, minimizer, cssFile)); | ||
}); | ||
|
||
console.log( | ||
' > was minimized by cssshrink --> ' + | ||
(minSize === fileSizeCssShrink ? (fileSizeCssShrink + '').bold.green : fileSizeCssShrink) + ' b | ' + | ||
(minTime === timeCssShrink ? (timeCssShrink + '').bold.green : timeCssShrink) + ' ms' | ||
); | ||
// gets the best and the fastest results | ||
var minSize = maxSize, | ||
minTime = maxTime; | ||
listOfMinimizers.forEach(function(minimizer) { | ||
minSize = Math.min(minSize, res[minimizer].size); | ||
minTime = Math.min(minTime, res[minimizer].time); | ||
}); | ||
|
||
// logs the results of the test | ||
listOfMinimizers.forEach(function(minimizer) { | ||
console.log( | ||
' > was minimized by ' + minimizer + utils.addSpaces(minimizer, 20) + '--> ' + | ||
(minSize === res[minimizer].size ? (res[minimizer].size + '').bold.green : res[minimizer].size) + ' b | ' + | ||
(minTime === res[minimizer].time ? (res[minimizer].time + '').bold.green : res[minimizer].time) + ' ms' | ||
); | ||
}); | ||
|
||
console.log() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var fs = require('fs'); | ||
|
||
/** | ||
* Makes directories | ||
* @param {Array} - list of paths | ||
*/ | ||
function mkdirs(paths) { | ||
paths.forEach(function(path) { | ||
try { | ||
fs.mkdirSync(path); | ||
} | ||
catch(err) { | ||
if (err && err.code !== 'EEXIST') throw err; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Adds spaces for pretty logging | ||
* @param {String} - string in the log | ||
* @param {Number} - interval | ||
* @returns {String} - string of spaces | ||
*/ | ||
function addSpaces(name, interval) { | ||
return new Array(interval - name.length).join(' '); | ||
} | ||
|
||
/** | ||
* Returns a size of a minimized file and a time of a minimizer's work | ||
* @param {Object} - required minimizer's module | ||
* @param {String} - name of minimizer's method | ||
* @returns {Object} | ||
*/ | ||
function minimize(minimizer, minMethod) { | ||
return function(inputFile, outputFile) { | ||
var css = fs.readFileSync(inputFile, 'utf-8'), | ||
res = {}; | ||
|
||
start = Date.now(); | ||
min = minimizer[minMethod](css); | ||
end = Date.now(); | ||
res.time = end - start; | ||
|
||
fs.writeFileSync(outputFile, min); | ||
res.size = fs.statSync(outputFile).size; | ||
|
||
return res; | ||
}; | ||
} | ||
|
||
module.exports = { | ||
mkdirs: mkdirs, | ||
addSpaces: addSpaces, | ||
minimize: minimize | ||
} |