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

Commit ff429e2

Browse files
committed
Adds support for web workers
This adds initial support for Web Workers; at least to add basic modules. Not as much work as you would think, most places where it checks if we are in a browser will work in a Worker as well. Fixes #201
1 parent b583464 commit ff429e2

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

lib/loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,8 @@ function logloads(loads) {
10481048
var traceur;
10491049
Loader.prototype.parse = function(load) {
10501050
if (!traceur) {
1051-
if (typeof window == 'undefined')
1051+
if (typeof window == 'undefined' &&
1052+
typeof WorkerGlobalScope == 'undefined')
10521053
traceur = require('traceur');
10531054
else if (__global.traceur)
10541055
traceur = __global.traceur;

lib/system.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
(function (global) {
1414

15-
var isBrowser = typeof window != 'undefined';
15+
var isWorker = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
16+
var isBrowser = typeof window != 'undefined' && !isWorker;
1617
var Loader = global.Reflect && global.Reflect.Loader || require('./loader');
1718
var Promise = global.Promise || require('when/es6-shim/Promise');
1819

@@ -59,7 +60,7 @@
5960
}
6061

6162
var fetchTextFromURL;
62-
if (isBrowser) {
63+
if (isBrowser || isWorker) {
6364
fetchTextFromURL = function(url, fulfill, reject) {
6465
var xhr = new XMLHttpRequest();
6566
var sameDomain = true;
@@ -112,7 +113,7 @@
112113
}
113114

114115
var System = new Loader({
115-
global: isBrowser ? window : global,
116+
global: isBrowser ? window : (isWorker ? self : global),
116117
strict: true,
117118
normalize: function(name, parentName, parentAddress) {
118119
if (typeof name != 'string')
@@ -204,7 +205,7 @@
204205
// according to https://github.com/jorendorff/js-loaders/blob/master/browser-loader.js#L238
205206
// we should encode everything, but it breaks for servers that don't expect it
206207
// like in (https://github.com/systemjs/systemjs/issues/168)
207-
if (isBrowser)
208+
if (isBrowser || isWorker)
208209
outPath = outPath.replace(/#/g, '%40');
209210

210211
return toAbsoluteURL(this.baseURL, outPath);
@@ -219,8 +220,8 @@
219220
},
220221
});
221222

222-
if (isBrowser) {
223-
var href = window.location.href.split('#')[0].split('?')[0];
223+
if (isBrowser || isWorker) {
224+
var href = global.location.href.split('#')[0].split('?')[0];
224225
System.baseURL = href.substring(0, href.lastIndexOf('/') + 1);
225226
}
226227
else {

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,12 @@ function runTests() {
566566
test('System instanceof Loader', function(assert) {
567567
assert(System instanceof Reflect.Loader, true);
568568
});
569+
570+
test('Loading inside of a Web Worker', function(assert) {
571+
var worker = new Worker('worker/worker.js');
572+
573+
worker.onmessage = function(e) {
574+
assert(e.data, 'p');
575+
};
576+
});
569577
}

test/worker/es6.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var p = 'p';

test/worker/worker.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
importScripts("../../node_modules/traceur/bin/traceur.js",
2+
"../../node_modules/when/es6-shim/Promise.js",
3+
"../../lib/loader.js",
4+
"../../lib/system.js");
5+
6+
System['import']('es6').then(function(m) {
7+
postMessage(m.p);
8+
}, function(err) {
9+
console.error(err, err.stack);
10+
});

0 commit comments

Comments
 (0)