Skip to content

Commit f237050

Browse files
feat: Use lazy compilation for webpack-dev-server by default (#15158)
* Use lazy compilation for webpack-dev-server * Do not lazy compile in run mode * Cache babel on CI * Fix config * Fix ts error * Update webpack-dev-server unit tests Co-authored-by: ElevateBart <ledouxb@gmail.com>
1 parent 26edc21 commit f237050

File tree

11 files changed

+74
-34
lines changed

11 files changed

+74
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ packages/server/test/support/fixtures/server/libs
3838
# from npm/react
3939
/npm/react/bin/*
4040
/npm/react/cypress/videos
41+
/npm/react/.babel-cache
4142

4243
# from runner-ct
4344
/packages/runner-ct/cypress/screenshots

circle.yml

+8
Original file line numberDiff line numberDiff line change
@@ -1086,13 +1086,21 @@ jobs:
10861086
- attach_workspace:
10871087
at: ~/
10881088
- check-conditional-ci
1089+
- restore_cache:
1090+
name: Restore yarn cache
1091+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-npm-react-babel-cache
10891092
- run:
10901093
name: Build
10911094
command: yarn workspace @cypress/react build
10921095
- run:
10931096
name: Run tests
10941097
command: yarn workspace @cypress/react test
10951098
- store-npm-logs
1099+
- save_cache:
1100+
name: Save Cache
1101+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-npm-react-babel-cache
1102+
paths:
1103+
- cypress/npm/react/.babel-cache
10961104
- persist_to_workspace:
10971105
root: ~/
10981106
paths: cypress/npm/react

cli/types/cypress.d.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -5134,11 +5134,7 @@ declare namespace Cypress {
51345134

51355135
interface DevServerOptions {
51365136
specs: Spec[]
5137-
config: {
5138-
supportFile?: string
5139-
projectRoot: string
5140-
webpackDevServerPublicPathRoute: string
5141-
},
5137+
config: ResolvedConfigOptions & RuntimeConfigOptions,
51425138
devServerEvents: NodeJS.EventEmitter,
51435139
}
51445140

npm/react/cypress/plugins/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
// @ts-check
12
const { startDevServer } = require('@cypress/webpack-dev-server')
3+
const path = require('path')
24
const babelConfig = require('../../babel.config.js')
35

46
/** @type import("webpack").Configuration */
@@ -17,7 +19,7 @@ const webpackConfig = {
1719
{
1820
test: /\.(js|jsx|mjs|ts|tsx)$/,
1921
loader: 'babel-loader',
20-
options: babelConfig,
22+
options: { ...babelConfig, cacheDirectory: path.resolve(__dirname, '..', '..', '.babel-cache') },
2123
},
2224
{
2325
test: /\.modules\.css$/i,
@@ -59,7 +61,7 @@ const webpackConfig = {
5961
* @type Cypress.PluginConfig
6062
*/
6163
module.exports = (on, config) => {
62-
on('dev-server:start', (options) => startDevServer({ options, webpackConfig }))
64+
on('dev-server:start', (options) => startDevServer({ options, webpackConfig, disableLazyCompilation: false }))
6365

6466
return config
6567
}

npm/webpack-dev-server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"debug": "4.3.2",
14+
"lazy-compile-webpack-plugin": "0.1.11",
1415
"semver": "^7.3.4",
1516
"webpack-merge": "^5.4.0"
1617
},

npm/webpack-dev-server/src/index.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import { EventEmitter } from 'events'
21
import { debug as debugFn } from 'debug'
32
import { AddressInfo } from 'net'
43
import { start as createDevServer } from './startServer'
5-
const debug = debugFn('cypress:webpack-dev-server:webpack')
4+
import { UserWebpackDevServerOptions } from './makeWebpackConfig'
65

7-
export interface DevServerOptions {
8-
specs: Cypress.Cypress['spec'][]
9-
config: {
10-
supportFile: string
11-
projectRoot: string
12-
webpackDevServerPublicPathRoute: string
13-
}
14-
devServerEvents: EventEmitter
15-
}
6+
const debug = debugFn('cypress:webpack-dev-server:webpack')
167

178
type DoneCallback = () => unknown
189

@@ -21,9 +12,9 @@ export interface ResolvedDevServerConfig {
2112
close: (done?: DoneCallback) => void
2213
}
2314

24-
export interface StartDevServer {
15+
export interface StartDevServer extends UserWebpackDevServerOptions {
2516
/* this is the Cypress options object */
26-
options: DevServerOptions
17+
options: Cypress.DevServerOptions
2718
/* support passing a path to the user's webpack config */
2819
webpackConfig?: Record<string, any>
2920
}

npm/webpack-dev-server/src/makeWebpackConfig.ts

+37-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
import { debug as debugFn } from 'debug'
22
import * as path from 'path'
3-
import { Configuration } from 'webpack'
3+
import * as webpack from 'webpack'
44
import { merge } from 'webpack-merge'
5+
import defaultWebpackConfig from './webpack.config'
6+
import LazyCompilePlugin from 'lazy-compile-webpack-plugin'
57
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptions } from './plugin'
68

79
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+
}
824

925
const mergePublicPath = (baseValue, userValue = '/') => {
1026
return path.join(baseValue, userValue, '/')
1127
}
1228

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+
}
1542
}
1643

