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

feat(worker): allow passing options to worker constructors #6145

Closed
wants to merge 12 commits into from

Conversation

branchseer
Copy link

@branchseer branchseer commented Dec 16, 2021

Description

This PR adds support for the options parameter of worker constructors, notably allowing { type: "classic" } (fixes #2550).

Additional context

There is #2578, but it's closed at #2578 (comment). This PR workarounds that problem by prepending importScript(...) instead of import ... to classic worker scripts.

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.

@Niputi Niputi added feat: web workers p2-nice-to-have Not breaking anything but nice to have (priority) labels Dec 16, 2021
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
try {
return objURL ? new Worker(objURL) : new Worker("data:application/javascript;base64," + encodedJs, {type: "module"});
Copy link
Author

Choose a reason for hiding this comment

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

ping @andreasg123

I don't quite understand why we didn't pass {type: "module"} to new Worker(objURL) (#4674). An oversight maybe?

@techird
Copy link

techird commented Dec 22, 2021

I am using monaco editor via vitejs. The ts_worker is loaded as module worker, importScript fails when i use customWorkerPath.

image

This PR helps. Any chance to merge this?

@zhouhuiquan
Copy link

Dingtalk's chromium is 72, Worker module type surport 80, It Is Necessary!

Copy link

@artemjackson artemjackson left a comment

Choose a reason for hiding this comment

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

lgtm

@sapphi-red
Copy link
Member

sapphi-red commented Apr 28, 2022

Closing as the support has landed with different syntax. #2550 (comment)
https://vitejs.dev/guide/features.html#import-with-constructors

@sapphi-red sapphi-red closed this Apr 28, 2022
@0x30
Copy link

0x30 commented Jun 10, 2022

#2550(comment)

@sapphi-red

It can be used in the development environment, but when 'NPM run build', my new URL is a typescript file?

new URL("./worker.ts", import.meta.url)
// after npm run build
const ng=new URL("./worker.ts",self.location);Yr=new Worker(ng,{type:"classic"});

Does this mean that I have to compile my web worker file in advance in this way? Or is there something wrong with the way I use it?

I still think this method can better solve the problem

import MyClassicWorker from './ classic-worker? worker'

const worker = new MyClassicWorker({ type: 'classic' })

@sapphi-red
Copy link
Member

@0x30 I feel your usage is not correct. Please create an Q&A discussion with reproduction.

@0x30
Copy link

0x30 commented Jun 10, 2022

Yes, you are right. You can see discussions#8525.

  1. But I don't think the document tells us that new workers (New URLs) are not allowed to be separated.Based on previous experience, I think it is OK to transfer URL as a variable to the worker. Obviously, there may be some problems. Maybe the document can focus on this.
  2. Yes, you can also see from the discussion that I did these operations when solving the problem of [dev mode classic worker],Although the problem of worker classic under dev has been modified pull/7099, you can see myexample. This problem still exists in vite 2.9.11. Or what? Did I use the wrong way again?

image

@sapphi-red
Copy link
Member

@0x30

  1. Please create an issue or a PR about improving document.
  2. I think it is When using import inside classic worker, syntax error happens during dev but build success #8470. You cannot use import in classic worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow importing classic Web Workers (not Module Workers)
8 participants