Skip to content
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

Merged
merged 4 commits into from Jun 24, 2019
Merged

Add support for import maps in Worker; resolves #9 #17

merged 4 commits into from Jun 24, 2019

Conversation

costingeana
Copy link
Collaborator

Add support for import maps in Worker. Resolves #9.

  1. add WorkerShim class;
  2. 'expose' importMap on global object (self) in web workers and browser.

1. add WorkerShim class
2. 'expose' importMap on global object (self) in web workers and browser.
@guybedford
Copy link
Owner

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 new Worker detection in the lexer and handle the rewriting automatically without needing to use WorkerShim, but I'd be happy to include that as a possible future follow-on to this PR.

@costingeana would you be able to include a simple test before we land this?

@costingeana
Copy link
Collaborator Author

Thank you @guybedford, but I only followed your initial idea from #9 (comment).
I just needed import maps in Workers in a project I'm working on and I used your WorkerShim proof of concept, which worked beautifully.

What's next?
Firstly I would add some tests for the current implementation.
Secondly I will implement and test your suggestion: new Worker detection in lexer.

Thank you for the feedback on this pull request; it helps a lot.

@guybedford
Copy link
Owner

@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 new keyword followed by the Worker identifier would only ever be valid in the right place (eg function (new Worker) isn't supported etc). So again we can hack around the limitations of the lexer in a well defined way hopefully here!

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
@costingeana
Copy link
Collaborator Author

My apologies for the delay!
@guybedford, please let me know if there is something else I can add (or remove).

@costingeana
Copy link
Collaborator Author

@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>
Copy link
Owner

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.

Copy link
Collaborator Author

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:

  1. Revert the src path to src="../src/es-module-shims.js" and add a comment to inform everybody that Worker's tests need a different path for es-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).
  2. Add a separate html file (e.g. test/test-worker.html) which loads only the Worker tests; in this html the src to es-module-shims script will point to ../dist/es-module-shims.js".

Copy link
Owner

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.

Copy link
Owner

@guybedford guybedford left a 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.

Copy link
Owner

@guybedford guybedford left a 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.

}

const workerScriptUrl = createBlob(`importScripts('${es_module_shims_src}');
self.importMap = ${JSON.stringify(options.importMap || {})}; importShim('${new URL(aURL, baseUrl).href}')`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.

Copy link
Collaborator Author

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>
Copy link
Owner

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.


break;
}
}
Copy link
Owner

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.
@guybedford guybedford merged commit 998d8dd into guybedford:master Jun 24, 2019
@guybedford
Copy link
Owner

Amazing work, thank you!

@costingeana
Copy link
Collaborator Author

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.

@guybedford guybedford mentioned this pull request Oct 6, 2021
@guybedford guybedford mentioned this pull request Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose import map shim on JS API
2 participants