Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
HTML and CSS files are not minified on deploy.
Browse files Browse the repository at this point in the history
This adds CSS/HTML compression (and comment removal) to the compress
build task (used in `jake deploy`).

This adds two new (local) NPM modules:

- http://npmjs.org/package/cssmin
- http://npmjs.org/package/html-minifier

This also address this GitHub Issue:
https://github.com/blackberry/Ripple-UI/issues/213
  • Loading branch information
brentlintner committed Sep 13, 2012
1 parent 4527b9b commit 45f0c5c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
87 changes: 68 additions & 19 deletions build/build/compress.js
Expand Up @@ -14,36 +14,85 @@
* limitations under the License.
*/
var childProcess = require('child_process'),
fs = require('fs'),
_c = require('./conf'),
utils = require('./utils'),
childProcess = require('child_process'),
jWorkflow = require('jWorkflow');
jWorkflow = require('jWorkflow'),
cssmin = require('cssmin').cssmin,
htmlmin = require('html-minifier').minify;

module.exports = function (prev, baton) {
var files = [],
cmd = jWorkflow.order();

function compress(file) {
cmd.andThen(function (prev, baton) {
baton.take();
childProcess.exec('uglifyjs --overwrite ' + file, function (error) {
if (error) {
process.stdout.write("Something bad happened. Is uglify-js installed?");
process.stdout.write(error);
process.exit(1);
} else {
function compressJS(workflow, file) {
workflow.andThen(function (prev, baton) {
baton.take();
childProcess.exec('uglifyjs --overwrite ' + file, function (err) {
if (err) {
process.stdout.write("Something bad happened. Is uglify-js installed?");
process.stdout.write(err);
process.exit(1);
} else {
baton.pass();
}
});
});
}

function compressCSS(workflow, file) {
workflow.andThen(function (prev, baton) {
baton.take();
fs.readFile(file, "utf-8", function (err, data) {
if (err) {
process.stdout.write("Something bad happened while minifying css.");
process.stdout.write(err);
process.exit(1);
} else {
fs.writeFile(file, cssmin(data), function () {
baton.pass();
}
});
});
}
});
}
});
}

function compressHTML(workflow, file) {
workflow.andThen(function (prev, baton) {
baton.take();
fs.readFile(file, "utf-8", function (err, data) {
if (err) {
process.stdout.write("Something bad happened while minifying html.");
process.stdout.write(err);
process.exit(1);
} else {
fs.writeFile(file, htmlmin(data, {removeComments: true, collapseWhitespace: true}), function () {
baton.pass();
});
}
});
});
}

module.exports = function (prev, baton) {
var cmd = jWorkflow.order(),
files = {
css: [],
html: [],
js: []
};

process.stdout.write("compressing...");
baton.take();

utils.collect(_c.DEPLOY, files);
function matches(regex, str) {
return !!str.match(regex);
}

utils.collect(_c.DEPLOY, files.js, matches.bind(this, /\.js$/));
utils.collect(_c.DEPLOY, files.css, matches.bind(this, /\.css$/));
utils.collect(_c.DEPLOY, files.html, matches.bind(this, /\.html$/));

files.forEach(compress);
files.js.forEach(compressJS.bind(this, cmd));
files.css.forEach(compressCSS.bind(this, cmd));
files.html.forEach(compressHTML.bind(this, cmd));

cmd.start(baton.pass);
};
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -16,7 +16,9 @@
"dependencies": {
"connect": "2.3.6",
"jsdom": "0.2.14",
"jWorkflow": "*"
"jWorkflow": "*",
"cssmin": "0.3.1",
"html-minifier": "0.4.5"
},
"bundledDependencies": [
"connect"
Expand Down

0 comments on commit 45f0c5c

Please sign in to comment.