Skip to content

Commit

Permalink
[loader] Check if requires list passes tests and add optionals to it
Browse files Browse the repository at this point in the history
This way _sort() has knowledge of optional requires to sort them as
dependencies
  • Loading branch information
juandopazo committed Feb 27, 2014
1 parent b44bd17 commit 63802d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
57 changes: 36 additions & 21 deletions src/loader/js/loader.js
Expand Up @@ -1380,6 +1380,20 @@ Y.Loader.prototype = {
}
return r;
},

_canBeAttached: function (m) {
if (typeof m === 'string') {
m = this.getModule(m);
}
if (m && m.optTest) {
if (!m.hasOwnProperty('_testResult')) {
m._testResult = m.optTest(Y);
}
return m._testResult;
}
return true;
},

/**
* Returns an object containing properties for all modules required
* in order to load the requested module
Expand Down Expand Up @@ -1434,6 +1448,27 @@ Y.Loader.prototype = {
return mod.expanded;
}

r = mod.requires;
for (i = 0, length = r.length; i < length; i++) {
if (!this._canBeAttached(r[i])) {
this._failed = true;
}
}

// Optional dependencies are dependencies that may or may not be
// available.
// This feature was designed specifically to be used when transpiling
// ES6 modules, in order to use polyfills and regular scripts that define
// global variables without having to import them since they should be
// available in the global scope.
if (optReqs) {
for (i = 0, length = optReqs.length; i < length; i++) {
m = this.getModule(optReqs[i]);
if (m && this._canBeAttached(m)) {
mod.requires.push(m.name);
}
}
}

d = [];
hash = {};
Expand All @@ -1460,12 +1495,6 @@ Y.Loader.prototype = {
hash[r[i]] = true;
m = this.getModule(r[i]);
if (m) {
if (typeof m._testResult === 'undefined' && m.optTest) {
m._testResult = m.optTest(Y);
}
if (m._testResult === false) {
this._failed = true;
}
add = this.getRequires(m);
intl = intl || (m.expanded_map &&
(INTL in m.expanded_map));
Expand Down Expand Up @@ -1595,21 +1624,6 @@ Y.Loader.prototype = {
}
}

// Optional dependencies are dependencies that may or may not be
// available.
// This feature was designed specifically to be used when transpiling
// ES6 modules, in order to use polyfills and regular scripts that define
// global variables without having to import them since they should be
// available in the global scope.
if (optReqs) {
for (i = 0, length = optReqs.length; i < length; i++) {
m = this.getModule(optReqs[i]);
if (m) {
d.push(m.name);
}
}
}

mod._parsed = false;

if (intl) {
Expand Down Expand Up @@ -2424,6 +2438,7 @@ Y.log('Undefined module: ' + mname + ', matched a pattern: ' +
Y.log('public insert() ' + (type || '') + ', ' +
Y.Object.keys(this.required), "info", "loader");
if (this._failed && this.onEnd) {
this._failed = false;
this.onEnd({
msg: 'notregistered',
success: false
Expand Down
5 changes: 1 addition & 4 deletions src/yui/js/yui.js
Expand Up @@ -739,10 +739,7 @@ with any configuration info required for the module.
if (loader) {
def = loader.getModule(name);
if (def) {
if (typeof def._testResult === 'undefined' && def.optTest) {
def._testResult = def.optTest(Y);
}
if (def._testResult === false) {
if (!loader._canBeAttached(def)) {
Y.log('Failed to attach module ' + name, 'warn', 'yui');
return true;
}
Expand Down

0 comments on commit 63802d8

Please sign in to comment.