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: broken worker urls in 3rd party modules (fix #10838) #16418

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Danielku15
Copy link

@Danielku15 Danielku15 commented Apr 13, 2024

Description

fixes #10838

As commented in the issues and we can see in the diff the main change is to introduce an additional "fallback file search" for worker files.

3rd party dependencies (from node_modules) will have their bundle output in the vite cache folder.

Situation Before: If this dependency references a worker using the ?module syntax, Vite derives a wrong file path for the worker which doesn't exist. Later the URL for the worker points to a non-existent file which is never generated. It is treated as optimized dependency.

Situation After: We check if the file importing the worker is an optimized dependency, and if yes, we try to resolve the worker file relative to the source of the optimized dependency. This way we jump back into node_modules for the worker file and the normal flow activates.

Checklist:

  • Prepare main change working with vite dev
  • Prepare a stub for the unit test
  • Manually test the fix
  • Get automatic tests running
  • Manually ensure the fix works also with vite build output.

Copy link

stackblitz bot commented Apr 13, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@Danielku15 Danielku15 force-pushed the fix/workers-in-3rd-party-modules branch from f6f670f to 5659f47 Compare April 13, 2024 13:40
@hi-ogawa
Copy link
Collaborator

I haven't looked into the original reproduction and the test case here, but would adding optimizeDeps.exclude: ["@vitejs/test-dep-with-worker"] help for these situations?

When node_modules contains a code which requires Vite specific features (such as ?url), I expected it wouldn't work if it optimized, so I normally put it in optimizeDeps.exclude when the package is intended to be consumed through a vite plugin.

@Danielku15
Copy link
Author

@hi-ogawa I have't tried this but from the issue discussion it might have worked for some people. But the problem with this approach is: The library authors don't control the config of the consumer projects. Library authors need to document then for each framework how to integrate and configure the exceptions. Depending on the Frontend Framework used it gets more complex as the vite.config might be hidden from the user. To avoid this complexity it would be good to support this scenario properly in Vite. But I agree that it might be a valid workaround until it is fixed.

e.g. Angular requires you to move to a custom builder to specify such things:

Exposing vite configuration does not align with the Angular CLI design goals.
To add additional Vite plugins please use a custom builder.
Originally posted by @alan-agius4 in angular/angular-cli#26859 (comment)

@hi-ogawa
Copy link
Collaborator

@Danielku15 Yeah, it's certainly unintuitive (and fair to say it's a bug) when something breaks after the code is moved to node_modules. Thanks for looking into the fix. I was merely suggesting a common workaround since it wasn't mentioned in the original issue.

Btw, it looks like import.meta.url is not working with deps optimizer in some cases. I wonder how "planned" fix there relates to the usage with ?worker.

@Danielku15
Copy link
Author

@Danielku15 Yeah, it's certainly unintuitive (and fair to say it's a bug) when something breaks after the code is moved to node_modules. Thanks for looking into the fix. I was merely suggesting a common workaround since it wasn't mentioned in the original issue.

Btw, it looks like import.meta.url is not working with deps optimizer in some cases. I wonder how "planned" fix there relates to the usage with ?worker.

Very good point. The bug description looks very similar to the worker problem. I haven't used these features yet but assuming that that the assetImportMetaUrl is the relevant plugin for #8427 it might suffer from the exact same problem like the worker. Extending the code here might fix the bug:

file = tryFsResolve(file, fsResolveOptions) ?? file

Hence it would make sense to put the new tryOptimizedDepResolve into a common place and share it. But I'd have to dig a bit deeper into this to understand the other plugin behavior and flow.

@Danielku15
Copy link
Author

@hi-ogawa I moved the new resolve helper function to a shared place and use it now in the worker and assets import meta url module. According to the tests I could find as a reference in #8427 the output is now as expected with this change.

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.

"Could not read from file …" error thrown when using ?url and ?worker suffixes inside 3rd party module
2 participants