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: execute classic worker in dev mode #7099

Merged
merged 22 commits into from Mar 3, 2022

Conversation

poyoho
Copy link
Member

@poyoho poyoho commented Feb 26, 2022

Description

fix: #4586
fix: #5853
fix: #7019
close: #6973

Additional context

Detect the case where { type: 'module' } is not provided or { type: 'classic' } is passed with a static string and use classic workers. If a dynamic expression appears, we can issue an error. And give /* @vite-ignore */ out of Vite processing.

  1. we should throw the dynamic worker type error, so we should find the all worker options.
  2. use re match worker type for avoid the situation that json5 cannot parse functions call string.
  3. dynamic worker options type we can't know how import the env so don't inject the env in dev mode, but build mode it also be replaced.

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 p3-minor-bug An edge case that only affects very specific usage (priority) labels Feb 26, 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.

I'm not the most familiar with workers, but I'm not sure if we want to introduce a special @vite-ignore comment here. That would be non-standard and prone to errors, e.g. some tooling strips comments.

For dynamic cases, I think Vite should just ignore it without an error, and preserve the user code as is? 🤔

Edit: Ah looks like @patak-dev suggested something similar at #7019 (comment)

@poyoho
Copy link
Member Author

poyoho commented Feb 26, 2022

I think user can't to write code compatible classic worker and module worker When I write the test case for dynamic worker.
The dynamic worker may only happen in user accidentally passed in the variable. So I think the error report is reasonable.

But vite can automatically ignore this worker file, so it only needs warning.

@poyoho
Copy link
Member Author

poyoho commented Feb 27, 2022

"Exceeded timeout of 30000 ms for a test.

I don't know why got a error in unit test

@poyoho
Copy link
Member Author

poyoho commented Feb 27, 2022

🤕 import * as JSON5 from 'json5' is work in my machine.

patak-dev
patak-dev previously approved these changes Feb 27, 2022
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Looks good to me. Let's wait for the 2.9 beta to merge it to get some testing.

@patak-dev patak-dev requested a review from bluwy February 27, 2022 06:48
@patak-dev patak-dev added this to the 2.9 milestone Feb 27, 2022
@poyoho
Copy link
Member Author

poyoho commented Feb 28, 2022

@ygj6 would you help reviewing this PR?

I modify your method(getEndIndex), in this case it will got a error.

/* ) */ ) 
// comment 

it will got a single comment index and the endIndex no in the single comment, return a error value(index of multiline comment ))

so I use the small index to matched.

@poyoho poyoho requested a review from bluwy March 1, 2022 01:53
ygj6
ygj6 previously approved these changes Mar 1, 2022
Copy link
Member

@ygj6 ygj6 left a comment

Choose a reason for hiding this comment

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

LGTM

@poyoho poyoho requested a review from patak-dev March 1, 2022 05:03
@poyoho poyoho requested review from ygj6 and patak-dev March 1, 2022 09:59
ygj6
ygj6 previously approved these changes Mar 1, 2022
packages/vite/src/node/plugins/workerImportMetaUrl.ts Outdated Show resolved Hide resolved
@patak-dev patak-dev merged commit 3c0a609 into vitejs:main Mar 3, 2022
@bluwy bluwy mentioned this pull request Mar 8, 2022
7 tasks
@poyoho poyoho deleted the fix/worker-module branch March 19, 2022 13:30
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
5 participants