-
-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for import maps in Worker; resolves #9 #17
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { baseUrl as pageBaseUrl, createBlob } from './common.js'; | ||
|
||
export class WorkerShim { | ||
constructor(aURL, options = {type: 'classic'}) { | ||
if (options.type !== 'module') { | ||
return new Worker(aURL, options); | ||
} | ||
|
||
let es_module_shims_src = new URL('es-module-shims.js', pageBaseUrl).href; | ||
const scripts = document.scripts; | ||
for (let i = 0, len = scripts.length; i < len; i++) { | ||
if (scripts[i].src.includes('es-module-shims.js')) { | ||
es_module_shims_src = scripts[i].src; | ||
|
||
break; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than this restriction, could we instead try something like - const esModuleShimsSrc = document.currentScript && document.currentScript.src; Then in the if (!esModuleShimsSrc) throw new Error('es-module-shims.js must be loaded with a script tag for WorkerShim support.'); note the above would have to be outside of the class constructor and instead in the script initialization. |
||
|
||
const workerScriptUrl = createBlob(`importScripts('${es_module_shims_src}'); | ||
self.importMap = ${JSON.stringify(options.importMap || {})}; importShim('${new URL(aURL, pageBaseUrl).href}')`); | ||
|
||
const workerOptions = {...options}; | ||
workerOptions.type = 'classic'; | ||
|
||
return new Worker(workerScriptUrl, workerOptions); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
self.postMessage('classic'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import test from "test"; | ||
|
||
self.postMessage(test); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import test from "test"; | ||
console.log(test); | ||
</script> | ||
<script type="module" src="../src/es-module-shims.js"></script> | ||
<script type="module" src="../dist/es-module-shims.js"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this necessary for the tests to pass? I must admit I quite like being able to run the tests without a rebuild. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Short answer: yes, this is necessary for tests to pass.
As I said before I made this change only to see these simple To overcome this situation I see 2 quick solutions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see what you mean, thanks. Let's go with (2) then so that this test restriction only applies to the worker tests. |
||
<script type="module"> | ||
mocha.setup('tdd'); | ||
mocha.allowUncaught(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I submitted by mistake this file, namely
dist/es-module-shims.js
. I shouldn't have done that. I apologize.