kit
kit copied to clipboard
feat: enable cwd override for vite plugin
When using SvelteKit in a monorepo, currently you are forced to keep it in the root because the sveltekit looks for the svelte.config.js in the process.cwd().
This prevents us from:
- Having multiple sveltekit apps in nested folders
- Which we would be able to build/serve from the root of the monorepo
Note: Test seems to be failing with unrelated code
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
- [X] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.
Tests
- [X] Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- [X] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- [X] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
🦋 Changeset detected
Latest commit: dec9864c73d248b4074a896f2ae8f51b96530a56
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 | Minor |
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
Hello! I encountered the same issue as you but missed your PR, so I created my own here (https://github.com/sveltejs/kit/pull/12420). I have no problem keeping yours, but I found some cases you missed.
Things about the analyse part and pre-rendering for example. If needed, I would be happy to help!
I'm not sure this is needed, you can just set the cwd when invoking the scripts instead:
pnpm --dir apps/app1 build (npm and yarn have similar args)
@Anthony-Jhoiro feel free to add missing parts to this PR or finish yours. I'm fine either way.
@dominikg, you can find an example of the issue in https://github.com/sveltejs/kit/pull/12420.
The problem is if you are using a monorepo tooling such as Nx, Turbo, Lerna, etc. where you typically run command from the root, but then use configuration to specify the cwd overrides. You can do this via vite.config, but sveltekit plugin does not respect that nor can you currently explicitly pass it as a parameter - which this PR is trying to fix.
please share an actual repo that demonstrates the problem this PR tries to solve. The linked issue has a screenshot of a directory structure but it does not show the actual package.json scripts being used or describe what the goal really is.
In general for a change of this significance i would prefer creating an issue/feature request first instead of just sending a PR that could be the solution but also just a workaround.
Sveltekit deliberately loads svelte config and passes it on to vite-plugin-svelte so that it has full control, but did you explore https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#config-file-resolving ?
Monorepo software should have tools to run scripts in various directories, so again i am not sure why a cwd option is needed for sveltekits vite plugin - which would have to be added to vite.config.
Example: https://github.com/sveltejs/kit/blob/657e52aed75f359c27a6fe53eb4b44c9d107a2f0/package.json#L20 builds all public packages in their respective directories in this monorepo
You can check this repo: https://github.com/meeroslav/sveltauri/tree/sveltekit-issue
To reproduce:
- Clone the
[email protected]:meeroslav/sveltauri.git - Checkout the branch
sveltekit-issue - Run
pnpm install - Run
pnpm nx build frontend --verbose
You should see this error:
The sveltekit() plugin from frontend/vite.config.js still looks for svelte.config.js in the root rather than the folder where the vite config is.
The pnpm --dir ... would not work in the following scenario for 2 reasons:
- That subfolder doesn't need to have
package.json(as is the case in the provided repo) - The
nxcommand can be run to build multiple projects at the same time that live in different folders so singledirwould not suffice
Additionally, adding:
svelte({
configFile: 'frontend/svelte.config.js'
}),
doesn't seem to be having any impact.
That subfolder doesn't need to have package.json (as is the case in the provided repo)
i am not sure how this is supposed to work with users installing dependencies that are not shared between apps or use tooling like svelte-add that might expect a package.json to exist. create-svelte scaffolds sveltkit applications with a vite.config.js,svelte.config.js and package.json in place and my recommendation would be to keep all that for compatibility within the svelte ecosystem.
If you change all that, i'd say it is your responsibility to also ensure that your script runner set the right cwd beforehand to avoid assumptions inside sveltekit or other tooling from breaking.
Happy to discuss how your case can be accomodated, but a PR thread is the wrong place for that. Please open a feature request describing how excactly vite is invoked and what you want to achieve. are you setting vite root differently, how does it work with other frameworks etc.
The
pnpm --dir ...would not work in the following scenario for 2 reasons:
- That subfolder doesn't need to have
package.json(as is the case in the provided repo)- The
nxcommand can be run to build multiple projects at the same time that live in different folders so singledirwould not suffice
the filter command i posted above would work, if there was a package.json with a name in the subpackage...
Additionally, adding:
svelte({ configFile: 'frontend/svelte.config.js' }),doesn't seem to be having any impact.
yes, in sveltekit configFile is currently ignored but it could have been an alternative option to your cwd proposal here...
@meeroslav @dominikg has a feature request been created to add better support for Nx/monorepo projects?
@dominikg @meeroslav I've just added a Feature Request for this issue to be resolved
closing as resolved in https://github.com/sveltejs/kit/issues/12499#issuecomment-2320091512