Skip to content

Commit

Permalink
Allows SystemJS to load within Web Workers
Browse files Browse the repository at this point in the history
This adds support for web workers building on the work done in ES6
Module Loader. Mostly just window checks, did have to do a plain old
eval in this case. Fixes #165
  • Loading branch information
matthewp committed Aug 16, 2014
1 parent 176e752 commit 11d5144
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.8.0",
"main": "dist/system.js",
"dependencies": {
"es6-module-loader": "~0.8.1"
"es6-module-loader": "~0.8.2"
},
"devDependencies": {
"qunit": "~1.12.0"
Expand Down
46 changes: 38 additions & 8 deletions dist/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ $__global.upgradeSystemLoader = function() {
$__global.System = System.originalSystem;
}

/*

/*
* Meta Extension
*
* Sets default metadata on a load record (load.metadata) from
Expand Down Expand Up @@ -788,9 +789,14 @@ function core(loader) {

// override locate to allow baseURL to be document-relative
var baseURI;
if (typeof window == 'undefined') {
if (typeof window == 'undefined' &&
typeof WorkerGlobalScope == 'undefined') {
baseURI = process.cwd() + '/';
}
// Inside of a Web Worker
else if(typeof window == 'undefined') {
baseURI = loader.global.location.href;
}
else {
baseURI = document.baseURI;
if (!baseURI) {
Expand Down Expand Up @@ -1076,14 +1082,13 @@ function cjs(loader) {
as well as a RequireJS-style require on System.require
*/
function amd(loader) {

// by default we only enforce AMD noConflict mode in Node
var isNode = typeof module != 'undefined' && module.exports;

// AMD Module Format Detection RegEx
// define([.., .., ..], ...)
// define(varName); || define(function(require, exports) {}); || define({})
var amdRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*(("[^"]+"|'[^']+')\s*,|\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*(\s*("[^"]+"|'[^']+')\s*,?\s*)?(\s*(\/\/.*\r?\n|\/\*(.|\s)*?\*\/)\s*)*\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/;
var amdRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*(("[^"]+"|'[^']+')\s*,|\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*(\s*("[^"]+"|'[^']+')\s*,?)?(\s*(\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*\s*\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/;
var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;

var cjsRequirePre = "(?:^\\s*|[}{\\(\\);,\\n=:\\?\\&]\\s*)";
Expand Down Expand Up @@ -2041,7 +2046,11 @@ var $__curScript, __eval;
}
};

if (typeof window != 'undefined') {
var isWorker = typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope;
var isBrowser = typeof window != 'undefined';

if (isBrowser) {
var head;

var scripts = document.getElementsByTagName('script');
Expand Down Expand Up @@ -2078,6 +2087,29 @@ var $__curScript, __eval;
$__global.upgradeSystemLoader();
}
}
else if(isWorker) {
doEval = function(source) {
try {
eval(source);
} catch(e) {
throw e;
}
};

if (!$__global.System || !$__global.LoaderPolyfill) {
var basePath = '';
try {
throw new Error('Getting the path');
} catch(err) {
var idx = err.stack.indexOf('at ') + 3;
var withSystem = err.stack.substr(idx, err.stack.substr(idx).indexOf('\n'));
basePath = withSystem.substr(0, withSystem.lastIndexOf('/') + 1);
}
importScripts(basePath + 'es6-module-loader.js');
} else {
$__global.upgradeSystemLoader();
}
}
else {
var es6ModuleLoader = require('es6-module-loader');
$__global.System = es6ModuleLoader.System;
Expand All @@ -2093,6 +2125,4 @@ var $__curScript, __eval;
}
})();

})(typeof window != 'undefined' ? window : global);


})(this);
1 change: 0 additions & 1 deletion lib/extension-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
as well as a RequireJS-style require on System.require
*/
function amd(loader) {

// by default we only enforce AMD noConflict mode in Node
var isNode = typeof module != 'undefined' && module.exports;

Expand Down
7 changes: 6 additions & 1 deletion lib/extension-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ function core(loader) {

// override locate to allow baseURL to be document-relative
var baseURI;
if (typeof window == 'undefined') {
if (typeof window == 'undefined' &&
typeof WorkerGlobalScope == 'undefined') {
baseURI = process.cwd() + '/';
}
// Inside of a Web Worker
else if(typeof window == 'undefined') {
baseURI = loader.global.location.href;
}
else {
baseURI = document.baseURI;
if (!baseURI) {
Expand Down
33 changes: 29 additions & 4 deletions lib/polyfill-wrapper-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ var $__curScript, __eval;
}
};

if (typeof window != 'undefined') {
var isWorker = typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope;
var isBrowser = typeof window != 'undefined';

if (isBrowser) {
var head;

var scripts = document.getElementsByTagName('script');
Expand Down Expand Up @@ -60,6 +64,29 @@ var $__curScript, __eval;
$__global.upgradeSystemLoader();
}
}
else if(isWorker) {
doEval = function(source) {
try {
eval(source);
} catch(e) {
throw e;
}
};

if (!$__global.System || !$__global.LoaderPolyfill) {
var basePath = '';
try {
throw new Error('Getting the path');
} catch(err) {
var idx = err.stack.indexOf('at ') + 3;
var withSystem = err.stack.substr(idx, err.stack.substr(idx).indexOf('\n'));
basePath = withSystem.substr(0, withSystem.lastIndexOf('/') + 1);
}
importScripts(basePath + 'es6-module-loader.js');
} else {
$__global.upgradeSystemLoader();
}
}
else {
var es6ModuleLoader = require('es6-module-loader');
$__global.System = es6ModuleLoader.System;
Expand All @@ -75,6 +102,4 @@ var $__curScript, __eval;
}
})();

})(typeof window != 'undefined' ? window : global);


})(this);
2 changes: 1 addition & 1 deletion lib/polyfill-wrapper-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ $__global.upgradeSystemLoader = function() {
$__global.System = System.originalSystem;
}



9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,13 @@ asyncTest('AMD -> System.register circular -> ES6', function() {
}, err);
});

asyncTest('Using SystemJS in a Web Worker', function() {
var worker = new Worker('tests/worker.js');
worker.onmessage = function(e) {
ok(e.data.amd === 'AMD Module');
ok(e.data.es6 === 'ES6 Module');
start();
};
});

})();
14 changes: 14 additions & 0 deletions test/tests/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
importScripts('../../bower_components/traceur/traceur.js',
'../../bower_components/es6-module-loader/dist/es6-module-loader.js',
'../../dist/system.js');

System.baseURL = '../';

System.import('tests/es6-and-amd').then(function(m) {
postMessage({
amd: m.amd_module,
es6: m.es6_module
});
}, function(err) {
console.error(err);
});

0 comments on commit 11d5144

Please sign in to comment.