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

Module API to start the SvelteKit (vite) dev server #2921

Closed
JessicaSachs opened this issue Nov 26, 2021 · 5 comments
Closed

Module API to start the SvelteKit (vite) dev server #2921

JessicaSachs opened this issue Nov 26, 2021 · 5 comments

Comments

@JessicaSachs
Copy link

JessicaSachs commented Nov 26, 2021

Describe the problem

Hello! I'm adding Svelte (and SvelteKit) support for Cypress Component Testing.

For component testing in Cypress, we use a dev-server based architecture to bundle and serve each of your components (and their tests!) in isolation. Kind of like if each of your Spec files got its own HTML page.

Whenever we add support for a new app framework, we need the ability to start the user's local dev server from a node process and receive a handle to the server that was started.

Since SvelteKit is built on top of Vite, I can just use our existing @cypress/vite-dev-server, except that because SvelteKit's svelte.config.js config file augments the Vite Config with its own internal options (similar to how CRA, NextJS and Vue CLI wrap Webpack) I need to be able to get at the resolved configuration.

Describe the proposed solution

I would like one of two things. Either:

  • A method to resolve the merged SvelteKit Config + Vite Config options
  • A module API to start the SvelteKit dev server and return the server instance just started

Alternatives considered

No response

Importance

Cannot use SvelteKit without this.

Additional Information

I can likely work around this for simple use cases, but without Unless SvelteKit exposes the resolved options or there's a module API to start the server, we'll be unable to test components that reference the $app module.

@JessicaSachs JessicaSachs changed the title Module API to start the sveltekit (vite) dev server Module API to start the SvelteKit (vite) dev server Nov 26, 2021
@dominikg
Copy link
Member

To get a reference to the devserver and config, you can ask users to add a plugin to kit.vite.plugins in svelte.config.js
and use the hooks provided by vite
https://vitejs.dev/guide/api-plugin.html#configresolved
https://vitejs.dev/guide/api-plugin.html#configureserver

export function vitePluginCypressHelper() {
  let server: ViteDevServer;
  let config: ConfigResolved;
  const plugin = {
    name: 'vite-plugin-cypress-helper',
    apply: 'serve', // plugin will be skipped on vite build
    configResolved(_config) {
      config = _config;
    }
    configureServer(_server) {
      server = _server;
      // server has useful api too, like access to the pluginContainer, moduleGraph
    }
    buildStart() {
      // called after initialization, do stuff with server and config here
    }
  }
}

vite plugins can modify config (should be stable in configResolved) and customize module resolving too (eg https://github.com/aleclarson/vite-tsconfig-paths) so i'd recommend to use vites own api on the devserver instance over just parsing the config unless you are sure that the information you get from the config (path aliases i assume?) is working as intended in vite at runtime

@JessicaSachs
Copy link
Author

@dominikg I can't believe it's been 4 days already! I'll give this a go tonight.

@ambrt
Copy link

ambrt commented Dec 9, 2021

@JessicaSachs Any updates on this?

@JessicaSachs
Copy link
Author

JessicaSachs commented Mar 15, 2022

Did I seriously already create this issue? Even if my short term memory is bad, at least I'm consistent.

@Rich-Harris
Copy link
Member

Closing this as it appears to be a dupe of #4329 (or vice versa, but the discussion is happening over there)

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

No branches or pull requests

4 participants