17-
export async function makeWebpackConfig (userWebpackConfig: Configuration, options: MakeWebpackConfigOptions): Promise<Configuration> {
44+
export async function makeWebpackConfig (userWebpackConfig: webpack.Configuration, options: MakeWebpackConfigOptions): Promise<webpack.Configuration> {
1845
const { projectRoot, webpackDevServerPublicPathRoute, files, supportFile, devServerEvents } = options
1946

2047
debug(`User passed in webpack config with values %o`, userWebpackConfig)
2148

22-
const defaultWebpackConfig = require('./webpack.config')
23-
24-
debug(`Merging Evergreen's webpack config with users'`)
25-
2649
debug(`New webpack entries %o`, files)
2750
debug(`Project root`, projectRoot)
2851
debug(`Support file`, supportFile)
@@ -45,7 +68,12 @@ export async function makeWebpackConfig (userWebpackConfig: Configuration, optio
4568
],
4669
}
4770

48-
const mergedConfig = merge<Configuration>(userWebpackConfig, defaultWebpackConfig, dynamicWebpackConfig)
71+
const mergedConfig = merge<webpack.Configuration>(
72+
userWebpackConfig,
73+
defaultWebpackConfig,
74+
dynamicWebpackConfig,
75+
getLazyCompilationWebpackConfig(options),
76+
)
4977

5078
mergedConfig.entry = entry
5179

npm/webpack-dev-server/src/startServer.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ import { makeWebpackConfig } from './makeWebpackConfig'
66

77
const debug = Debug('cypress:webpack-dev-server:start')
88

9-
export async function start ({ webpackConfig: userWebpackConfig, options }: StartDevServer): Promise<WebpackDevServer> {
9+
export async function start ({ webpackConfig: userWebpackConfig, options, ...userOptions }: StartDevServer): Promise<WebpackDevServer> {
1010
if (!userWebpackConfig) {
1111
debug('User did not pass in any webpack configuration')
1212
}
1313

14-
const { projectRoot, webpackDevServerPublicPathRoute } = options.config
14+
// @ts-expect-error ?? webpackDevServerPublicPathRoute is not a valid option of Cypress.Config
15+
const { projectRoot, webpackDevServerPublicPathRoute, isTextTerminal } = options.config
1516

1617
const webpackConfig = await makeWebpackConfig(userWebpackConfig || {}, {
1718
files: options.specs,
1819
projectRoot,
1920
webpackDevServerPublicPathRoute,
2021
devServerEvents: options.devServerEvents,
21-
supportFile: options.config.supportFile,
22+
supportFile: options.config.supportFile as string,
23+
isOpenMode: !isTextTerminal,
24+
...userOptions,
2225
})
2326

2427
debug('compiling webpack')

npm/webpack-dev-server/src/webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const HtmlWebpackPlugin = require('html-webpack-plugin')
33

4+
/** @type {import('webpack').Configuration} */
45
module.exports = {
56
mode: 'development',
67
optimization: {

npm/webpack-dev-server/test/e2e.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ const specs: Cypress.Cypress['spec'][] = [
5151
const config = {
5252
projectRoot: root,
5353
supportFile: '',
54+
isTextTerminal: true,
5455
webpackDevServerPublicPathRoute: root,
55-
}
56+
} as any as Cypress.ResolvedConfigOptions & Cypress.RuntimeConfigOptions
5657

5758
describe('#startDevServer', () => {
5859
it('serves specs via a webpack dev server', async () => {

yarn.lock

+9-1
Original file line numberDiff line numberDiff line change
@@ -13421,7 +13421,7 @@ detect-node@^2.0.4:
1342113421
resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c"
1342213422
integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==
1342313423

13424-
detect-port-alt@1.1.6:
13424+
detect-port-alt@1.1.6, detect-port-alt@^1.1.6:
1342513425
version "1.1.6"
1342613426
resolved "https://registry.yarnpkg.com/detect-port-alt/-/detect-port-alt-1.1.6.tgz#24707deabe932d4a3cf621302027c2b266568275"
1342713427
integrity sha512-5tQykt+LqfJFBEYaDITx7S7cR7mJ/zQmLXZ2qt5w04ainYZw6tBf9dBunMjVeVOdYVRUzUOE4HkY5J7+uttb5Q==
@@ -21463,6 +21463,14 @@ lazy-cache@^1.0.3:
2146321463
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
2146421464
integrity sha1-odePw6UEdMuAhF07O24dpJpEbo4=
2146521465

21466+
lazy-compile-webpack-plugin@0.1.11:
21467+
version "0.1.11"
21468+
resolved "https://registry.yarnpkg.com/lazy-compile-webpack-plugin/-/lazy-compile-webpack-plugin-0.1.11.tgz#be3b9487ccc731a606dc55bcfcd80000c72e4237"
21469+
integrity sha512-d2D72x0XqFSj83SRmgx1dvgRvmyIoXpC2lMj+XVS+xzt0FxXDPzF/2FbxOVmW9gzp6d8U29Ne4RGF4x+MTYwow==
21470+
dependencies:
21471+
detect-port-alt "^1.1.6"
21472+
loader-utils "^1.2.3"
21473+
2146621474
lazy-property@~1.0.0:
2146721475
version "1.0.0"
2146821476
resolved "https://registry.yarnpkg.com/lazy-property/-/lazy-property-1.0.0.tgz#84ddc4b370679ba8bd4cdcfa4c06b43d57111147"

0 commit comments

Comments
 (0)