Skip to content

Commit

Permalink
Added bundle process to create single webworks.js. Used require aspec…
Browse files Browse the repository at this point in the history
…t as per discussion. Added new jake task to bundle together webworks.js: jake bundle
  • Loading branch information
Rowell Cruz committed Dec 7, 2011
1 parent 54aa0ca commit 0182d03
Show file tree
Hide file tree
Showing 6 changed files with 738 additions and 30 deletions.
5 changes: 5 additions & 0 deletions Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ task('default', [], require('./build/build'));
desc("package framework - jake build");
task('build', [], require('./build/build'));

desc("bundle webworks.js");
task('bundle', [], function () {
require('./build/build/bundler').bundle();
});

desc("start server");
task('start', [], function () {
require('./lib/server').start(process.argv);
Expand Down
39 changes: 39 additions & 0 deletions build/build/bundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require('fs');

module.exports = {
bundle: function () {
var fs = require('fs'),
files = [
"lib/public/builder.js"
],
include = function (files, transform) {
files = files.map ? files : [files];
return files.map(function (file) {
var str = fs.readFileSync(file, "utf-8") + "\n";
return transform ? transform(str, file) : str;
}).join('\n');
},
output = "",
filepath;

//include LICENSE
output += include("LICENSE", function (file) {
return "/*\n" + file + "\n*/\n";
});

//include require
output += include("dependencies/browser-require/require.js");

//include modules
output += include(files, function (file, path) {
return "require.define('" + path.replace(/lib\/public/, "").replace(/\.js$/, "") +
"', function (require, module, exports) {\n" + file + "});\n";
});

//include window.webworks
output += include("lib/public/window-webworks.js");

filepath = __dirname.replace(/\\/g, '/');
fs.writeFileSync(filepath + "/../../lib/public/webworks.js", output);
}
};
2 changes: 2 additions & 0 deletions build/build/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function _copyFiles() {
module.exports = function (src, baton) {
baton.take();

require('./bundler').bundle();

childProcess.exec(_copyFiles(), function (error, stdout, stderr) {
if (error) {
console.log(stdout);
Expand Down
38 changes: 15 additions & 23 deletions lib/public/builder.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
function prepareFeaturePath(filepath) {
filepath = filepath.replace(/local:\/\//, "");
return filepath.replace(/\.js$/, "");
filepath = filepath.replace(/local:\/\//, "").replace(/\.js$/, "");
return "../../" + filepath;
}

function requireLocalResource(localUrl, id) {
var requireId = id || prepareFeaturePath(localUrl);

if (require.main.require === undefined) { // in browser
window.require.load({
id: requireId,
url: localUrl
});
}
else { //in node
requireId = "../../" + requireId;
function requireLocalResource(localUrl) {
if (require.resolve !== undefined) { // in node
return require(prepareFeaturePath(localUrl));
}
return require(requireId);

return window.require(localUrl);
}

function buildNamespace(currentNamespace, namespaceParts, featureProperties) {
var featureId,
var featureId,
nextPart,
utils = requireLocalResource("local://lib/utils.js");

utils = requireLocalResource("local://lib/utils");
if (namespaceParts.length === 1) {
//base case, feature properties go here
featureId = namespaceParts[0];
Expand All @@ -45,17 +37,17 @@ function buildNamespace(currentNamespace, namespaceParts, featureProperties) {
}

function include(parent, featureIdList) {
var featureId,
var featureId,
featureProperties,
localUrl,
i;

for (i = 0; i < featureIdList.length; i++) {
featureId = featureIdList[i];
localUrl = "local://ext/" + featureId + "/client.js";
featureId = featureIdList[i];

localUrl = "local://ext/" + featureId + "/client";
featureProperties = requireLocalResource(localUrl);

buildNamespace(parent, featureId.split("."), featureProperties);
}
}
Expand Down

0 comments on commit 0182d03

Please sign in to comment.