Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit af3722d

Browse files
committed
updates, move Loader.prototype.parse into loader implementation
1 parent b5a3f66 commit af3722d

File tree

3 files changed

+292
-340
lines changed

3 files changed

+292
-340
lines changed

lib/loader.js

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ function logloads(loads) {
262262
if (load.status != 'loading')
263263
return;
264264

265-
var depsList;
266265
if (instantiateResult === undefined) {
267266
load.address = load.address || 'anon' + ++anonCnt;
268267
load.kind = 'declarative';
269-
depsList = loader.loaderObj.parse(load);
268+
// parse sets load.declare, load.depsList
269+
loader.loaderObj.parse(load);
270270
}
271271
else if (typeof instantiateResult == 'object') {
272-
depsList = instantiateResult.deps || [];
272+
load.depsList = instantiateResult.deps || [];
273273
load.execute = instantiateResult.execute;
274274
load.kind = 'dynamic';
275275
}
@@ -278,7 +278,7 @@ function logloads(loads) {
278278

279279
// 15.2.4.6 ProcessLoadDependencies
280280
load.dependencies = [];
281-
load.depsList = depsList;
281+
var depsList = load.depsList;
282282

283283
var loadPromises = [];
284284
for (var i = 0, l = depsList.length; i < l; i++) (function(request, index) {
@@ -1012,6 +1012,58 @@ function logloads(loads) {
10121012

10131013
var _newModule = Loader.prototype.newModule;
10141014

1015+
1016+
/*
1017+
* Traceur-specific Parsing Code for Loader
1018+
*/
1019+
(function() {
1020+
function checkForErrors(output, load) {
1021+
if (output.errors.length) {
1022+
for (var i = 0, l = output.errors.length; i < l; i++)
1023+
console.error(output.errors[i]);
1024+
throw new Error('Parse of ' + load.name + ', ' + load.address + ' failed, ' + output.errors.length);
1025+
}
1026+
}
1027+
1028+
// parse function is used to parse a load record
1029+
// Returns an array of ModuleSpecifiers
1030+
Loader.prototype.parse = function(load) {
1031+
if (!traceur) {
1032+
if (typeof window == 'undefined')
1033+
traceur = require('traceur');
1034+
else if (__global.traceur)
1035+
traceur = __global.traceur;
1036+
else
1037+
throw new TypeError('Include Traceur for module syntax support');
1038+
}
1039+
1040+
console.assert(load.source, 'Non-empty source');
1041+
1042+
var depsList;
1043+
1044+
load.kind = 'declarative';
1045+
1046+
var compiler = new traceur.Compiler();
1047+
var options = System.traceurOptions || {};
1048+
options.modules = 'instantiate';
1049+
var output = compiler.stringToTree({content: load.source, options: options});
1050+
checkForErrors(output);
1051+
1052+
output = compiler.treeToTree(output);
1053+
checkForErrors(output);
1054+
1055+
output = compiler.treeToString(output);
1056+
checkForErrors(output);
1057+
var source = output.js;
1058+
var sourceMap = output.generatedSourceMap;
1059+
1060+
if (__global.btoa && sourceMap)
1061+
source += '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) + '\n';
1062+
1063+
__eval(source, __global, load);
1064+
}
1065+
})();
1066+
10151067
if (typeof exports === 'object')
10161068
module.exports = Loader;
10171069

@@ -1022,4 +1074,32 @@ function logloads(loads) {
10221074

10231075
})();
10241076

1077+
// Define our eval outside of the scope of any other reference defined in this
1078+
// file to avoid adding those references to the evaluation scope.
1079+
var __curRegister;
1080+
function __eval(__source, __global, load) {
1081+
// Hijack System.register to set declare function
1082+
__curRegister = System.register;
1083+
System.register = function(name, deps, declare) {
1084+
if (typeof name != 'string') {
1085+
declare = deps;
1086+
deps = name;
1087+
}
1088+
// store the registered declaration as load.declare
1089+
// store the deps as load.deps
1090+
load.declare = declare;
1091+
load.depsList = deps;
1092+
}
1093+
try {
1094+
eval('var __moduleName = "' + (load.name || '').replace('"', '\"') + '"; (function() { ' + __source + ' \n }).call(__global);');
1095+
}
1096+
catch(e) {
1097+
if (e.name == 'SyntaxError' || e.name == 'TypeError')
1098+
e.message = 'Evaluating ' + (load.name || load.address) + '\n\t' + e.message;
1099+
throw e;
1100+
}
1101+
1102+
System.register = __curRegister;
1103+
}
1104+
10251105
})(typeof global !== 'undefined' ? global : this);

0 commit comments

Comments
 (0)