1
1
import Debug from 'debug'
2
- import { StartDevServer } from '.'
3
- import { createServer , ViteDevServer , InlineConfig } from 'vite'
2
+ import { createServer , ViteDevServer , InlineConfig , UserConfig } from 'vite'
4
3
import { dirname , resolve } from 'path'
4
+ import getPort from 'get-port'
5
5
import { makeCypressPlugin } from './makeCypressPlugin'
6
- import { EventEmitter } from 'events'
7
6
8
7
const debug = Debug ( 'cypress:vite-dev-server:start' )
9
8
10
- // TODO: Pull in types for Options so we can infer these
11
- const serverConfig = ( projectRoot : string , supportFilePath : string , devServerEvents : EventEmitter ) : InlineConfig => {
12
- return {
13
- root : resolve ( __dirname , projectRoot ) ,
14
- base : '/__cypress/src/' ,
15
- plugins : [ makeCypressPlugin ( projectRoot , supportFilePath , devServerEvents ) ] ,
16
- server : {
17
- port : 0 ,
18
- } ,
19
- resolve : {
20
- alias : {
21
- // Necessary to avoid a "prefixIdentifiers" issue from slots mounting
22
- // Could be resolved in test-utils
23
- '@vue/compiler-core' : resolve ( dirname ( require . resolve ( '@vue/compiler-core' ) ) , 'dist' , 'compiler-core.cjs.js' ) ,
24
- } ,
25
- } ,
26
- }
9
+ interface Options {
10
+ specs : Cypress . Cypress [ 'spec' ] [ ]
11
+ config : Record < string , string >
12
+ devServerEvents : EventEmitter
13
+ [ key : string ] : unknown
14
+ }
15
+
16
+ export interface StartDevServer {
17
+ /* this is the Cypress options object */
18
+ options : Options
19
+ /* support passing a path to the user's webpack config */
20
+ viteConfig ?: UserConfig // TODO: implement taking in the user's vite configuration. Right now we don't
27
21
}
28
22
29
- const resolveServerConfig = ( { viteConfig, options } : StartDevServer ) => {
30
- const defaultServerConfig = serverConfig (
31
- options . config . projectRoot ,
32
- options . config . supportFile ,
33
- options . devServerEvents ,
34
- )
23
+ const resolveServerConfig = async ( { viteConfig, options } : StartDevServer ) : Promise < InlineConfig > => {
24
+ const { projectRoot, supportFile } = options . config
35
25
36
- const requiredOptions = {
37
- base : defaultServerConfig . base ,
38
- root : defaultServerConfig . root ,
26
+ const requiredOptions : InlineConfig = {
27
+ base : '/__cypress/src/' ,
28
+ root : projectRoot ,
39
29
}
40
30
41
- const finalConfig = { ...defaultServerConfig , ...viteConfig , ...requiredOptions }
31
+ const finalConfig : InlineConfig = { ...viteConfig , ...requiredOptions }
32
+
33
+ finalConfig . plugins = [ ...( viteConfig . plugins || [ ] ) , makeCypressPlugin ( projectRoot , supportFile , options . devServerEvents ) ]
34
+
35
+ // This alias is necessary to avoid a "prefixIdentifiers" issue from slots mounting
36
+ // only cjs compiler-core accepts using prefixIdentifiers in slots which vue test utils use.
37
+ // Could we resolve this usage in test-utils?
38
+ finalConfig . resolve = finalConfig . resolve || { }
39
+ finalConfig . resolve . alias = {
40
+ ...finalConfig . resolve . alias ,
41
+ '@vue/compiler-core' : resolve ( dirname ( require . resolve ( '@vue/compiler-core' ) ) , 'dist' , 'compiler-core.cjs.js' ) ,
42
+ } ,
43
+
44
+ finalConfig . server = finalConfig . server || { }
42
45
43
- finalConfig . plugins = [ ...( viteConfig . plugins || [ ] ) , defaultServerConfig . plugins [ 0 ] ]
44
- finalConfig . server . port = defaultServerConfig . server . port
46
+ finalConfig . server . port = await getPort ( { port : 3000 , host : 'localhost' } ) ,
45
47
46
48
debug ( `the resolved server config is ${ JSON . stringify ( finalConfig , null , 2 ) } ` )
47
49
@@ -55,7 +57,7 @@ export async function start (devServerOptions: StartDevServer): Promise<ViteDevS
55
57
}
56
58
57
59
debug ( 'starting vite dev server' )
58
- const resolvedConfig = resolveServerConfig ( devServerOptions )
60
+ const resolvedConfig = await resolveServerConfig ( devServerOptions )
59
61
60
62
return createServer ( resolvedConfig )
61
63
}
0 commit comments