71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
|
/*
|
||
|
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.entry) {
|
||
|
|
||
|
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, !!load.metadata.encapsulateGlobal);
|
||
|
__exec.call(loader, load);
|
||
|
|
||
|
return retrieveGlobal();
|
||
|
}
|
||
|
}
|
||
|
return instantiate.call(this, load);
|
||
|
};
|
||
|
});
|