Skip to content

Commit

Permalink
adding require+define to cordova object, removing from global scope b…
Browse files Browse the repository at this point in the history
…y wrapping entire script in another closure. tweaked browser test suite to work with new approach.
  • Loading branch information
filmaj committed Mar 9, 2012
1 parent 56fa89f commit 07b5166
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ task('build', ['clean'], function () {
fs = require('fs'),
packager = require("./build/packager");

packager.bundle("blackberry");
packager.bundle("playbook");
packager.bundle("ios");
packager.bundle("wp7");
packager.bundle("android");
packager.write("blackberry");
packager.write("playbook");
packager.write("ios");
packager.write("wp7");
packager.write("android");

util.puts(fs.readFileSync("build/dalek", "utf-8"));
});
Expand Down
23 changes: 21 additions & 2 deletions build/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function walk(dir, doRecursive) {
}
return results;
}

// Simply inline includes the specified file(s) with an optional transform function.
function include(files, transform) {
files = files.map ? files : [files];
return files.map(function (file) {
Expand All @@ -37,6 +39,10 @@ function include(files, transform) {
}
}).join('\n');
}

// Includes the specified file(s) with optional overriding id
// Wraps the specified file(s) in a define statement which implicitly
// creates a closure as well.
function drop(files, id) {
return include(files, function(file, path) {
var define_id = (typeof id != 'undefined' && id.length > 0 ? id : path.replace(/lib\//, "cordova/").replace(/\.js$/, ''));
Expand Down Expand Up @@ -96,19 +102,32 @@ module.exports = {
return "/*\n" + file + "\n*/\n";
});

// wrap the entire thing in one more closure
// closure closure closure
output += "(function() {\n";

//include modules
output += this.modules(platform);

// HACK: this gets done in bootstrap.js anyways, once native side is ready + domcontentloaded is fired. Do we need it?
// HACK: this gets done in bootstrap.js anyways, once native side is ready + domcontentloaded is fired.
// TODO: Do we need it?
output += "window.cordova = require('cordova');\n";

//include bootstrap
output += include('lib/bootstrap.js');
// TODO: we don't need platform-specific bootstrap.

// TODO/HACK: we don't need platform-specific bootstrap.
// those can go into the init function inside the platform/*.js
// files
output += include('lib/bootstrap/' + platform + '.js');

// closing the closure har har
output += "})();";

return output;
},
write: function (platform) {
var output = this.bundle(platform);
fs.writeFileSync(__dirname + "/../pkg/cordova." + platform + ".js", output);
}
};
2 changes: 2 additions & 0 deletions lib/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function createEvent(type, data) {
}

var cordova = {
define:define,
require:require,
/**
* Methods to add/remove your own addEventListener hijacking on document + window.
*/
Expand Down
2 changes: 1 addition & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = {
return '<script src="' + file.replace(/^.*test/, "test") +
'" type="text/javascript" charset="utf-8"></script>';
}).join('');
modules = packager.modules('test');
modules = packager.bundle('test');
doc = html.replace(/<!-- TESTS -->/g, specs).replace(/"##MODULES##"/g, modules);
res.end(doc);
});
Expand Down
2 changes: 2 additions & 0 deletions test/suite.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

<script type="text/javascript">
"##MODULES##"
var define = cordova.define;
var require = cordova.require;
</script>

<!-- TESTS -->
Expand Down
9 changes: 5 additions & 4 deletions test/test.require.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
describe("require", function () {
it("exists off of window", function () {
expect(require).toBeDefined();
expect(define).toBeDefined();
describe("require + define", function () {
it("exist off of cordova", function () {
var cordova = require('cordova');
expect(cordova.require).toBeDefined();
expect(cordova.define).toBeDefined();
});

describe("when defining", function () {
Expand Down

0 comments on commit 07b5166

Please sign in to comment.