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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import url worker two times #7468

Merged
merged 9 commits into from Mar 29, 2022

Conversation

poyoho
Copy link
Member

@poyoho poyoho commented Mar 26, 2022

Description

avoid emit same file

case one:

const worker = new Worker(new URL('./worker.js', import.meta.url), {type: 'classic'})
const worker2 = new Worker(new URL('./worker.js', import.meta.url))

match two times worker constructor avoids building two times. there will always use a first-time result. I think it will be ok because the worker plugin no deps any more info from the worker constructor.

this case it always emit rollup assets.

case two:

const moduleWorker = new Worker(
  new URL('./module-and-worker.js', import.meta.url),
  { type: "module" }
)

moduleWorker.onmessage = (event) => {
  self.postMessage({
    type: 'module-and-worker:worker',
    data: event.data
  })
}

import("./module-and-worker").then(res => {
  self.postMessage({
    type: 'module-and-worker:module',
    data: res.module
  })
})

In this case, it may be emitting assets or chunks.

if the file is imported by the module package it will have the export expression and if the file is imported by the worker package it will have not the export expression so don't have the same file.

overwrites a previously emitted file of the same name.

if the above script is used in another worker, It also reports a warning. 馃槄

so I cache the same id, if emitted and don't emit next time.

avoid bundle same file

worker bundle doesn't deps on any more worker runtime info, so one id only had one result. Save worker bundled file id to avoid repeated execution of bundles. And if the worker had bundled, rewritten truth id, no need to replace by asset plugin.(because nested worker bundle context don't had file what emitted by outside bundle)

import.meta.url in worker

As the unit test of the worker was originally improved, it was found that replacing the fix import.meta.url inside the define plugin would cause the nested worker to not be recognized. Now try to replace it in the chunkRender hook. 馃う

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@poyoho poyoho marked this pull request as draft March 26, 2022 10:17
@poyoho poyoho marked this pull request as ready for review March 26, 2022 13:52
@bluwy bluwy added feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority) labels Mar 27, 2022
@patak-dev patak-dev merged commit f05a813 into vitejs:main Mar 29, 2022
@poyoho poyoho deleted the fix/build-two-times-worker branch March 29, 2022 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants