Skip to content

Commit

Permalink
Fixed production compatibility with asset packaging.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuckabee committed Aug 17, 2011
1 parent 8a592bd commit 81aecd1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cache
public/top.json
public/newest.json
public/compressed
public/css/compiled
public/js/compiled
public/compressed/*
public/css/compiled/*
public/js/compiled/*
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.configure('development', function(){
});

app.configure('production', function(){
PORT = 8000;
PORT = 80;
app.use(express.errorHandler());
});

Expand Down
33 changes: 20 additions & 13 deletions lib/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/
var compress = false,

PUBLIC_DIR = __dirname.replace(/\/lib$/, '/public'),
CSS_DIR = 'public/css/',
JS_DIR = 'public/js/',
public_dir = '',
css_dir = '',
js_dir = '',
JS_HASH = '',
CSS_HASH = '',

fs = require('fs'),
crypto = require('crypto'),
ams = require('ams').build.create(PUBLIC_DIR),
ams = require('ams'),

css = [
'style.css'
Expand All @@ -26,19 +26,19 @@ var compress = false,

function compress_everything() {
css.forEach(function(filename) {
ams.add(CSS_DIR + filename, CSS_DIR);
ams.add(css_dir + filename, css_dir);
});
js.forEach(function(filename) {
ams.add(JS_DIR + filename, JS_DIR);
ams.add(js_dir + filename, js_dir);
});

ams
.process({
uglifyjs: false,
cssvendor: false,
cssdataimg: false,
csshost: { host: 'http://hn_reader.joshhuckabee.com'},
cssabspath: {host: '..'},
csshost: false,
cssabspath: false,
cssimport: false,
htmlabspath: false,
cssmin: false,
Expand All @@ -60,28 +60,29 @@ function compress_everything() {
jstransport: false,
texttransport: false
})
.write(PUBLIC_DIR + '/compressed')
.write(public_dir + '/compressed')
.end();

hashifyAssets();
}

function hashifyAssets() {
fs.readFile(PUBLIC_DIR + '/compressed/combined.js', 'utf8', function(err, js) {
fs.readFile(public_dir + '/compressed/combined.js', 'utf8', function(err, js) {
if (!err) {
JS_HASH = crypto.createHash('md5').update(js).digest("hex");
console.log('Writing '+ JS_HASH + '.js');
fs.writeFile(PUBLIC_DIR + '/js/compiled/'+JS_HASH+'.js', js);
fs.writeFile(public_dir + '/js/compiled/'+JS_HASH+'.js', js);
}
else {
console.log(err);
}
});

fs.readFile(PUBLIC_DIR +'/compressed/combined.css', 'utf8', function(err, css) {
fs.readFile(public_dir +'/compressed/combined.css', 'utf8', function(err, css) {
if (!err) {
CSS_HASH = crypto.createHash('md5').update(css).digest("hex");
console.log('Writing '+ CSS_HASH + '.css');
fs.writeFile(PUBLIC_DIR + '/css/compiled/'+ CSS_HASH +'.css', css);
fs.writeFile(public_dir + '/css/compiled/'+ CSS_HASH +'.css', css);
}
else {
console.log(err);
Expand Down Expand Up @@ -114,8 +115,14 @@ function scripts(options) {
}

function addHandler(app) {
public_dir = app.set('public');
css_dir = public_dir + '/css/';
js_dir = public_dir + '/js/';

compress = app.set('env') == 'production';

ams = ams.build.create(public_dir);

if (compress) {
compress_everything();
}
Expand Down

0 comments on commit 81aecd1

Please sign in to comment.