Skip to content

Commit

Permalink
added output for file name in minified files, using one method to do …
Browse files Browse the repository at this point in the history
…file check, json lookup tables
  • Loading branch information
dtan committed Jun 27, 2011
1 parent 6144ed0 commit 14469c1
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@ var utils = require('./railway_utils'),
path = require('path'),
fs = require('fs'),
crypto = require('crypto'),
regexps = {
'cached': /^cache\//,
'isHttp': /^https?:\/\/|\/\//
},
exts = {
'css': '.css',
'js' : '.js'
},
paths = {
'css': '/stylesheets/',
'js' : '/javascripts/'
},
undef;

function generic_tag (name, inner, params, override) {
function generic_tag(name, inner, params, override) {
return '<' + name + html_tag_params(params, override) + '>' + inner + '</' + name + '>';
}

function generic_sc_tag (name, params, override) {
function generic_sc_tag(name, params, override) {
return '<' + name + html_tag_params(params, override) + ' />';
}

function data_param (key) {
function data_param(key) {
if (this[key]) {
this['data-' + key] = this[key];
delete this[key];
}
}

function HelperSet (ctl) {
function HelperSet(ctl) {
var controller = ctl;
this.controller = ctl;

Expand All @@ -34,6 +46,21 @@ function HelperSet (ctl) {
}
}

function checkProd() {
return app.settings.env === 'production';
}

function checkFile(type, file) {
var isExternalFile = regexps.isHttp.test(file),
isCached = file.match(regexps.cached),
href = !isExternalFile ? paths[type] + file + exts[type] : file,
isProd = checkProd();
if (!isCached && !isProd && !isExternalFile) {
href += '?' + Date.now()
}
return href;
}

module.exports = new HelperSet(null);

/**
Expand All @@ -44,7 +71,6 @@ module.exports.personalize = function (controller) {
return new HelperSet(controller);
};


// <link href="/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />
HelperSet.prototype.stylesheet_link_tag = function () {
var args = Array.prototype.slice.call(arguments);
Expand All @@ -55,11 +81,9 @@ HelperSet.prototype.stylesheet_link_tag = function () {
var links = [];
mergeFiles('stylesheets', args).forEach(function (file) {
delete options.href;
var href = "/stylesheets/" + file + ".css";
if (!file.match(/^cache\//) && app.settings.env !== 'production') {
href += '?' + Date.now()
}
links.push(generic_sc_tag('link', options, {href: href}));
// there should be an option to change the /stylesheets/ folder
var href = checkFile('css', file);
links.push(generic_sc_tag('link', options, { href: href }));
});
return links.join('\n ');
};
Expand All @@ -84,35 +108,31 @@ HelperSet.prototype.javascript_include_tag = function () {
}
var scripts = [];
mergeFiles('javascripts', args).forEach(function (file) {
var localScript = !file.match(/^https?:\/\//);
var href = localScript ? "/javascripts/" + file + ".js" : file;
if (localScript && app.settings.env !== 'production' && !file.match(/^cache\//)) {
href += '?' + Date.now();
}
var href = checkFile('js', file);
delete options.src;
scripts.push(generic_tag('script', '', options, {src: href}));
});
return scripts.join('\n ');
};

var merged = {
stylesheets: {ext: '.css'},
javascripts: {ext: '.js'}
stylesheets: {ext: exts.css},
javascripts: {ext: exts.js}
};

function mergeFiles (scope, files) {
function mergeFiles(scope, files) {
// ensure that feature is enabled
if (app.disabled('merge ' + scope)) {
return files;
}
var ext = merged[scope].ext;
var result = [];
var shasum = crypto.createHash('sha1');
var minify = [];
var ext = merged[scope].ext,
result = [],
shasum = crypto.createHash('sha1'),
minify = [];

// only merge local files
files.forEach(function (file) {
if (file.search(/^https?:\/\//) === -1) {
if (!regexps.isHttp.test(file)) {
shasum.update(file);
minify.push(file);
} else {
Expand All @@ -122,7 +142,6 @@ function mergeFiles (scope, files) {

// calculate name of new script based on names of merged files
var digest = shasum.digest('hex');

// check cache state (undefined = not cached, false = cache in progress, String = cached)
var cached = merged[scope][digest];
if (cached) {
Expand All @@ -145,6 +164,7 @@ function mergeFiles (scope, files) {
if (exists) {
counter += 1;
fs.readFile(filename, 'utf8', function (err, data) {
stream.write('// /' + scope + '/' + file + ext + '\n');
stream.write(data + '\n');
done();
});
Expand Down

0 comments on commit 14469c1

Please sign in to comment.