Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 245 lines (235 sloc) 7.21 KB
#!/usr/bin/env node
/*! (C) Andrea Giammarchi */
function Queue(args, f) {
// http://webreflection.blogspot.ie/2012/03/tweet-sized-queue-system.html
setTimeout(args.next = function next() {
return (f = args.shift()) ? !!f(args) || !0 : !1;
}, 0);
return args;
}
function cleanPath(fileName) {
return fileName.slice(config.path.length).replace(reJSend, "");
}
function readableSize(bytes) {
var i = 2, size;
while (1024 <= bytes && i--) {
bytes /= 1024;
}
switch (i) {
case 2: size = "bytes"; break;
case 1: size = "KB"; break;
case 0: size = "MB"; break;
}
return (i === 2 ? bytes : bytes.toFixed(2)) + " " + size;
}
function resolveArguments(args, keepProgramName) {
var a = [].slice.call(args, 0);
if (/(?:^|\/|\\)node(?:\.exe)?$/.test(a[0])) a.shift();
keepProgramName || a.shift();
return a;
}
function watch() {
clearTimeout(watchInterval);
watchInterval = setTimeout(function () {
watchInterval = 0;
Queue(program.slice());
}, 1000);
}
var
args = resolveArguments(process.argv),
withYUI = args.indexOf("--yui"),
noStrict = args.indexOf("--nostrict"),
useUYI = withYUI < 0 ? false : !!args.splice(withYUI, 1),
useNoStrict = noStrict < 0 ? false : !!args.splice(noStrict, 1),
withWatch = args.indexOf("--watch"),
useWatch = withWatch < 0 ? false : !!args.splice(withWatch, 1),
validArgs = !!args.length,
reJSend = /\.js$/,
reIsRequire = /(?:^|\\|\/)require\.js$/,
path = require("path"),
fs = require("fs"),
zlib = require("zlib"),
spawn = require('child_process').spawn,
config = {path:path.join(args.shift(), "./")},
require_src = fs.readFileSync(
path.join(__dirname, "src", "require.js"),
"utf-8"
).replace(/\/\/:dev([^\1]*)(\/\/dev:)/g, ""),
grab = Function("config",
"return " + RegExp.$1.replace(/^[^\1]*(function grab\()/, "$1")
)(config),
build_folder = path.join(__dirname, "build"),
exist = fs.existsSync(build_folder) || fs.mkdirSync(build_folder),
tmp_file = path.join(__dirname, "build", "tmp"),
tmp_uid = 0,
tmp_parsed = 0,
require_parsed = path.join(__dirname, "build", "require_parsed.js"),
require_build = path.join(__dirname, "build", "require.js"),
yuicompressor = path.join(__dirname, "jar", "yuicompressor-2.4.6.jar"),
modules = {},
program = [],
watchInterval = 0,
buildNO = 1,
watchList = [],
preCode = /^this\.exports=function\(\)\{/,
postCode = /\}\.call\(this\);?$/,
wrapCode = function (code) {
return "this.exports=function(){"+code+"}.call(this);";
},
unwrapCode = function (code) {
return code.replace(preCode, "").replace(postCode, "");
},
$compile = useUYI ?
function $compile(regular, compressed, onclose) {
if (regular !== require_parsed) {
fs.writeFileSync(
regular + ".___.js", wrapCode(
fs.readFileSync(regular, "utf-8")
), "utf-8"
);
regular += ".___.js";
}
spawn(
"java", [
"-jar",
yuicompressor,
regular,
"-o",
compressed
], { stdio: 'inherit' }
).on("close", function () {
if (regular !== require_parsed) {
fs.unlinkSync(regular);
}
onclose();
});
} :
function $compile(regular, compressed, onclose) {
var
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify,
content = fs.readFileSync(regular, "utf-8"),
ast = jsp.parse(regular === require_parsed ? content : wrapCode(content))
;
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
fs.writeFileSync(compressed, pro.gen_code(ast), "utf-8");
process.nextTick(onclose);
},
compile = function (regular, compressed, onclose) {
try {
$compile(regular, compressed, onclose);
} catch(e) {
onclose(e);
}
}
;
if (validArgs) {
program = Queue([
function (q) {
console.log("");
console.log("[require_client] build #" + buildNO++);
fs.writeFileSync(require_parsed, require_src, "utf-8");
q.next();
},
function first(q) {
compile(require_parsed, require_build, q.next);
},
function readDir(q, dir) {
fs.readdirSync(dir || (dir = config.path)).forEach(function (fileName) {
fileName = path.join(dir, fileName);
// avoid the inclusion of require itself as a module
if (reIsRequire.test(fileName)) return;
if (reJSend.test(fileName)) {
var tmp_name = tmp_file + tmp_uid++ + ".js";
tmp_parsed++;
if (watchList.indexOf(fileName) < 0) {
watchList.push(fileName);
useWatch && fs.watch(fileName, {persistent:true}, watch);
}
console.log("compiling module " + cleanPath(fileName));
compile(fileName, tmp_name, function (e) {
if (fs.existsSync(tmp_name)) {
modules[cleanPath(
fileName
)] = unwrapCode(fs.readFileSync(tmp_name, "utf-8"));
fs.unlinkSync(tmp_name);
} else {
console.log("[FAIL] unable to compile " + fileName);
if (e) {
console.log(e.message);
console.log("");
}
}
--tmp_parsed || q.next();
});
} else if(fs.statSync(fileName).isDirectory()) {
readDir(q, fileName);
}
});
tmp_parsed || q.next();
},
function (q) {
fs.unlinkSync(require_parsed);
fs.writeFileSync(
require_build,
fs.readFileSync(
require_build, "utf-8"
)
.replace(/(strict:[^,]+?)(,path:('|")[^\3]*?\3)/, "\n" + (
useNoStrict ? "strict:false" : "$1"
) + "\n")
.replace(/\{production:1\}/, JSON.stringify(
modules, null, " "
)) + ";", "utf-8"
);
console.log(readableSize(
fs.statSync(require_build).size
));
fs
.createReadStream(require_build)
.pipe(
zlib.createGzip({
level: zlib.Z_BEST_COMPRESSION
})
)
.pipe(
fs.createWriteStream(require_build + ".gz")
)
.on("close", function () {
console.log(readableSize(
fs.statSync(require_build + ".gz").size
) + " gzipped");
fs.unlinkSync(require_build + ".gz");
if (args.length && path.resolve(args[0]) !== require_build) {
fs.writeFileSync(
args.shift(),
fs.readFileSync(require_build, "utf-8"),
"utf-8"
);
fs.unlinkSync(require_build);
}
q.next();
})
;
},
function () {
if (useWatch) {
useWatch = false;
console.log("");
console.log("watching " + config.path + " like a boss");
console.log("");
fs.watch(config.path, {persistent:true}, watch);
}
}
]).slice();
} else {
console.log("");
console.log("Usage:");
console.log(" node require_client JS/source/folder [destination_file] [--yui]");
console.log("");
console.log("if destination_file is not specified, built will be in");
console.log("if --yui is not present, UglifyJS will be used as default");
console.log(require_build);
console.log("");
}
Jump to Line
Something went wrong with that request. Please try again.