Skip to content

Conversation

gwennlbh
Copy link

@gwennlbh gwennlbh commented Jul 2, 2025

Probably needs some more code in case the service worker path is changed in the sveltekit config (to change the entry), or automatic sw registration is disabled (to remove the entry)

Relevant config pages: https://svelte.dev/docs/kit/service-workers (general info) and https://svelte.dev/docs/kit/$service-worker (for the $service-worker alias)

I also did not test this locally, I made changes in my knip.json config in a project, and dug thru the knip codebase and edited on github.com directly after confirming that my config changes worked

Closes #1162

@webpro
Copy link
Member

webpro commented Jul 16, 2025

Thanks Gwenn!

If the service worker location depends on the local sveltekit config, then resolveConfig can be added to the plugin, and then conditionally have it return the entry (this config is the first argument of resolveConfig(config, options) {}.

Would also be great if you could add a fixture or two to exercise the updated plugin a bit in a new or existing test.

@gwennlbh
Copy link
Author

How should I go about getting the value of the service worker path from the local config? From what I see in the Vite plugin, you use static analysis instead of running, in that case, svelte.config.[jt]s ? I guess this would prevent dynamic setups from working, which is fine imo

Considering that there are multiple ways to define the config (a const declaration then a export default config statement, as I saw in the fixture; a direct export default statement, ...) I wonder if you have utils already in place to help with that, so that I don't reinvent some wheels?

@webpro
Copy link
Member

webpro commented Jul 16, 2025

For now (not behind a computer), I can say that Knip actually loads the config file and the function receives the exported resolved object. Hopefully that's mentioned in the docs somewhere ("writing a plugin"). There is resolveAST though, if necessary (but rarely the case).

@gwennlbh
Copy link
Author

gwennlbh commented Jul 16, 2025

ok so, i think I got it. sorry for not checking out the contributing.md, i was searching for it at /, didn't see it under .github/ '^^

I still get the unused file error when running bun test tests/plugins/svelte.test.ts , I don't really know why. console.log doesn't seem to work either, so I can't see what's wrong in my resolveConfig logic '^^

EDIT: If I hardcode src/sw.ts in const production = [ it still shows an error... what am I doing wrong?

@webpro
Copy link
Member

webpro commented Jul 16, 2025

Please see the following docs for more details & tips:

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.

🐛 SvelteKit's src/service-worker.{ts,js} is marked as unused
2 participants