Browse files

add data-curl-run='prod || dev' functionality

  • Loading branch information...
1 parent e69dc66 commit 39960301a002f445cced49d36ed49d98e6094b6b @unscriptable unscriptable committed Feb 7, 2013
Showing with 57 additions and 4 deletions.
  1. +20 −4 src/curl.js
  2. +26 −0 test/data-curl-run-failover.html
  3. +11 −0 test/stuff/run-module.js
View
24 src/curl.js
@@ -52,6 +52,7 @@
findDotsRx = /(\.)(\.?)(?:$|\/([^\.\/]+.*)?)/g,
removeCommentsRx = /\/\*[\s\S]*?\*\/|\/\/.*?[\n\r]/g,
findRValueRequiresRx = /require\s*\(\s*(["'])(.*?[^\\])\1\s*\)|[^\\]?(["'])/g,
+ splitMainDirectives = /\s*\|\|\s*/,
cjsGetters,
core;
@@ -1148,22 +1149,37 @@
}
function _config (cfg, callback, errback) {
- var promise;
+ var pPromise, mPromise, main;
if (cfg) {
core.setApi(cfg);
userCfg = core.config(cfg);
// check for preloads
if ('preloads' in cfg) {
- promise = new CurlApi(cfg['preloads'], undef, errback, preload, true);
+ pPromise = new CurlApi(cfg['preloads'], undef, errback, preload, true);
// yes, this is hacky and embarrassing. now that we've got that
// settled... until curl has deferred factory execution, this
// is the only way to stop preloads from dead-locking when
// they have dependencies inside a bundle.
- setTimeout(function () { preload = promise; }, 0);
+ setTimeout(function () { preload = pPromise; }, 0);
}
// check for main module(s). this waits for preloads implicitly.
if ('main' in cfg) {
- return new CurlApi(cfg['main'], callback, errback)
+ main = cfg['main'];
+ if (isType(main, 'String')) {
+ // process data-curl-run="prod || dev" syntax
+ main = main.split(splitMainDirectives);
+ mPromise = new Promise();
+ new CurlApi(main[0].split(','), mPromise.resolve, main[1]
+ ? function (ex) {
+ new CurlApi(main[1].split(','), mPromise.resolve, mPromise.reject);
+ }
+ : mPromise.reject
+ );
+ }
+ else {
+ mPromise = new CurlApi(main, callback, errback);
+ }
+ return mPromise;
}
}
}
View
26 test/data-curl-run-failover.html
@@ -0,0 +1,26 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<title>data-curl-run test (with failover)</title>
+
+<script>
+
+ function callback (value) {
+ write('SUCCESS: data-curl-run should failover and execute second module.');
+ write((value == 1 ? 'SUCCESS' : 'FAILED') + ': data-curl-run should have loaded a module.');
+ function write (msg) {
+ curl('domReady!', function () {
+ document.body.appendChild(document.createElement('div')).innerHTML = msg;
+ });
+ }
+ }
+
+
+</script>
+<script src="../src/curl.js" data-curl-run="bogus||stuff/run-module"></script>
+
+</head>
+<body>
+
+</body>
+</html>
View
11 test/stuff/run-module.js
@@ -0,0 +1,11 @@
+define(['curl'], function (curl) {
+
+ curl.config({
+ baseUrl: '',
+ paths: {
+ curl: '../src/curl'
+ }
+ });
+ curl(['stuff/one'], callback);
+
+});

0 comments on commit 3996030

Please sign in to comment.