1
1
import { debug as debugFn } from 'debug'
2
2
import * as path from 'path'
3
- import { Configuration } from 'webpack'
3
+ import * as webpack from 'webpack'
4
4
import { merge } from 'webpack-merge'
5
+ import defaultWebpackConfig from './webpack.config'
6
+ import LazyCompilePlugin from 'lazy-compile-webpack-plugin'
5
7
import CypressCTOptionsPlugin , { CypressCTOptionsPluginOptions } from './plugin'
6
8
7
9
const debug = debugFn ( 'cypress:webpack-dev-server:makeWebpackConfig' )
10
+ const WEBPACK_MAJOR_VERSION = Number ( webpack . version . split ( '.' ) [ 0 ] )
11
+
12
+ export interface UserWebpackDevServerOptions {
13
+ /**
14
+ * if `true` will compile all the specs together when the first one is request and can slow up initial build time.
15
+ * @default false
16
+ */
17
+ disableLazyCompilation ?: boolean
18
+ }
19
+
20
+ interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions , UserWebpackDevServerOptions {
21
+ webpackDevServerPublicPathRoute : string
22
+ isOpenMode : boolean
23
+ }
8
24
9
25
const mergePublicPath = ( baseValue , userValue = '/' ) => {
10
26
return path . join ( baseValue , userValue , '/' )
11
27
}
12
28
13
- interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions {
14
- webpackDevServerPublicPathRoute : string
29
+ function getLazyCompilationWebpackConfig ( options : MakeWebpackConfigOptions ) : webpack . Configuration {
30
+ if ( options . disableLazyCompilation || ! options . isOpenMode ) {
31
+ return { }
32
+ }
33
+
34
+ switch ( WEBPACK_MAJOR_VERSION ) {
35
+ case 4 :
36
+ return { plugins : [ new LazyCompilePlugin ( ) ] }
37
+ case 5 :
38
+ return { experiments : { lazyCompilation : true } } as webpack . Configuration
39
+ default :
40
+ return { }
41
+ }
15
42
}
16
43
17
- export async function makeWebpackConfig ( userWebpackConfig : Configuration , options : MakeWebpackConfigOptions ) : Promise < Configuration > {
44
+ export async function makeWebpackConfig ( userWebpackConfig : webpack . Configuration , options : MakeWebpackConfigOptions ) : Promise < webpack . Configuration > {
18
45
const { projectRoot, webpackDevServerPublicPathRoute, files, supportFile, devServerEvents } = options
19
46
20
47
debug ( `User passed in webpack config with values %o` , userWebpackConfig )
21
48
22
- const defaultWebpackConfig = require ( './webpack.config' )
23
-
24
- debug ( `Merging Evergreen's webpack config with users'` )
25
-
26
49
debug ( `New webpack entries %o` , files )
27
50
debug ( `Project root` , projectRoot )
28
51
debug ( `Support file` , supportFile )
@@ -45,7 +68,12 @@ export async function makeWebpackConfig (userWebpackConfig: Configuration, optio
45
68
] ,
46
69
}
47
70
48
- const mergedConfig = merge < Configuration > ( userWebpackConfig , defaultWebpackConfig , dynamicWebpackConfig )
71
+ const mergedConfig = merge < webpack . Configuration > (
72
+ userWebpackConfig ,
73
+ defaultWebpackConfig ,
74
+ dynamicWebpackConfig ,
75
+ getLazyCompilationWebpackConfig ( options ) ,
76
+ )
49
77
50
78
mergedConfig . entry = entry
51
79
0 commit comments