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

feat: provide Vite hooks api #5694

Closed
wants to merge 10 commits into from

Conversation

userquin
Copy link
Contributor

@userquin userquin commented Jul 24, 2022

This PR will expose some hooks to avoid race condition on Vite, since all plugins will run in parallel and some will need the prerender process to finish: pwa plugin and sitemap for example.

There is a thread in Vite Discord server: https://discord.com/channels/804011606160703521/1000700061094776832

The latest changes @benmccann is making to the alignment of Vite and SvelteKit will not work with some plugins, even though the process.exit in the closeBundle has been removed. The main problem is that the async and parallel types of the plugins involved cause a race condition to exist on either writeBundle or closeBundle.

I also send a PR to include a new preCloseBundle hook: vitejs/vite#9326

closes #5709

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jul 24, 2022

🦋 Changeset detected

Latest commit: 6300815

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@userquin
Copy link
Contributor Author

I have also excluded IntelliJ/WebStorm stuff in .gitignore file.

@userquin
Copy link
Contributor Author

userquin commented Jul 24, 2022

Maybe we can reverse the control, just registering the plugin names in the kit config or kit vite plugin options, and find them in the call, and so kit will control it, vite plugins will not need to do anything: kit logic will just check for api.hook function in each configured plugin.

chore: move some logic to utils module
@userquin
Copy link
Contributor Author

I just added in prerendering test a log with a dummy plugin: https://imgur.com/AekOLxE

chore: provide kit and vite config to the hooks
@userquin
Copy link
Contributor Author

you should change the netlify adapter build script, it is failing in Windows (it is using cp to copy a js file)

@Rich-Harris
Copy link
Member

Thank you but can you please open an issue before creating very large PRs like this? It adds a lot of code and complexity and it's completely unclear to me what it does or what problem it solves.

@userquin
Copy link
Contributor Author

Thank you but can you please open an issue before creating very large PRs like this? It adds a lot of code and complexity and it's completely unclear to me what it does or what problem it solves.

I'll try to link a new issue later

@userquin
Copy link
Contributor Author

userquin commented Jul 26, 2022

Thank you but can you please open an issue before creating very large PRs like this? It adds a lot of code and complexity and it's completely unclear to me what it does or what problem it solves.

Created issue: since SvelteKit has removed the process.exit in the closeBundle hook, we can also remove the onKitAdapter hook, it can be done with a plugin with closeBundle hook configured after kit plugin.

EDIT: the onKitConfig hook is to avoid duplicated configuration, kit configuration and plugin configuration. In the vite-plugin-pwa we need a few entries to add the pages properly to the service worker's precache manifest and the navigation fallback; you can check default kit configuration used here https://github.com/antfu/vite-plugin-pwa/pull/327/files#diff-ce40c1fd4c255d5794d1e333ec10be2a01544960143649d69521cdba4d023ccf

@benmccann
Copy link
Member

I think https://github.com/vitejs/vite/discussions/9442 is a better solution to this problem, so let's see what happens there first

@Rich-Harris
Copy link
Member

Came here to post the same thing as @benmccann. This is a problem that should be solved at the Vite level. Will close this PR, thanks

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.

Vite Hooks Api
3 participants