Permalink
Browse files

Move plugins into separate files, use same wrapper for all files, ref…

…actor
  • Loading branch information...
1 parent 32a573b commit f434d34b80e458276da57fcf03b60cd3d468e4f9 @mixu committed Mar 10, 2014
View
@@ -21,7 +21,7 @@ lint:
--disable=6 \
--max_line_length=120 \
--custom_jsdoc_tags=api \
- --exclude_directories=node_modules,lib/runner/package-commonjs/resources,test \
+ --exclude_directories=node_modules,lib/runner/commonjs/resources,test,lib/require \
--max_line_length=120 --disable=0131,300,2,1,6 \
-r .
View
@@ -7,17 +7,19 @@ var fs = require('fs'),
AmdList = require('../lib/list/amd.js'),
loadAMDConfig = require('../lib/runner/amd/load-config.js'),
runner = require('../lib/runner/amd'),
- Cache = require('minitask').Cache;
+ Cache = require('minitask').Cache,
+ nodeResolve = require('resolve');
-var opts = require('optimist')
+var optimist = require('optimist')
.usage('Usage: $0 --include <file/dir ...>')
.options({
'amd': { },
+ 'cache': { default: true },
'include': { },
'main': { },
})
.boolean('amd'),
- argv = opts.parse(process.argv);
+ argv = optimist.parse(process.argv);
if(!argv['include']) {
console.log('Usage: --include <file/dir>');
@@ -32,9 +34,21 @@ if(!argv['include']) {
Minilog.enable();
-opts = {
- 'cache-method': 'stat',
- 'cache-path': os.tmpDir() + '/gluejs-' + new Date().getTime()
+var homePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
+homePath = (typeof homePath === 'string' ? path.normalize(homePath) : process.cwd());
+
+if(!argv['cache-path']) {
+ argv['cache-path'] = homePath + path.sep + '.gluejs-cache' + path.sep;
+}
+
+// if the cache is disabled, then use a temp path
+if(!argv.cache) {
+ argv['cache-path'] = os.tmpDir() + '/gluejs-' + new Date().getTime();
+}
+
+var opts = {
+ 'cache-method': argv['cache-method'] || 'stat',
+ 'cache-path': argv['cache-path']
};
if(!Array.isArray(argv.include)) {
@@ -43,10 +57,10 @@ if(!Array.isArray(argv.include)) {
// determine main
var main = argv.main || argv.include[0],
- basepath = argv.basepath || path.dirname(main);
+ basepath = path.resolve(process.cwd(), argv.basepath) || path.dirname(main);
// resolve paths relative to process.cwd
-['list-files', 'out'].forEach(function(key) {
+['list-files', 'out', 'vendor-base'].forEach(function(key) {
if(argv[key]) {
argv[key] = path.resolve(process.cwd(), argv[key]);
}
@@ -74,6 +88,21 @@ if(argv.amd && main) {
opts.amdresolve.baseDir = basepath;
}
+function findModule(name) {
+ var result = '';
+ try {
+ result = nodeResolve.sync(name, { basedir: process.cwd() });
+ } catch(e) {
+ try {
+ result = nodeResolve.sync(name, { basedir: __dirname });
+ } catch(e) {
+ console.error('Cannot find module ' + name + ' from ' + process.cwd() + ' or ' + __dirname);
+ throw e;
+ }
+ }
+ return result;
+}
+
var list = new AmdList(opts);
var cache = Cache.instance({
@@ -108,6 +137,18 @@ list.exec(function(err, files) {
}
});
+ // prefix: function(name, filepath) {}
+ var plugins = {};
+
+ Object.keys(argv).forEach(function(name) {
+ var matched = (typeof name === 'string' ? name.match(/plugin\-(.*)/) : false);
+ if(matched) {
+ var ext = matched[1];
+ argv[name] = findModule(argv[name]);
+ plugins[ext] = require(argv[name]);
+ }
+ });
+
runner({ files: files }, {
main: argv.main,
basepath: basepath,
@@ -121,17 +162,10 @@ list.exec(function(err, files) {
exclude: vendor.exclude,
extras: ['underscore'],
command: argv.command,
- plugins: {
- 'jade': function(name, filepath) {
- var jade = require('jade');
- return "define('" + name + "', ['jade-runtime'], function(jade){ return " +
- jade.compile(fs.readFileSync(filepath).toString(), { client: true, compileDebug: false }) + "});\n";
- },
- 'json': function(name, filepath) {
- return "define('" + name + "', [], function(){ return " +
- fs.readFileSync(filepath).toString() + "});\n";
- }
- }
+ nomin: vendor.nomin || [],
+ plugins: plugins,
+ // set this so that builds are invalidated as the version changes
+ 'gluejs-version': require('../package.json').version
}, fs.createWriteStream(argv['out']), function(err, processedFiles) {
if(argv['list-files']) {
fs.appendFileSync(argv['list-files'], processedFiles.join('\n'));
View
@@ -151,7 +151,7 @@ if(argv['npm']) {
'Directly include the file path to module, e.g. --include ./node_modules/foo instead.\n');
}
-// set this so that builds are invalidates as the version changes
+// set this so that builds are invalidated as the version changes
g.set('gluejs-version', require('../package.json').version);
// -- out
@@ -19,10 +19,10 @@ module.exports = function wrapAMDVendor(name, content, deps, globalName) {
// however, since the end result of those transformations is just to map to a specific form
// of define() call, we can just wrap the module and deal with the four (!) or so variants
// of define calls that r.js allows.
- result += '\n' + '(function(define) {\ndefine.amd = {};\n'
+ result += '\n' + '(function(define) {\ndefine.amd = {};\n';
result += content;
result += template2.replace(/%name%/g, JSON.stringify(name))
.replace(/%deps%/g, JSON.stringify(deps));
}
return result;
-}
+};
@@ -1,9 +0,0 @@
-var fs = require('fs'),
- path = require('path');
-
-module.exports = function wrapAMD(name, content, deps, basePath) {
- return content.toString().replace('define(', 'define(' +
- JSON.stringify(name) + ', ' +
- JSON.stringify(deps) + ', '
- );
-};
View
@@ -63,7 +63,7 @@ function DetectiveList(opts) {
return onDone(null, queue);
});
-};
+}
DetectiveList.prototype = new List();
@@ -79,7 +79,7 @@ function DetectiveList(opts) {
});
});
});
-};
+}
DetectiveList.prototype = new List();
@@ -47,7 +47,7 @@ exports.postlude = function(opts) {
} else {
str += fs.readFileSync(__dirname +'/umd.min.js');
}
- str += 'umd(r(' + JSON.stringify(opts['root-file']) +'), ' + JSON.stringify(opts['export']) + ');'
+ str += 'umd(r(' + JSON.stringify(opts['root-file']) +'), ' + JSON.stringify(opts['export']) + ');';
break;
}
if(opts['global-require']) {
@@ -13,7 +13,6 @@ var fs = require('fs'),
amdDeps = require('./deps.js'),
spawn = require('../../file-tasks/spawn.js'),
// tasks
- wrapAMD = require('../../file-tasks/wrap-amd.js'),
wrapAMDVendor = require('../../file-tasks/wrap-amd-vendor.js'),
log = require('minilog')('amd');
@@ -91,7 +90,7 @@ module.exports = function(list, opts, out, onDone) {
missing = missing.filter(function(name) {
var hasPath = vendorPaths[name],
- isIgnored = (typeof vendorPaths[name] === 'boolean' && vendorPaths[name])
+ isIgnored = (typeof vendorPaths[name] === 'boolean' && vendorPaths[name]),
hasExternal = vendorComplete[name];
return !isIgnored && !hasPath && !hasExternal;
});
@@ -101,7 +100,7 @@ module.exports = function(list, opts, out, onDone) {
if(failed.length > 0) {
console.log('Failed to resolve vendor files:');
- var tmplObj = {}
+ var tmplObj = {};
failed.map(function(name) {
tmplObj[name] = '';
});
@@ -182,7 +181,7 @@ module.exports = function(list, opts, out, onDone) {
var tasks = [];
// "simple mode": one --command which only applies to .js files
- if(opts.command && path.extname(filepath) == '.js') {
+ if(opts.command && path.extname(filepath) == '.js' && opts.nomin.indexOf(moduleName) == -1) {
tasks.push(function() {
return spawn({
name: filepath, // full path
@@ -256,7 +255,7 @@ module.exports = function(list, opts, out, onDone) {
var tasks = [];
// "simple mode": one --command which only applies to .js files
- if(opts.command && path.extname(filepath) == '.js') {
+ if(opts.command && path.extname(filepath) == '.js' && opts.nomin.indexOf(moduleName) == -1) {
tasks.push(function() {
return spawn({
name: filepath, // full path
@@ -266,7 +265,7 @@ module.exports = function(list, opts, out, onDone) {
}
tasks.push(function(input, done) {
- done(null, wrapAMD(moduleName, input, amdDeps(cache, filepath), opts.basepath));
+ done(null, wrapAMDVendor(moduleName, input, amdDeps(cache, filepath), false));
});
var task = new Task(tasks).input(function() {
@@ -5,8 +5,7 @@ module.exports = function loadAMDConfig(filepath) {
// the config specification for RJS is painful to parse as it's not a JSON file
// but rather a JS file that defines as specifically named variable
var sandbox = {};
- vm.runInNewContext(
- fs.readFileSync(filepath).toString(), sandbox);
+ vm.runInNewContext(fs.readFileSync(filepath).toString(), sandbox);
return sandbox.require;
-}
+};
@@ -15,7 +15,7 @@ SortDependencies.prototype.resolve = function(name) {
};
SortDependencies.prototype.isEmpty = function() {
- return this.items.length == 0;
+ return this.items.length === 0;
};
SortDependencies.prototype.ignoreResolved = function(item) {
@@ -29,7 +29,7 @@ SortDependencies.prototype.next = function() {
var self = this;
this.items.sort(function(a, b) {
var diff = self.ignoreResolved(b) - self.ignoreResolved(a);
- if (diff == 0) {
+ if (diff === 0) {
return a.name.localeCompare(b.name);
}
return diff;
View
@@ -34,7 +34,10 @@
"minilog": "~2.0.3",
"progress": "~1.1.2",
"browser-resolve": "~1.2.2",
- "detective": "~2.4.0"
+ "detective": "~2.4.0",
+ "amd-resolve": "~0.1.1",
+ "amdetective": "0.0.1",
+ "resolve": "~0.6.1"
},
"devDependencies": {
"mocha": "1.x",

0 comments on commit f434d34

Please sign in to comment.