From 61d59b94d462f6f18f8bfa9214c0f0daebfa26ea Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 9 Mar 2017 10:31:17 +0000 Subject: [PATCH 1/3] Add support for web workers --- src/loader.ts | 16 ++++++++++++++++ tests/functional/all.ts | 1 + tests/functional/webworkerAmd.ts | 15 +++++++++++++++ tests/functional/webworkerBasic.html | 21 +++++++++++++++++++++ tests/functional/worker.ts | 28 ++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 tests/functional/webworkerAmd.ts create mode 100644 tests/functional/webworkerBasic.html create mode 100644 tests/functional/worker.ts diff --git a/src/loader.ts b/src/loader.ts index a8cb84c..9c7f042 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -186,6 +186,7 @@ declare const Packages: {} | undefined; has.add('host-browser', typeof document !== 'undefined' && typeof location !== 'undefined'); has.add('host-node', typeof process === 'object' && process.versions && process.versions.node); has.add('host-nashorn', typeof load === 'function' && typeof Packages !== 'undefined'); + has.add('host-web-worker', !has('host-browser') && typeof importScripts !== 'undefined'); has.add('debug', true); has.add('loader-configurable', true); @@ -1064,6 +1065,21 @@ declare const Packages: {} | undefined; setGlobals = globalObjectGlobals; } + else if (has('host-web-worker')) { + injectUrl = function (url: string, callback: (node?: HTMLScriptElement) => void, module: DojoLoader.Module, + parent?: DojoLoader.Module): void { + + try { + importScripts(url); + } + catch (e) { + reportModuleLoadError(parent, module, url); + } + callback(); + }; + + setGlobals = globalObjectGlobals; + } else { throw new Error('Unsupported platform'); } diff --git a/tests/functional/all.ts b/tests/functional/all.ts index 08ceda6..4c6dff3 100644 --- a/tests/functional/all.ts +++ b/tests/functional/all.ts @@ -4,4 +4,5 @@ import './crossOrigin'; import './csp'; import './scriptConfigReading'; import './shimAmdLoading'; +import './webworkerAmd'; import './require/require'; diff --git a/tests/functional/webworkerAmd.ts b/tests/functional/webworkerAmd.ts new file mode 100644 index 0000000..8850940 --- /dev/null +++ b/tests/functional/webworkerAmd.ts @@ -0,0 +1,15 @@ +import * as assert from 'intern/chai!assert'; +import * as registerSuite from 'intern!object'; +import executeTest from './executeTest'; + +const AMD_APP_MESSAGE = 'Message from AMD app.'; + +registerSuite({ + name: 'AMD loading in web worker', + + 'basic loading'(this: any) { + return executeTest(this, './basicAmdLoading.html', function (results: any) { + assert.strictEqual(results.message, AMD_APP_MESSAGE); + }); + } +}); diff --git a/tests/functional/webworkerBasic.html b/tests/functional/webworkerBasic.html new file mode 100644 index 0000000..4e5f1d2 --- /dev/null +++ b/tests/functional/webworkerBasic.html @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/tests/functional/worker.ts b/tests/functional/worker.ts new file mode 100644 index 0000000..e5acd78 --- /dev/null +++ b/tests/functional/worker.ts @@ -0,0 +1,28 @@ +onmessage = function (e) { + try { + /* load the loader */ + importScripts('../../src/loader.js'); + + require.config({ + packages: [ + { name: 'amdApp', location: './amdApp' } + ] + }); + + require([ + 'amdApp/app' + ], function (app) { + /* post message back with the results */ + ( postMessage)({ + message: app.getMessage() + }); + }); + } + catch (e) { + ( postMessage)({ + message: e.message, + status: 'fail' + }); + throw e; + } +}; From c1120073780ce593926baa7922b42a252c848197 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 9 Mar 2017 11:13:45 +0000 Subject: [PATCH 2/3] Force NodeJS 6 types --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 5537199..5a8b70b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@types/chai": "~3.4.0", "@types/glob": "~5.0.0", "@types/grunt": "~0.4.0", + "@types/node": "~6.0.0", "dts-generator": "~2.0.0", "grunt": "^1.0.1", "grunt-contrib-uglify": "^2.0.0", From c9b1a9f4d5da98eee025146d72bdc660d4165bad Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 9 Mar 2017 14:14:32 +0000 Subject: [PATCH 3/3] Fix issue with functional tests --- tests/functional/webworkerAmd.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/webworkerAmd.ts b/tests/functional/webworkerAmd.ts index 8850940..54dca71 100644 --- a/tests/functional/webworkerAmd.ts +++ b/tests/functional/webworkerAmd.ts @@ -8,7 +8,7 @@ registerSuite({ name: 'AMD loading in web worker', 'basic loading'(this: any) { - return executeTest(this, './basicAmdLoading.html', function (results: any) { + return executeTest(this, './webworkerBasic.html', function (results: any) { assert.strictEqual(results.message, AMD_APP_MESSAGE); }); }