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
16 var evt
= new Event('error');
17 } catch (_eventError
) {
18 var evt
= document
.createEvent('Event');
19 evt
.initEvent('error', true, true);
21 evt
.message
= err
.message
;
23 window
.dispatchEvent(evt
);
25 // throw so it still shows up in the console
30 document
.removeEventListener('DOMContentLoaded', ready
, false );
34 var scripts
= document
.getElementsByTagName('script');
35 for (var i
= 0; i
< scripts
.length
; i
++) {
36 var script
= scripts
[i
];
37 if (script
.type
== 'module' && !script
.loaded
) {
40 loader
.import(script
.src
).catch(handleError
);
42 // anonymous modules supported via a custom naming scheme and registry
44 var uri
= './<anon' + ++anonCnt
+ '>';
45 if (script
.id
!== ""){
46 uri
= "./" + script
.id
;
49 var anonName
= resolveIfNotPlain(uri
, baseURI
);
50 anonSources
[anonName
] = script
.innerHTML
;
51 loader
.import(anonName
).catch(handleError
);
58 if (document
.readyState
=== 'complete')
61 document
.addEventListener('DOMContentLoaded', ready
, false);
64 function BrowserESModuleLoader(baseKey
) {
66 this.baseKey
= resolveIfNotPlain(baseKey
, baseURI
) || resolveIfNotPlain('./' + baseKey
, baseURI
);
68 RegisterLoader
.call(this);
72 // ensure System.register is available
73 global
.System
= global
.System
|| {};
74 if (typeof global
.System
.register
== 'function')
75 var prevRegister
= global
.System
.register
;
76 global
.System
.register = function() {
77 loader
.register
.apply(loader
, arguments
);
79 prevRegister
.apply(this, arguments
);
82 BrowserESModuleLoader
.prototype = Object
.create(RegisterLoader
.prototype);
84 // normalize is never given a relative name like "./x", that part is already handled
85 BrowserESModuleLoader
.prototype[RegisterLoader
.resolve
] = function(key
, parent
) {
86 var resolved
= RegisterLoader
.prototype[RegisterLoader
.resolve
].call(this, key
, parent
|| this.baseKey
) || key
;
88 throw new RangeError('ES module loader does not resolve plain module names, resolving "' + key
+ '" to ' + parent
);
93 function xhrFetch(url
, resolve
, reject
) {
94 var xhr
= new XMLHttpRequest();
95 function load(source
) {
96 resolve(xhr
.responseText
);
99 reject(new Error('XHR error' + (xhr
.status
? ' (' + xhr
.status
+ (xhr
.statusText
? ' ' + xhr
.statusText
: '') + ')' : '') + ' loading ' + url
));
102 xhr
.onreadystatechange = function () {
103 if (xhr
.readyState
=== 4) {
104 // in Chrome on file:/// URLs, status is 0
105 if (xhr
.status
== 0) {
106 if (xhr
.responseText
) {
110 // when responseText is empty, wait for load or error event
111 // to inform if it is a 404 or empty file
112 xhr
.addEventListener('error', error
);
113 xhr
.addEventListener('load', load
);
116 else if (xhr
.status
=== 200) {
124 xhr
.open("GET", url
, true);
128 var WorkerPool = function (script
, size
) {
129 this._workers
= new Array(size
);
133 this.onmessage
= undefined;
134 this._stopTimeout
= undefined;
135 for (let i
= 0; i
< size
; i
++) {
136 let wrkr
= new Worker(script
);
139 wrkr
.onmessage
= this._onmessage
.bind(this, wrkr
);
140 this._workers
[i
] = wrkr
;
145 WorkerPool
.prototype = {
146 postMessage: function (msg
) {
147 if (this._stopTimeout
!== undefined) {
148 clearTimeout(this._stopTimeout
);
149 this._stopTimeout
= undefined;
151 let wrkr
= this._workers
[this._ind
% this._size
];
154 wrkr
.postMessage(msg
);
158 _onmessage: function (wrkr
, evt
) {
161 this.onmessage(evt
, wrkr
);
165 _checkJobs: function () {
166 if (this._jobs
=== 0 && this._stopTimeout
=== undefined) {
167 // wait for 2s of inactivity before stopping (that should be enough for local loading)
168 this._stopTimeout
= setTimeout(this._stop
.bind(this), 2000);
173 this._workers
.forEach(function(wrkr
) {
179 var promiseMap
= new Map();
180 var babelWorker
= new WorkerPool('/vendor/browser-es-module-loader/dist/babel-worker.js', 3);
181 babelWorker
.onmessage = function (evt
) {
182 var promFuncs
= promiseMap
.get(evt
.data
.key
);
183 promFuncs
.resolve(evt
.data
);
184 promiseMap
.delete(evt
.data
.key
);
187 // instantiate just needs to run System.register
188 // so we fetch the source, convert into the Babel System module format, then evaluate it
189 BrowserESModuleLoader
.prototype[RegisterLoader
.instantiate
] = function(key
, processAnonRegister
) {
192 // load as ES with Babel converting into System.register
193 return new Promise(function(resolve
, reject
) {
195 if (anonSources
[key
]) {
196 resolve(anonSources
[key
])
197 anonSources
[key
] = undefined;
199 // otherwise we fetch
201 xhrFetch(key
, resolve
, reject
);
204 .then(function(source
) {
205 // check our cache first
206 const cacheEntryTrans
= localStorage
.getItem(key
+'!transpiled');
207 if (cacheEntryTrans
) {
208 const cacheEntryRaw
= localStorage
.getItem(key
+'!raw');
209 // TODO: store a hash instead
210 if (cacheEntryRaw
=== source
) {
211 return Promise
.resolve({key
: key
, code
: cacheEntryTrans
, source
: source
});
214 return new Promise(function (resolve
, reject
) {
215 promiseMap
.set(key
, {resolve
: resolve
, reject
: reject
});
216 babelWorker
.postMessage({key
: key
, source
: source
});
218 }).then(function (data
) {
219 // evaluate without require, exports and module variables
220 // we leave module in for now to allow module.require access
221 if (data
.key
.slice(-8) !== '#nocache') {
222 localStorage
.setItem(key
+'!raw', data
.source
);
223 localStorage
.setItem(data
.key
+'!transpiled', data
.code
);
225 (0, eval
)(data
.code
+ '\n//# sourceURL=' + data
.key
+ '!transpiled');
226 processAnonRegister();
230 // create a default loader instance in the browser
232 loader
= new BrowserESModuleLoader();
234 export default BrowserESModuleLoader
;