feature(join) /join/ -> /join:

This commit is contained in:
coderaiser 2014-08-14 07:54:43 -04:00
parent bf20f6aadb
commit 14597a31fa
4 changed files with 108 additions and 76 deletions

View file

@ -8,7 +8,7 @@
<link rel="icon" href="/favicon.ico" />
<title>{{ title }}</title>
<link rel=stylesheet href=/join/css/reset.css:css/style.css:css/icons.css:css/help.css>
<link rel=stylesheet href=/join:css/reset.css:css/style.css:css/icons.css:css/help.css>
<noscript>
<link rel=stylesheet href="/css/nojs.css">
@ -84,11 +84,9 @@
document.body.appendChild(script);
}
function getJoinURL(files) {
var regExp = new RegExp(',', 'g'),
url = '/join/' + files;
url = url.replace(regExp, ':');
function getJoinURL(names) {
var prefix = '/join:',
url = prefix + names.join(':');
return url;
}

View file

@ -1,34 +1,28 @@
(function(scope) {
'use strict';
var Scope = scope.window ? window : global;
var Scope = scope.window ? window : global,
PREFIX = '/join';
if (typeof module === 'object' && module.exports)
module.exports = join;
else
Scope.join = join;
function join (names) {
var url,
regExp = new RegExp(',', 'g'),
nameStr = names + '';
function join (prefix, names) {
var url;
if (!names) {
names = prefix;
prefix = PREFIX;
}
if (!names)
throw(Error('names must be array!'));
nameStr = nameStr.replace(regExp, ':');
nameStr = rmFirstSlash(nameStr);
url = '/join/' + nameStr;
return url;
}
function rmFirstSlash(url) {
var regExp = new RegExp('^/'),
is = url.match(regExp);
if (is)
url = url.replace('/', '');
url = prefix + ':' + names.join(':');
return url;
}

View file

@ -121,7 +121,14 @@
funcs = [
Rest,
Route,
join(beforeJoin),
join({
minify: function() {
var isMinify = main.config.minify;
return isMinify;
}
}),
controller
],
@ -223,43 +230,6 @@
});
}
function minify(name, callback) {
Minify.optimize(name, callback);
}
function beforeJoin(names, callback) {
var i, name, check, minName,
func,
funcs = [],
dir = DIR,
config = main.config,
n = names.length;
for (i = 0; i < n; i++) {
name = Path.join(dir, names[i]);
names[i] = name;
if (config.minify) {
check = checkExt(name);
if (check) {
minName = Minify.getName(name);
if (name !== minName)
names[i] = minName;
}
funcs.push(Util.exec.with(minify, name));
}
}
func = Util.exec.ret(callback, names);
Util.exec.if(!config.minify, func, function(callback) {
Util.exec.parallel(funcs, callback);
});
}
function checkExt(name) {
var ret;

View file

@ -6,23 +6,42 @@
files = require(DIR_SERVER + 'files'),
ponse = require(DIR_SERVER + 'ponse'),
Minify = require(DIR_SERVER + 'minify'),
Util = require(DIR + 'util'),
zlib = require('zlib');
path = require('path'),
zlib = require('zlib'),
PREFIX = '/join';
module.exports = function(before) {
return join.bind(null, before);
module.exports = function(options) {
return join.bind(null, options);
};
function join(before, req, res, next) {
var names,
function join(options, req, res, next) {
var prefix, dir,
names,
isMinify,
isFunc,
exec = Util.exec,
readFunc = exec.with(readPipe, req, res),
read = exec.with(readPipe, req, res),
path = ponse.getPathName(req),
regExp = new RegExp('^/join/'),
regExpFile = new RegExp('^/join/join.js$'),
regExp = new RegExp('^' + PREFIX + ':'),
regExpFile = new RegExp('^' + PREFIX + '/join.js$'),
isJoin = path.match(regExp),
isJoinFile = path.match(regExpFile);
if (!options)
options = {};
isFunc = Util.isFunction(options.minify);
if (isFunc)
isMinify = options.minify();
else
isMinify = options.minify;
if (isJoinFile) {
ponse.sendFile({
name : __dirname + '/../join.js',
@ -33,25 +52,38 @@
} else if (!isJoin) {
next();
} else {
names = parse(path);
prefix = options.prefix || PREFIX;
dir = options.dir || __dirname + '/../../';
exec.if(!before, readFunc, function(callback) {
before(names, callback);
names = parse(prefix, dir, path);
exec.if(!isMinify, function(namesNew) {
var is = Util.isArray(namesNew);
if (is)
names = namesNew;
read(names);
}, function(callback) {
uglify(names, callback);
});
}
return isJoin;
}
function parse(url) {
function parse(prefix, dir, url) {
var names,
isStr = typeof url === 'string';
if (!isStr)
throw(Error('url must be string!'));
names = url.replace('/join', '')
.split(':');
names = url.replace(prefix + ':', '')
.split(':')
.map(function(name) {
return path.join(dir, name);
});
return names;
}
@ -99,4 +131,42 @@
if (isGzip)
gzip.pipe(res);
}
function minify(name, callback) {
Minify.optimize(name, callback);
}
function retMinify(name) {
return minify.bind(null, name);
}
function checkExt(name) {
var ret;
ret = Util.checkExt(name, ['js', 'css', 'html']);
return ret;
}
function uglify(names, callback) {
var func,
funcs = [];
names = names.map(function(name) {
var is;
funcs.push(retMinify(name));
is = checkExt(name);
if (is) {
name = Minify.getName(name);
}
return name;
});
func = Util.exec.ret(callback, names);
Util.exec.parallel(funcs, func);
}
})();