Skip to content

Commit

Permalink
Pass dependencies through to legacy factory. Fixes #270.
Browse files Browse the repository at this point in the history
  • Loading branch information
unscriptable committed Jun 16, 2014
1 parent 65dc25b commit 662138c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
32 changes: 20 additions & 12 deletions src/curl/loader/legacy.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
return { return {


'load': function (resId, require, callback, cfg) { 'load': function (resId, require, callback, cfg) {
var exports, factory, deps, dontAddFileExt, url, options, countdown; var exports, factory, depIds, dontAddFileExt, url, options,
countdown, deps;


exports = cfg['exports'] || cfg.exports; exports = cfg['exports'] || cfg.exports;
factory = cfg['factory'] || cfg.factory; factory = cfg['factory'] || cfg.factory;
if (!exports && !factory) { if (!exports && !factory) {
throw new Error('`exports` or `factory` required for legacy: ' + resId); throw new Error('`exports` or `factory` required for legacy: ' + resId);
} }


deps = [].concat(cfg['requires'] || cfg.requires || []); depIds = [].concat(cfg['requires'] || cfg.requires || []);
deps = [];
dontAddFileExt = cfg['dontAddFileExt'] || cfg.dontAddFileExt; dontAddFileExt = cfg['dontAddFileExt'] || cfg.dontAddFileExt;
dontAddFileExt = dontAddFileExt dontAddFileExt = dontAddFileExt
? new RegExp(dontAddFileExt) ? new RegExp(dontAddFileExt)
Expand All @@ -119,15 +121,15 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
order: true, order: true,
// set a fake mimetype if we need to wait and don't support // set a fake mimetype if we need to wait and don't support
// script.async=false. // script.async=false.
mimetype: hasAsyncFalse || !deps.length ? '' : 'text/cache' mimetype: hasAsyncFalse || !depIds.length ? '' : 'text/cache'
}; };


// hasAsyncFalse, nodeps: load | _export // hasAsyncFalse, nodeps: load | _export
// hasAsyncFalse, deps: getDeps+load | _export // hasAsyncFalse, deps: getDeps+load | _export
// !hasAsyncFalse, nodeps: load | _export // !hasAsyncFalse, nodeps: load | _export
// !hasAsyncFalse, deps: getDeps+load | reload | _export // !hasAsyncFalse, deps: getDeps+load | reload | _export


if (deps.length) { if (depIds.length) {
countdown = 2; countdown = 2;
getDeps(); getDeps();
load(); load();
Expand All @@ -138,28 +140,34 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
} }


function getDeps () { function getDeps () {
// start process of getting deps, then either export or reload require(depIds, _continue, reject);
require(deps, hasAsyncFalse ? _export : reload, reject);
} }


function load () { function load () {
// load script, possibly with a fake mimetype // load script, possibly with a fake mimetype
loadScript(options, _export, reject); loadScript(options, _export, reject);
} }


function reload () { function _continue () {
// if we faked the mimetype, we need to refetch. // save dependencies
// (hopefully, from cache, if cache headers allow.) deps = arguments;
options.mimetype = ''; if (!hasAsyncFalse) {
loadScript(options, _export, reject); // if we faked the mimetype, we need to refetch.
// (hopefully, from cache, if cache headers allow.)
options.mimetype = '';
load();
}
else {
_export();
}
} }


function _export () { function _export () {
var exported; var exported;
if (--countdown > 0) return; if (--countdown > 0) return;
if (factory) { if (factory) {
try { try {
exported = factory.call(global, resId); exported = factory.call(global, resId, deps);
} }
catch (ex) { catch (ex) {
reject(new Error('Factory for legacy ' + resId + ' failed: ' + ex.message)); reject(new Error('Factory for legacy ' + resId + ' failed: ' + ex.message));
Expand Down
20 changes: 15 additions & 5 deletions test/legacy.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


<script> <script>


var curl, factoryId, factoryDeps;

curl = { curl = {
debug: true, debug: true,
paths: { paths: {
Expand All @@ -26,7 +28,11 @@
main: 'plain_old_2', main: 'plain_old_2',
config: { config: {
loader: 'curl/loader/legacy', loader: 'curl/loader/legacy',
factory: function () { return testDomain.awesome; }, factory: function (id, deps) {
factoryId = id;
factoryDeps = deps;
return testDomain.awesome;
},
requires: ['test1'] requires: ['test1']
} }
} }
Expand All @@ -51,15 +57,19 @@
'usingRequire' 'usingRequire'
] ]
).then( ).then(
function (three, saucer, foobar,definedUpTop, usingRequire) { function (three, saucer, foobar, definedUpTop, usingRequire) {
write('A module with dependencies loaded successfully if 3 == ' + three + '.'); write('A module with dependencies loaded successfully if 3 == ' + three + '.');
write('If the following line says something then the plain js files loaded in the right order:'); write('If the following line says something then the plain js files loaded in the right order:');


//write(testDomain.foo.bar + ' ' + testDomain.awesome.sauce); //write(testDomain.foo.bar + ' ' + testDomain.awesome.sauce);
write('Did js file with no deps return a value? ' + (foobar == 'whizzah!' ? 'SUCCESS' : 'FAILED')); write('Did js file with no deps return a value? ' + (foobar == 'whizzah!' ? 'SUCCESS' : 'FAILED'));


write('Did module with non-amd deps load dep correctly? ' + (definedUpTop == 'whizzah!' ? 'SUCCESS' : 'FAILED')); write('Did module with non-amd deps load deps correctly? ' + (definedUpTop == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
write('Did module with non-amd deps load dep correctly? ' + (usingRequire == 'whizzah!' ? 'SUCCESS' : 'FAILED')); write('Did module with non-amd deps load deps correctly? ' + (usingRequire == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
console.log(definedUpTop, usingRequire);
// test factory
write('Did legacy script with factory pass id? ' + (factoryId === 'test2/plain_old_2' ? 'SUCCESS' : 'FAILED'));
write('Did legacy script with factory pass dependencies? ' + (factoryDeps[0] === testDomain.foo.bar ? 'SUCCESS' : 'FAILED'));
} }
); );


Expand Down

0 comments on commit 662138c

Please sign in to comment.