Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add bin/gluejs, add reporting about package file sizes.

  • Loading branch information...
commit 6656c05b8e98eb961f759801464818a9819e5303 1 parent fb980ec
@mixu authored
View
114 bin/gluejs
@@ -0,0 +1,114 @@
+#!/usr/bin/env node
+
+var fs = require('fs'),
+ path = require('path'),
+ Glue = require('../lib/glue.js'),
+ log = require('minilog')('cmd'),
+ args = require('argsparser').parse();
+
+var build = new Glue(),
+ paths = (Array.isArray(args['--include']) ? args['--include'] : [args['--include']]).filter(function(v) { return !!v; }),
+ basepath = args['--basepath'] || process.cwd(),
+ // --out
+ outfile = ( args['--out'] ? relpath(args['--out']) : false),
+ npmLookups = (args['--npm'] ? args['--npm'].split(',') : []),
+ replaces = (args['--replace'] ? args['--replace'].split(',') : []),
+ global = (args['--global'] ? args['--global'].split(',') : 'Foo');
+
+function relpath(p) {
+ if(p[0] == '/') {
+ return p;
+ }
+ return path.join(basepath, p);
+}
+
+if(paths.length == 0) {
+ console.log('');
+ console.log('Usage: gluejs --include <file/dir ...> --out filename.js');
+ console.log(' --include [path] Paths to import.');
+ console.log(' --out [path] File to write. Default: stdout');
+ console.log(' --global [name] Name of the global to export. Default: "Foo"');
+ console.log(' --basepath [path] Base path for the list of files. Default: process.cwd().');
+ console.log(' --main [name] Name of the main file/module to export. Default: index.js')
+ console.log(' --exclude [regexp] Exclude files matching given regular expression.');
+ console.log(' --replace [name=expr,...] Bind require("name") to the expression, e.g. jQuery to window.$.');
+ console.log(' --npm [name1,name2,fromdir] Include a single package from a directory in the package.');
+ console.log(' --npm [package.json] Include all normal dependencies from a package.json in the package.')
+ console.log(' --silent Disable verbose output.');
+ process.exit();
+}
+
+// --silent
+if(!args['--silent'] && args['--out']) {
+ require('../lib/index.js');
+}
+
+// --basepath
+build.basepath(basepath);
+
+// --include
+paths.forEach(function(p) {
+ build.include(relpath(p));
+});
+
+// --global
+build.export(global);
+
+// --main
+if(args['--main']) {
+ build.main(args['--main']);
+}
+
+// --exclude
+if(args['--exclude']) {
+ if(!Array.isArray(args['--exclude'])) {
+ args['--exclude'] = [args['--exclude']];
+ }
+ args['--exclude'].forEach(function(re) {
+ build.exclude(new RegExp(re));
+ });
+}
+
+// --replace
+if(args['--replace']) {
+ var replaces = args['--replace'].split(',');
+ replaces.forEach(function(item) {
+ var parts = item.split('=', 2);
+ build.replace(parts[0], parts[1]);
+ });
+}
+
+// --npm
+var stack = [];
+npmLookups.forEach(function(part) {
+ var packagePath = relpath(part);
+ if(part.match(/package.json$/) || fs.existsSync(path.join(part, './package.json'))) {
+ if(stack.length > 0) {
+ stack.forEach(function(name) {
+ build.npm(name, packagePath);
+ });
+ stack = [];
+ } else {
+ build.npm(packagePath);
+ }
+ } else {
+ stack.push(part);
+ }
+});
+// if no package.json file is specified, assume npm names are looked up relative to the basepath
+if(stack.length > 0) {
+ stack.forEach(function(name) {
+ build.npm(name, basepath);
+ });
+}
+
+build.render(function(err, txt) {
+ if(err) throw err;
+
+ if(outfile) {
+ fs.writeFileSync(outfile, txt);
+ log.info('Wrote '+outfile);
+ } else {
+ console.log(txt);
+ }
+});
View
34 lib/glue.js
@@ -5,39 +5,6 @@ var fs = require('fs'),
Minilog = require('minilog'),
log = Minilog('glue');
-var styles = {
- //styles
- 'bold' : ['\033[1m', '\033[22m'],
- 'italic' : ['\033[3m', '\033[23m'],
- 'underline' : ['\033[4m', '\033[24m'],
- 'inverse' : ['\033[7m', '\033[27m'],
- //grayscale
- 'white' : ['\033[37m', '\033[39m'],
- 'grey' : ['\033[90m', '\033[39m'],
- 'black' : ['\033[30m', '\033[39m'],
- //colors
- 'blue' : ['\033[34m', '\033[39m'],
- 'cyan' : ['\033[36m', '\033[39m'],
- 'green' : ['\033[32m', '\033[39m'],
- 'magenta' : ['\033[35m', '\033[39m'],
- 'red' : ['\033[31m', '\033[39m'],
- 'yellow' : ['\033[33m', '\033[39m']
-},
-levelMap = { debug: 1, info: 2, warn: 3, error: 4 };
-
-function style(str, style) {
- return styles[style][0] + str + styles[style][1];
-}
-
-Minilog
- .pipe(Minilog.backends.nodeConsole)
- .format(function(name, level, args) {
- var colors = { debug: 'blue', info: 'cyan', warn: 'yellow', error: 'red' };
- return (name ? style(name +' ', 'grey') : '')
- + (level ? style(level, colors[level]) + ' ' : '')
- + args.join(' ');
- });
-
var requireCode = fs.readFileSync(__dirname + '/require.js', 'utf8')
.replace(/\/*([^/]+)\/\n/g, '')
.replace(/\n/g, '')
@@ -51,6 +18,7 @@ var defaults = {
function Renderer(options) {
var self = this;
options || (options = { });
+
// Package
this.build = new Package();
this.build.basepath = options.reqpath = defaults.reqpath;
View
34 lib/index.js
@@ -0,0 +1,34 @@
+var Minilog = require('minilog');
+
+var styles = {
+ //styles
+ 'bold' : ['\033[1m', '\033[22m'],
+ 'italic' : ['\033[3m', '\033[23m'],
+ 'underline' : ['\033[4m', '\033[24m'],
+ 'inverse' : ['\033[7m', '\033[27m'],
+ //grayscale
+ 'white' : ['\033[37m', '\033[39m'],
+ 'grey' : ['\033[90m', '\033[39m'],
+ 'black' : ['\033[30m', '\033[39m'],
+ //colors
+ 'blue' : ['\033[34m', '\033[39m'],
+ 'cyan' : ['\033[36m', '\033[39m'],
+ 'green' : ['\033[32m', '\033[39m'],
+ 'magenta' : ['\033[35m', '\033[39m'],
+ 'red' : ['\033[31m', '\033[39m'],
+ 'yellow' : ['\033[33m', '\033[39m']
+},
+levelMap = { debug: 1, info: 2, warn: 3, error: 4 };
+
+function style(str, style) {
+ return styles[style][0] + str + styles[style][1];
+}
+
+Minilog.pipe(Minilog.backends.nodeConsole).format(function(name, level, args) {
+ var colors = { debug: 'blue', info: 'cyan', warn: 'yellow', error: 'red' };
+ return (name ? style(name +' ', 'grey') : '')
+ + (level ? style(level, colors[level]) + ' ' : '')
+ + args.join(' ');
+});
+
+module.exports = require('./glue.js');
View
18 lib/package.js
@@ -84,7 +84,8 @@ Package.prototype._runHandlers = function(selfId, onDone) {
// create tasks for each file
// We will only allow one handler to match each file, since this makes things less confusing
- var handlers = [].concat(globalHandlers, this.handlers);
+ var handlers = [].concat(globalHandlers, this.handlers),
+ sizes = [];
this.files.forEach(function(filename) {
var matching = handlers.filter(function(handler) {
@@ -105,7 +106,8 @@ Package.prototype._runHandlers = function(selfId, onDone) {
tasks.push( function(done) {
opts.filename = filename;
opts.relativeFilename = opts.relative(filename);
- log.debug('Processing ('+self.name + '): ' +filename + ' as ' + opts.relative(filename));
+ log.debug('Processing ('+(self.name ? self.name : 'root') + '): ' +filename + ' as ' + opts.relative(filename));
+ sizes.push({ filename: opts.relative(filename), size: fs.statSync(filename).size });
matching[0].handler(opts, function(filename, source) {
var relpath = relative(filename);
result[relpath] = 'function(module, exports, require){' +
@@ -117,10 +119,18 @@ Package.prototype._runHandlers = function(selfId, onDone) {
});
// serial execution for tasks
- series(tasks, function() { onDone(result); });
+ series(tasks, function() {
+ // summarize sizes
+ var total = sizes.reduce(function(prev, item) { return prev + item.size}, 0);
+ sizes.sort(function(a, b) { return b.size - a.size; });
+ sizes.forEach(function(item, index) {
+ log.info('' + (index+1) + '. ' + item.filename + ' '+ (item.size / 1024).toFixed(2) +'k (' + Math.floor( item.size / total * 100 )+ ' %)');
+ });
+ log.info('Package total: ' + (total / 1024).toFixed(2) +'k');
+ onDone(result);
+ });
};
-
Package.prototype.render = function(result, onDone) {
var self = this,
selfId = result.length,
View
8 package.json
@@ -16,10 +16,14 @@
"module",
"package"
],
- "main": "lib/glue.js",
+ "main": "lib/index.js",
+ "bin": {
+ "gluejs": "./bin/gluejs"
+ },
"dependencies": {
"package-json-resolver": "git://github.com/mixu/package-json-resolver.git#master",
- "minilog": "0.0.4"
+ "minilog": "0.0.4",
+ "argsparser": "0.0.6"
},
"devDependencies": {
"mocha": "1.x"
Please sign in to comment.
Something went wrong with that request. Please try again.