Bones/node_modules/systemjs/lib/global.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-05-17 13:45:25 -04:00
/*
SystemJS Global Format
Supports
metadata.deps
metadata.globals
metadata.exports
Without metadata.exports, detects writes to the global object.
*/
var __globalName = typeof self != 'undefined' ? 'self' : 'global';
hook('fetch', function(fetch) {
return function(load) {
if (load.metadata.exports && !load.metadata.format)
load.metadata.format = 'global';
return fetch.call(this, load);
};
});
// ideally we could support script loading for globals, but the issue with that is that
// we can't do it with AMD support side-by-side since AMD support means defining the
// global define, and global support means not definining it, yet we don't have any hook
// into the "pre-execution" phase of a script tag being loaded to handle both cases
hook('instantiate', function(instantiate) {
return function(load) {
var loader = this;
if (!load.metadata.format)
load.metadata.format = 'global';
// global is a fallback module format
if (load.metadata.format == 'global' && !load.metadata.registered) {
var entry = createEntry();
load.metadata.entry = entry;
entry.deps = [];
for (var g in load.metadata.globals) {
var gl = load.metadata.globals[g];
if (gl)
entry.deps.push(gl);
}
entry.execute = function(require, exports, module) {
var globals;
if (load.metadata.globals) {
globals = {};
for (var g in load.metadata.globals)
if (load.metadata.globals[g])
globals[g] = require(load.metadata.globals[g]);
}
var exportName = load.metadata.exports;
if (exportName)
load.source += '\n' + __globalName + '["' + exportName + '"] = ' + exportName + ';';
var retrieveGlobal = loader.get('@@global-helpers').prepareGlobal(module.id, exportName, globals);
__exec.call(loader, load);
return retrieveGlobal();
}
}
return instantiate.call(this, load);
};
});