-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
Conversation
1. add WorkerShim class 2. 'expose' importMap on global object (self) in web workers and browser.
Amazing work and thank you for the PR! I really like how simple this turned out to be. A nice addition might be to add support for @costingeana would you be able to include a simple test before we land this? |
Thank you @guybedford, but I only followed your initial idea from #9 (comment). What's next? Thank you for the feedback on this pull request; it helps a lot. |
@costingeana happy to merge here with just a test, then let's work on the lexer addition in a new PR. The lexer doesn't have any concept of identifiers and referenced identifiers vs other types of identifiers, but we can probably just work with the fact that the |
1. add worker-shim.js - this module 'hosts' WorkerShim class; 2. update common.js - move here and then export it the function 'createBlob' from es-modules-shims.js; 3. update es-modules-shims.js: 3.1. import WorkerShim from worker-shim.js; 3.2. expose WorkerShim on global object (i.e. self); 4. update test/ 4.1. add new test fixtures under test/fixtures/worker/ 4.2. update browser-modules.js - add 2 simple tests for WorkerShim 4.3. update test/test.html - update the 'src' of es-module-shims.js script to '../dist/es-module-shims.js' because otherwise importScripts('../src/es-module-shims.js') used in WorkerShim will fail; ../src/es-module-shims.js is a es6 module not a 'classic' script
My apologies for the delay! |
@guybedford, is there anything else I can do to make this pull request ready for merging? |
test/test.html
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer: yes, this is necessary for tests to pass.
Long answer: The importScript()
method used in WorkerShim
constructor fails if the script it loads has ES6 syntax; as far as I know the importScripts()
method will automatically fail inside module workers.
const workerScriptUrl = createBlob(\`importScripts('${es_module_shims_src}');
self.importMap = ${JSON.stringify(options.importMap || {})}; importShim('${new URL(aURL, pageBaseUrl).href}')`);
As I said before I made this change only to see these simple WorkerShim
tests pass. However, I am of the same opinion as you when you say that you like being able to run the tests without rebuild.
To overcome this situation I see 2 quick solutions:
- Revert the
src
path tosrc="../src/es-module-shims.js"
and add a comment to inform everybody that Worker's tests need a different path fores-module-shims
script; after all there are only 2 simple tests so it doesn't make sense to alter all the other tests which are much more important (IMHO). - Add a separate
html
file (e.g.test/test-worker.html
) which loads only the Worker tests; in thishtml
thesrc
toes-module-shims
script will point to../dist/es-module-shims.js"
.
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.
Ah I see what you mean, thanks.
Let's go with (2) then so that this test restriction only applies to the worker tests.
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.
LGTM, just let me know about that comment then I think we're good to merge.
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've made a suggestion re the pathing - let me know if that makes sense.
dist/es-module-shims.js
Outdated
} | ||
|
||
const workerScriptUrl = createBlob(`importScripts('${es_module_shims_src}'); | ||
self.importMap = ${JSON.stringify(options.importMap || {})}; importShim('${new URL(aURL, baseUrl).href}')`); |
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.
test/test.html
Outdated
@@ -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 comment
The 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.
src/worker-shims.js
Outdated
|
||
break; | ||
} | ||
} |
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.
Rather than this restriction, could we instead try something like -
const esModuleShimsSrc = document.currentScript && document.currentScript.src;
Then in the WorkerShim
constructor, something like -
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.
2. Use the approach suggested by @guybedford for detecting the `es-module-shims.js` path needed in WorkerShim constructor.
Amazing work, thank you! |
You’re welcome! And thank you, too. By the end of this week I will come up with another pull request to add support for new Worker detection in the lexer as you suggested earlier. |
Add support for import maps in Worker. Resolves #9.