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

fix(worker): import.meta.* #7706

Merged
merged 18 commits into from Apr 26, 2022
Merged

Conversation

poyoho
Copy link
Member

@poyoho poyoho commented Apr 13, 2022

Description

fix: #7699

Additional context

  1. import.meta.glob will transform to dynamic import, so only support format: es and module worker.
  2. import.meta.globEager will transform to static import, so only support format: iife|es and module worker.
  3. supplement unit-test for worker constructor in nested worker and simple worker.
  4. clean worker sourcemap warning.
  5. inline worker sourcemap 404

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.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Also The emitted file "assets/url-worker.f4391e1e.js.map" overwrites a previously emitted file of the same name. warning happens in my environment.

packages/playground/worker/__tests__/es/es-worker.spec.ts Outdated Show resolved Hide resolved
packages/vite/src/node/plugins/worker.ts Outdated Show resolved Hide resolved
@poyoho
Copy link
Member Author

poyoho commented Apr 13, 2022

Also The emitted file "assets/url-worker.f4391e1e.js.map" overwrites a previously emitted file of the same name. warning happens in my environment.

resolved. 😊

@poyoho
Copy link
Member Author

poyoho commented Apr 13, 2022

sourcemap had a micro error. wait for a mins.

@poyoho
Copy link
Member Author

poyoho commented Apr 13, 2022

done!

@poyoho
Copy link
Member Author

poyoho commented Apr 14, 2022

import.meta.url build with es it will replace with esbuild minify will got an unexpected data.

This can be solved only by upgrading the target of esbuild

import.meta.url build with iife it will polyfill with rollup will be used document in the worker

Rollup should set format: "es" to avoid it. But I think format: "iife" can still be replaced.

@poyoho poyoho force-pushed the fix/importMataGlob-in-worker branch 2 times, most recently from 25d64fb to 9e86745 Compare April 15, 2022 15:19
@patak-dev
Copy link
Member

I wonder what is your current assessment of this PR @poyoho. If we would like to avoid possible regressions, I think the best would be to queue it for Vite 3.0 (We may start the beta in around two weeks so I'm currently leaning towards this option). If there is some straightforward fix that you would like to extract from this PR to be applied in 2.9, maybe that is also an option. What do you think? 2.9 looks quite solid to me... maybe we should already start focusing our efforts in 3.0

@poyoho
Copy link
Member Author

poyoho commented Apr 16, 2022

In this PR, I reverted the import.meta.url all content (contained renderChunk hook replace), So I think this PR is like a patch.

Fix the following:

  1. import.meta.glob will transform to dynamic import, so only support format: es and module worker.
  2. import.meta.globEager will transform to static import, so only support format: iife|es and module worker.
  3. supplement unit-test for worker constructor in nested worker and simple worker.
  4. fix clean worker source map warning. (emit same file many times)
  5. fix inline worker source map 404 (inline worker self.location.href base on site base. make it use absolute paths.)

And about import.meta.url in worker.

import.meta.url build with es it will replace with esbuild minify will got an unexpected data.

This can be solved only by upgrading the target of esbuild.

import.meta.url build with iife it will polyfill with rollup will be used document in the worker

Rollup should set format: "es" to avoid it.

Discussing this problem, we can ignore the classic worker because it can't work in dev. I think it can be preset that the user has already set a higher version of the target.

format: es should need to setting higher version of the target. So I think it is ok.

format: iife after setting higher version of the target it also output compatible code I think format: "iife" can still be replaced.

@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 20, 2022
poyoho and others added 3 commits April 20, 2022 18:53
@poyoho
Copy link
Member Author

poyoho commented Apr 20, 2022

@bluwy Thank for your review.

bluwy
bluwy previously approved these changes Apr 20, 2022
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

LGTM. Perhaps we can tackle the import.meta.url replacement in a separate PR, or in Vite 3 when we preserve import.meta.url.

@poyoho
Copy link
Member Author

poyoho commented Apr 20, 2022

I've done something similar, but it's really a little difficult

@poyoho
Copy link
Member Author

poyoho commented Apr 24, 2022

the import.meta.url can resolve in #7837 (comment).

@patak-dev patak-dev changed the title fix: import.meta.* fix(worker): import.meta.* Apr 26, 2022
@patak-dev patak-dev merged commit b092697 into vitejs:main Apr 26, 2022
@poyoho poyoho deleted the fix/importMataGlob-in-worker branch April 27, 2022 01:23
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.

import.meta.glob doesn't work in webworker in production
4 participants