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

docs: Clarified web worker type: 'module' is compiled away #9742

Closed
wants to merge 1 commit into from

Conversation

Pyrolistical
Copy link

Description

Documentation states type: 'module' is supported, but unclear it is correctly compiled away in production. This PR clarifies that.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@@ -518,7 +518,7 @@ import MyWorker from './worker?worker'
const worker = new MyWorker()
```

The worker script can also use `import` statements instead of `importScripts()` - note during dev this relies on browser native support and currently only works in Chrome, but for the production build it is compiled away.
The worker script can also use `import` statements (with `type: 'module'` option) instead of `importScripts()` - note during dev this relies on browser native support and currently only works in Chrome, but for the production build it is compiled away.
Copy link
Member

Choose a reason for hiding this comment

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

This addition makes it sounds like you can only use import when using type: 'module' and that isn't the case. Could you expand on what you find confusing here?
Maybe it could be:

Suggested change
The worker script can also use `import` statements (with `type: 'module'` option) instead of `importScripts()` - note during dev this relies on browser native support and currently only works in Chrome, but for the production build it is compiled away.
The worker script can also use `import` statements (both for classic and `type: 'module'` workers) instead of `importScripts()` - note during dev this relies on browser native support and currently only works in Chrome, but for the production build it is compiled away.

Copy link
Author

Choose a reason for hiding this comment

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

Hmm but now it sounds like you could use import statement in classic type

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you can use it in classic types. It will be bundled and compiled away

Copy link
Author

Choose a reason for hiding this comment

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

Oh I didn't know that. What is the point of the module type then?

Copy link
Member

Choose a reason for hiding this comment

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

It works when ?worker is used.
But it does not work in dev when new Worker(new URL('./foo.js', import.meta.url)) is used (#8470).

Copy link
Member

Choose a reason for hiding this comment

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

And @Pyrolistical, the module type controls how it is bundled in production (but should still be able to use ES modules to do your workers in both, except for the bug that @sapphi-red just linked)

@Pyrolistical Pyrolistical deleted the patch-1 branch August 27, 2022 02:31
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.

3 participants