1 import RegisterLoader
from 'es-module-loader/core/register-loader.js';
2 import { InternalModuleNamespace as ModuleNamespace
} from 'es-module-loader/core/loader-polyfill.js';
4 import { baseURI
, global
, isBrowser
} from 'es-module-loader/core/common.js';
5 import { resolveIfNotPlain
} from 'es-module-loader/core/resolve.js';
9 // <script type="module"> support
11 if (typeof document
!= 'undefined' && document
.getElementsByTagName
) {
12 function handleError(err
) {
13 // dispatch an error event so that we can display in errors in browsers
14 // that don't yet support unhandledrejection
15 if (window
.onunhandledrejection
=== undefined) {
17 var evt
= new Event('error');
18 } catch (_eventError
) {
19 var evt
= document
.createEvent('Event');
20 evt
.initEvent('error', true, true);
22 evt
.message
= err
.message
;
24 window
.dispatchEvent(evt
);
27 // throw so it still shows up in the console
32 document
.removeEventListener('DOMContentLoaded', ready
, false );
36 var scripts
= document
.getElementsByTagName('script');
37 for (var i
= 0; i
< scripts
.length
; i
++) {
38 var script
= scripts
[i
];
39 if (script
.type
== 'module' && !script
.loaded
) {
42 loader
.import(script
.src
).catch(handleError
);
44 // anonymous modules supported via a custom naming scheme and registry
46 var uri
= './<anon' + ++anonCnt
+ '>';
47 if (script
.id
!== ""){
48 uri
= "./" + script
.id
;
51 var anonName
= resolveIfNotPlain(uri
, baseURI
);
52 anonSources
[anonName
] = script
.innerHTML
;
53 loader
.import(anonName
).catch(handleError
);
60 if (document
.readyState
=== 'complete')
63 document
.addEventListener('DOMContentLoaded', ready
, false);
66 function BrowserESModuleLoader(baseKey
) {
68 this.baseKey
= resolveIfNotPlain(baseKey
, baseURI
) || resolveIfNotPlain('./' + baseKey
, baseURI
);
70 RegisterLoader
.call(this);
74 // ensure System.register is available
75 global
.System
= global
.System
|| {};
76 if (typeof global
.System
.register
== 'function')
77 var prevRegister
= global
.System
.register
;
78 global
.System
.register = function() {
79 loader
.register
.apply(loader
, arguments
);
81 prevRegister
.apply(this, arguments
);
84 BrowserESModuleLoader
.prototype = Object
.create(RegisterLoader
.prototype);
86 // normalize is never given a relative name like "./x", that part is already handled
87 BrowserESModuleLoader
.prototype[RegisterLoader
.resolve
] = function(key
, parent
) {
88 var resolved
= RegisterLoader
.prototype[RegisterLoader
.resolve
].call(this, key
, parent
|| this.baseKey
) || key
;
90 throw new RangeError('ES module loader does not resolve plain module names, resolving "' + key
+ '" to ' + parent
);
95 function xhrFetch(url
, resolve
, reject
) {
96 var xhr
= new XMLHttpRequest();
97 function load(source
) {
98 resolve(xhr
.responseText
);
101 reject(new Error('XHR error' + (xhr
.status
? ' (' + xhr
.status
+ (xhr
.statusText
? ' ' + xhr
.statusText
: '') + ')' : '') + ' loading ' + url
));
104 xhr
.onreadystatechange = function () {
105 if (xhr
.readyState
=== 4) {
106 // in Chrome on file:/// URLs, status is 0
107 if (xhr
.status
== 0) {
108 if (xhr
.responseText
) {
112 // when responseText is empty, wait for load or error event
113 // to inform if it is a 404 or empty file
114 xhr
.addEventListener('error', error
);
115 xhr
.addEventListener('load', load
);
118 else if (xhr
.status
=== 200) {
126 xhr
.open("GET", url
, true);
130 var WorkerPool = function (script
, size
) {
131 this._workers
= new Array(size
);
135 this.onmessage
= undefined;
136 this._stopTimeout
= undefined;
137 for (let i
= 0; i
< size
; i
++) {
138 let wrkr
= new Worker(script
);
141 wrkr
.onmessage
= this._onmessage
.bind(this, wrkr
);
142 this._workers
[i
] = wrkr
;
147 WorkerPool
.prototype = {
148 postMessage: function (msg
) {
149 if (this._stopTimeout
!== undefined) {
150 clearTimeout(this._stopTimeout
);
151 this._stopTimeout
= undefined;
153 let wrkr
= this._workers
[this._ind
% this._size
];
156 wrkr
.postMessage(msg
);
160 _onmessage: function (wrkr
, evt
) {
163 this.onmessage(evt
, wrkr
);
167 _checkJobs: function () {
168 if (this._jobs
=== 0 && this._stopTimeout
=== undefined) {
169 // wait for 2s of inactivity before stopping (that should be enough for local loading)
170 this._stopTimeout
= setTimeout(this._stop
.bind(this), 2000);
175 this._workers
.forEach(function(wrkr
) {
181 var promiseMap
= new Map();
182 var babelWorker
= new WorkerPool('/vendor/browser-es-module-loader/dist/babel-worker.js', 3);
183 babelWorker
.onmessage = function (evt
) {
184 var promFuncs
= promiseMap
.get(evt
.data
.key
);
185 promFuncs
.resolve(evt
.data
);
186 promiseMap
.delete(evt
.data
.key
);
189 // instantiate just needs to run System.register
190 // so we fetch the source, convert into the Babel System module format, then evaluate it
191 BrowserESModuleLoader
.prototype[RegisterLoader
.instantiate
] = function(key
, processAnonRegister
) {
194 // load as ES with Babel converting into System.register
195 return new Promise(function(resolve
, reject
) {
197 if (anonSources
[key
]) {
198 resolve(anonSources
[key
])
199 anonSources
[key
] = undefined;
201 // otherwise we fetch
203 xhrFetch(key
, resolve
, reject
);
206 .then(function(source
) {
207 // check our cache first
208 const cacheEntryTrans
= localStorage
.getItem(key
+'!transpiled');
209 if (cacheEntryTrans
) {
210 const cacheEntryRaw
= localStorage
.getItem(key
+'!raw');
211 // TODO: store a hash instead
212 if (cacheEntryRaw
=== source
) {
213 return Promise
.resolve({key
: key
, code
: cacheEntryTrans
, source
: source
});
216 return new Promise(function (resolve
, reject
) {
217 promiseMap
.set(key
, {resolve
: resolve
, reject
: reject
});
218 babelWorker
.postMessage({key
: key
, source
: source
});
220 }).then(function (data
) {
221 // evaluate without require, exports and module variables
222 // we leave module in for now to allow module.require access
223 if (data
.key
.slice(-8) !== '#nocache') {
224 localStorage
.setItem(key
+'!raw', data
.source
);
225 localStorage
.setItem(data
.key
+'!transpiled', data
.code
);
227 (0, eval
)(data
.code
+ '\n//# sourceURL=' + data
.key
+ '!transpiled');
228 processAnonRegister();
232 // create a default loader instance in the browser
234 loader
= new BrowserESModuleLoader();
236 export default BrowserESModuleLoader
;