Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #69 from systemjs/builder-class
Browse files Browse the repository at this point in the history
Builder becomes a class
  • Loading branch information
crisptrutski committed Feb 12, 2015
2 parents 4239d38 + 226da85 commit 6510359
Show file tree
Hide file tree
Showing 24 changed files with 308 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ test/sfx.js
test/tree-build.js
test/tree-build.js.map
test/memory-test
test/jspm_packages/
test/output.*
142 changes: 85 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Promise = require('rsvp').Promise;

var System = exports.System = require('systemjs');
var System = require('systemjs');
var fs = require('fs');

var asp = require('rsvp').denodeify;
Expand All @@ -14,15 +14,23 @@ var builder = require('./lib/builder');

var path = require('path');

var loader, pluginLoader;
function Builder(cfg) {
this.System = System;
this.loader = null;
this.reset();
if (typeof cfg == "string")
this.loadConfigSync(cfg);
else if (typeof cfg == "object")
this.config(cfg);
}

function reset() {
loader = new Loader(System);
Builder.prototype.reset = function() {
var loader = this.loader = new Loader(System);
loader.baseURL = System.baseURL;
loader.paths = { '*': '*.js' };
loader.config = System.config;

pluginLoader = new Loader(System);
var pluginLoader = new Loader(System);
pluginLoader.baseURL = System.baseURL;
pluginLoader.paths = { '*': '*.js' };
pluginLoader.config = System.config;
Expand All @@ -40,29 +48,27 @@ function reset() {

amdCompiler.attach(loader);
amdCompiler.attach(pluginLoader);
exports.loader = loader;
}
exports.reset = reset;

reset();
};

exports.build = function(moduleName, outFile, opts) {
Builder.prototype.build = function(moduleName, outFile, opts) {
var self = this;
opts = opts || {};

return exports.trace(moduleName, opts.config)
return this.trace(moduleName, opts.config)
.then(function(trace) {
return exports.buildTree(trace.tree, outFile, opts);
return self.buildTree(trace.tree, outFile, opts);
});
};

function compileLoad(load, opts, compilers) {
function compileLoad(loader, load, opts, compilers) {
return Promise.resolve()
.then(function() {
// note which compilers we used
compilers = compilers || {};
if (load.metadata.build == false) {
if (load.metadata.build === false) {
return {};
}
// jshint sub:true
else if (load.metadata.format == 'es6') {
compilers['es6'] = true;
return es6Compiler.compile(load, opts, loader);
Expand Down Expand Up @@ -92,12 +98,10 @@ function compileLoad(load, opts, compilers) {
});
}

function buildOutputs(tree, opts, sfxCompilers) {
function buildOutputs(loader, tree, opts, sfxCompilers) {
var names = Object.keys(tree);

// store plugins with a bundle hook to allow post-processing
var plugins = {};

var outputs = [];

return Promise.all(names.map(function(name) {
Expand All @@ -116,15 +120,15 @@ function buildOutputs(tree, opts, sfxCompilers) {
entry.loads.push(load);
}

return Promise.resolve(compileLoad(load, opts, sfxCompilers))
return Promise.resolve(compileLoad(loader, load, opts, sfxCompilers))
.then(outputs.push.bind(outputs));
}))
.then(function() {
// apply plugin "bundle" hook
return Promise.all(Object.keys(plugins).map(function(pluginName) {
var entry = plugins[pluginName];
if (entry.bundle)
return Promise.resolve(entry.bundle.call(pluginLoader, entry.loads, opts))
return Promise.resolve(entry.bundle.call(loader.pluginLoader, entry.loads, opts))
.then(outputs.push.bind(outputs));
}));
})
Expand All @@ -133,19 +137,21 @@ function buildOutputs(tree, opts, sfxCompilers) {
});
}

exports.buildTree = function(tree, outFile, opts) {
Builder.prototype.buildTree = function(tree, outFile, opts) {
var loader = this.loader;

opts = opts || {};
opts.outFile = outFile;

return buildOutputs(tree, opts, false)
return buildOutputs(loader, tree, opts, false)
.then(function(outputs) {
outputs.unshift('"format register";\n');
return builder.writeOutput(opts, outputs, loader.baseURL);
});
};

exports.buildSFX = function(moduleName, outFile, opts) {
Builder.prototype.buildSFX = function(moduleName, outFile, opts) {
var loader = this.loader;

opts = opts || {};
var config = opts.config;
Expand All @@ -156,10 +162,10 @@ exports.buildSFX = function(moduleName, outFile, opts) {

var compilers = {};
opts.normalize = true;
return exports.trace(moduleName, config)
return this.trace(moduleName, config)
.then(function(trace) {
moduleName = trace.moduleName;
return buildOutputs(trace.tree, opts, compilers);
return buildOutputs(loader, trace.tree, opts, compilers);
})
.then(function(_outputs) {
outputs = _outputs;
Expand Down Expand Up @@ -201,25 +207,37 @@ exports.buildSFX = function(moduleName, outFile, opts) {
});
};

exports.loadConfig = function(configFile) {
return Promise.resolve()
.then(function() {
return asp(fs.readFile)(path.resolve(process.cwd(), configFile))
})
.then(function(source) {
var curSystem = global.System;
global.System = {
config: function(cfg) {
loader.config(cfg);
pluginLoader.config(cfg);
}
};
new Function(source.toString()).call(global);
global.System = curSystem;
});
function executeConfigFile(loader, source) {
var curSystem = global.System;
global.System = {
config: function(cfg) {
loader.config(cfg);
loader.pluginLoader.config(cfg);
}
};
// jshint evil:true
new Function(source.toString()).call(global);
global.System = curSystem;
}

exports.config = function(config) {
var resolvePath = path.resolve.bind(path, process.cwd());

Builder.prototype.loadConfig = function(configFile) {
var self = this;
return asp(fs.readFile)(resolvePath(configFile))
.then(executeConfigFile.bind(null, this.loader))
.then(function() { return self; });
};

Builder.prototype.loadConfigSync = function(configFile) {
var source = fs.readFileSync(resolvePath(configFile));
executeConfigFile(this.loader, source);
};

Builder.prototype.config = function(config) {
var loader = this.loader;
var pluginLoader = loader.pluginLoader;

var cfg = {};
for (var p in config) {
if (p != 'bundles')
Expand All @@ -230,14 +248,15 @@ exports.config = function(config) {
};

// returns a new tree containing tree1 n tree2
exports.intersectTrees = function(tree1, tree2) {
Builder.prototype.intersectTrees = function(tree1, tree2) {
var name;
var intersectTree = {};

var tree1Names = [];
for (var name in tree1)
for (name in tree1)
tree1Names.push(name);

for (var name in tree2) {
for (name in tree2) {
if (tree1Names.indexOf(name) == -1)
continue;

Expand All @@ -248,33 +267,35 @@ exports.intersectTrees = function(tree1, tree2) {
};

// returns a new tree containing tree1 + tree2
exports.addTrees = function(tree1, tree2) {
Builder.prototype.addTrees = function(tree1, tree2) {
var name;
var unionTree = {};

for (var name in tree2)
for (name in tree2)
unionTree[name] = tree2[name];

for (var name in tree1)
for (name in tree1)
unionTree[name] = tree1[name];

return unionTree;
};

// returns a new tree containing tree1 - tree2
exports.subtractTrees = function(tree1, tree2) {
Builder.prototype.subtractTrees = function(tree1, tree2) {
var name;
var subtractTree = {};

for (var name in tree1)
for (name in tree1)
subtractTree[name] = tree1[name];

for (var name in tree2)
for (name in tree2)
delete subtractTree[name];

return subtractTree;
};

// copies a subtree out of the tree
exports.extractTree = function(tree, moduleName) {
Builder.prototype.extractTree = function(tree, moduleName) {
var outTree = {};
return visitTree(tree, moduleName, null, function(load) {
outTree[load.name] = load;
Expand All @@ -284,9 +305,12 @@ exports.extractTree = function(tree, moduleName) {
});
};

exports.trace = function(moduleName, config, includePlugins) {
Builder.prototype.trace = function(moduleName, config, includePlugins) {
var loader = this.loader;
var pluginLoader = loader.pluginLoader;

if (config) {
exports.config(config);
this.config(config);
}

var System = loader.global.System;
Expand All @@ -301,7 +325,7 @@ exports.trace = function(moduleName, config, includePlugins) {
.then(function(_moduleName) {
moduleName = _moduleName;
loader.global.System = System;
return visitTree(loader.loads, moduleName, includePlugins && loader.pluginLoader, function(load) {
return visitTree(loader.loads, moduleName, includePlugins && pluginLoader, function(load) {
traceTree[load.name] = load;
});
})
Expand All @@ -315,7 +339,7 @@ exports.trace = function(moduleName, config, includePlugins) {
loader.global.System = System;
throw e;
});
}
};

function visitTree(tree, moduleName, pluginLoader, visit, seen) {
seen = seen || [];
Expand All @@ -328,7 +352,7 @@ function visitTree(tree, moduleName, pluginLoader, visit, seen) {
var load = tree[moduleName];

if (!load)
return Promise.resolve()
return Promise.resolve();

// visit the deps first
return Promise.all(load.deps.map(function(dep) {
Expand All @@ -347,3 +371,7 @@ function visitTree(tree, moduleName, pluginLoader, visit, seen) {
return visit(load);
});
}

Builder.legacy = new Builder();

module.exports = Builder;
2 changes: 1 addition & 1 deletion lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ exports.writeOutput = function(opts, outputs, baseURL) {
output = minify(output, opts.outFile, opts.mangle);

if (opts.outFile)
return writeOutputFile(opts, output, basePath);
return writeOutputFile(opts, output, basePath).then(function() { return output; });
else
return rsvp.resolve(output);
};
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "SystemJS Build Tool ===",
"main": "index.js",
"directories": {
"test": "test"
"test": "test",
"baseURL": "test",
"lib": "test",
"packages": "test/jspm_packages"
},
"dependencies": {
"mkdirp": "^0.5.0",
Expand All @@ -17,6 +20,8 @@
},
"devDependencies": {
"es6-module-loader": "0.13.0",
"mocha": "~2.0.0",
"chai": "^1.10.0",
"react-tools": "^0.12.1"
},
"repository": {
Expand All @@ -32,5 +37,11 @@
"bugs": {
"url": "https://github.com/systemjs/builder/issues"
},
"homepage": "https://github.com/systemjs/builder"
"homepage": "https://github.com/systemjs/builder",
"jspm": {
"directories": {
"baseURL": "test",
"lib": "test"
}
}
}
11 changes: 11 additions & 0 deletions test/cfg-manual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
System.config({
baseURL: '',
paths: {
'jquery-cdn': 'https://code.jquery.com/jquery-2.1.1.min.js'
},
meta: {
'jquery-cdn': {
build: false
}
}
});
7 changes: 5 additions & 2 deletions test/cfg.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
System.config({
baseURL: '.',
baseURL: 'test',
paths: {
'jquery-cdn': 'https://code.jquery.com/jquery-2.1.1.min.js'
},
map: {
'jquery-cdn': '@empty'
},
meta: {
'jquery-cdn': {
build: false
}
}
});
});
1 change: 1 addition & 0 deletions test/chain/first.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
second = require('./second')
Loading

0 comments on commit 6510359

Please sign in to comment.