-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathmakeWebpackConfig.ts
90 lines (72 loc) · 2.67 KB
/
makeWebpackConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { debug as debugFn } from 'debug'
import * as path from 'path'
import * as webpack from 'webpack'
import { merge } from 'webpack-merge'
import defaultWebpackConfig from './webpack.config'
import LazyCompilePlugin from 'lazy-compile-webpack-plugin'
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptions } from './plugin'
const debug = debugFn('cypress:webpack-dev-server:makeWebpackConfig')
const WEBPACK_MAJOR_VERSION = Number(webpack.version.split('.')[0])
export interface UserWebpackDevServerOptions {
/**
* if `true` will compile all the specs together when the first one is request and can slow up initial build time.
* @default false
*/
disableLazyCompilation?: boolean
}
interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions, UserWebpackDevServerOptions {
webpackDevServerPublicPathRoute: string
isOpenMode: boolean
}
const mergePublicPath = (baseValue, userValue = '/') => {
return path.join(baseValue, userValue, '/')
}
function getLazyCompilationWebpackConfig (options: MakeWebpackConfigOptions): webpack.Configuration {
if (options.disableLazyCompilation || !options.isOpenMode) {
return {}
}
switch (WEBPACK_MAJOR_VERSION) {
case 4:
return { plugins: [new LazyCompilePlugin()] }
case 5:
return { experiments: { lazyCompilation: true } } as webpack.Configuration
default:
return { }
}
}
export async function makeWebpackConfig (userWebpackConfig: webpack.Configuration, options: MakeWebpackConfigOptions): Promise<webpack.Configuration> {
const { projectRoot, webpackDevServerPublicPathRoute, files, supportFile, devServerEvents } = options
debug(`User passed in webpack config with values %o`, userWebpackConfig)
debug(`New webpack entries %o`, files)
debug(`Project root`, projectRoot)
debug(`Support file`, supportFile)
const entry = path.resolve(__dirname, './browser.js')
const publicPath = mergePublicPath(webpackDevServerPublicPathRoute, userWebpackConfig?.output?.publicPath)
const dynamicWebpackConfig = {
output: {
publicPath,
},
plugins: [
new CypressCTOptionsPlugin({
files,
projectRoot,
devServerEvents,
supportFile,
}),
],
}
const mergedConfig = merge<webpack.Configuration>(
userWebpackConfig,
defaultWebpackConfig,
dynamicWebpackConfig,
getLazyCompilationWebpackConfig(options),
)
mergedConfig.entry = entry
debug('Merged webpack config %o', mergedConfig)
if (process.env.WEBPACK_PERF_MEASURE) {
// only for debugging
const { measureWebpackPerformance } = require('./measureWebpackPerformance')
return measureWebpackPerformance(mergedConfig)
}
return mergedConfig
}