Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

command line runner working

  • Loading branch information...
commit 5d4e09c28ce893acbfc97d8e7c050da61689a4ff 1 parent 29b47a8
@rmurphey authored
View
83 lib/plugins/use.js
@@ -1,45 +1,32 @@
-(function() {
-
-var buildMap = {};
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
-/* RequireJS Use Plugin v0.1.0
+/* RequireJS Use Plugin v0.2.0
* Copyright 2012, Tim Branyen (@tbranyen)
* use.js may be freely distributed under the MIT license.
*/
+(function() {
+
+// Cache used to map configuration options between load and write.
+var buildMap = {};
+
define({
version: "0.2.0",
// Invoked by the AMD builder, passed the path to resolve, the require
// function, done callback, and the configuration options.
- //
- // Configuration format
- // --------------------------------------------------------------------------
- //
- // The string property used in attach will resolve to window[stringProp]
- // Functions are evaluated in the scope of the window and passed all
- // arguments.
- //
- // require.config({
- // use: {
- // "libs/underscore": {
- // attach: "_"
- // },
- //
- // "libs/backbone": {
- // deps: ["use!underscore", "jquery"],
- // attach: function(_, $) {
- // return this.Backbone.noConflict();
- // }
- // }
- // }
- // });
- //
load: function(name, req, load, config) {
+ // Dojo provides access to the config object through the req function.
+ if (!config) {
+ config = require.rawConfig;
+ }
+
var module = config.use && config.use[name];
- // No module to load so return early.
+ // No module to load, throw.
if (!module) {
- return load();
+ throw new TypeError("Module '" + name + "' is undefined or does not" +
+ " have a `use` config. Make sure it exists, add a `use` config, or" +
+ " don't use use! on it");
}
// Attach to the build map for use in the write method below.
@@ -48,55 +35,61 @@ define({
// Read the current module configuration for any dependencies that are
// required to run this particular non-AMD module.
req(module.deps || [], function() {
+ var depArgs = arguments;
// Require this module
req([name], function() {
// Attach property
- attach = module.attach;
+ var attach = module.attach;
// If doing a build don't care about loading
- if (config.isBuild) {
+ if (config.isBuild) {
return load();
}
// Return the correct attached object
- if (typeof attach == "function") {
- return load(attach.apply(window, arguments));
+ if (typeof attach === "function") {
+ return load(attach.apply(this, depArgs));
}
// Use window for now (maybe this?)
- return load(window[attach]);
+ return load(this[attach]);
});
});
},
+ // Also invoked by the AMD builder, this writes out a compatible define
+ // call that will work with loaders such as almond.js that cannot read
+ // the configuration data.
write: function(pluginName, moduleName, write) {
var module = buildMap[moduleName];
+ var deps = module.deps;
var normalize = { attach: null, deps: "" };
// Normalize the attach to window[name] or function() { }
- if (typeof attach == "function") {
- normalize.attach = "return " + module.attach.toString() + ";";
+ if (typeof module.attach === "function") {
+ normalize.attach = module.attach.toString();
} else {
- normalize.attach = "return window['" + module.attach + "'];";
+ normalize.attach = [
+ "function() {",
+ "return typeof ", module.attach,
+ " !== \"undefined\" ? ", module.attach, " : void 0;",
+ "}"
+ ].join("");
}
// Normalize the dependencies to have proper string characters
- if (module.deps.length) {
- normalize.deps = "'" + module.deps.toString().split(",").join("','") + "'";
+ if (deps.length) {
+ normalize.deps = "'" + deps.toString().split(",").join("','") + "'";
}
// Write out the actual definition
write([
"define('", pluginName, "!", moduleName, "', ",
- "[", normalize.deps, "],",
-
- "function() {",
- normalize.attach,
- "}",
-
+ "[", normalize.deps, "], ", normalize.attach,
");\n"
].join(""));
}
});
})();
+
View
4 package.json
@@ -13,7 +13,9 @@
},
"dependencies": {
"express" : "2.5.x",
- "underscore" : "1.3.x"
+ "underscore" : "1.3.x",
+ "amdefine" : "0.0.2",
+ "expect.js" : "0.1.2"
},
"devDependencies": {
"mocha" : "1.0.x",
View
3  tests/app/arrays.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!underscore' ], function(_) {
describe("arrays", function() {
var a, fn;
View
3  tests/app/flowControl.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!underscore' ], function(_) {
describe("flow control", function() {
var fn;
View
3  tests/app/functions.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!underscore' ], function(_) {
describe("functions", function() {
var sayIt = function(greeting, name, punctuation) {
View
3  tests/app/modules.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!underscore' ], function(_) {
describe("the module pattern", function() {
var fn = function() {};
View
3  tests/app/objects.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!underscore' ], function(_) {
describe("objects and context", function() {
var a, b, C, fn;
View
3  tests/app/views.js
@@ -1,3 +1,6 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+if (typeof expect !== 'function') { var expect = require('expect.js'); }
+
define([ 'use!backbone', 'use!underscore', 'jquery' ], function(Backbone, _, $) {
describe("a Backbone view", function() {
var tpl, model, view;
View
32 tests/runner.js
@@ -1,13 +1,35 @@
+var requirejs = require('requirejs');
+var mocha = require('mocha');
+
+requirejs.config({
+ baseUrl : __dirname + '/../',
+ nodeRequire : require,
+ paths : {
+ // Libraries
+ underscore : 'lib/underscore',
+
+ // Shim Plugin
+ use : 'lib/plugins/use',
+ text : 'lib/plugins/text',
+ jquery : 'lib/jquery'
+ },
+
+ use : {
+ underscore : {
+ attach : '_'
+ }
+ }
+});
+
var tests = [
// link to test files here
-
'tests/app/arrays',
'tests/app/objects',
'tests/app/functions',
- 'tests/app/async',
- 'tests/app/views'
+ // 'tests/app/async',
+ // 'tests/app/views'
];
-require(tests, function() {
- mocha.run();
+requirejs(tests, function() {
+ // mocha.run();
});
Please sign in to comment.
Something went wrong with that request. Please try again.