Skip to content

Re-optimize Svelte libraries when compile options change #419

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

Closed
benmccann opened this issue Aug 23, 2022 · 5 comments
Closed

Re-optimize Svelte libraries when compile options change #419

benmccann opened this issue Aug 23, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@benmccann
Copy link
Member

benmccann commented Aug 23, 2022

Describe the problem

Because the optimized Svelte libraries depend on some Svelte compile options, whenever the Svelte options change we want to trigger a re-optimize when using the prebundleSvelteLibraries option.

Describe the proposed solution

This info is currently kept in a _svelte_metadata.json in the Vite cache dir. So whenever that file doesn’t exist or if we compare and it’s stale, we would re-optimize.

As part of this, we should check if this metadata json is correctly saved in the right place, and not accidentally purged by Vite 3's new prebundling flow, eg the .vite/deps_temp thing.

Alternatives considered

Ideally we may want Vite to expose an API to say “mark optimized deps as stale and re-optimize on next run”

Importance

nice to have

@benmccann benmccann added enhancement New feature or request triage Awaiting triage by a project member labels Aug 23, 2022
@dominikg
Copy link
Member

dominikg commented Oct 2, 2022

We already restart the devserver on svelte config change here:

and server.restart has a flag to force reoptimization https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/index.ts#L265

So to implement this, we have to check if the project has svelte dependencies and add that information to the restart call. Re-optimize always has to be done in full, as there could be dependencies between optimized dependencies that need to be updated as well.

const forcePrebundle = hasSvelteDeps();
server.restart(forcePrebundle);

Question is how to get that information there.

@dominikg dominikg removed the triage Awaiting triage by a project member label Oct 2, 2022
@benmccann
Copy link
Member Author

benmccann commented Oct 20, 2022

server.restart has a flag to force reoptimization

That link is outdated, so here's a permalink for reference: https://github.com/vitejs/vite/blob/27e28328235d2f5c6a44129a7635b697097ec50a/packages/vite/src/node/server/index.ts#L271

Ok, so the challenge is we call watcher.on and need to know if the project has svelte dependencies from within the watcher. I think as a shortcut we could always force reoptimization. This would probably be fair to do because the vast majority of projects of any reasonable complexity will have a svelte dependency and changing the Svelte config is relatively rare. If a project doesn't have any Svelte dependencies then it's probably cheaper on average to reoptimize. While we could certainly do better in that case I think it'd be okay for us to take the easy way out as a quick way to get the prebundling feature turned on

@dominikg
Copy link
Member

Always reoptimizing would also affect non-svelte libraries. At the very least we should only do it when prebundleSvelteLibraries is active

@bluwy
Copy link
Member

bluwy commented Oct 20, 2022

Not sure if I miss something, but I think this is already implemented in the buildStart hook:

// Force Vite to optimize again. Although we mutate the config here, it works because
// Vite's optimizer runs after `buildStart()`.
// TODO: verify this works in vite3
viteConfig.optimizeDeps.force = true;

It is sort-of a hack but it prevents unnecessary restarts.

@benmccann
Copy link
Member Author

ah, thanks! closing this then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants