Skip to content

Commit 4bb1ecd

Browse files
authored
fix: Properly typecheck webpack-dev-server and fix several undefined issues (#16503)
1 parent f2ad12f commit 4bb1ecd

File tree

7 files changed

+34
-18
lines changed

7 files changed

+34
-18
lines changed

npm/webpack-dev-server/src/aut-runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env browser */
22

3-
export function init (importPromises, parent: Window = (window.opener || window.parent)) {
3+
export function init (importPromises: Array<() => Promise<void>>, parent: Window = (window.opener || window.parent)) {
44
const Cypress = window.Cypress = parent.Cypress
55

66
if (!Cypress) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path'
33
import * as webpack from 'webpack'
44
import { merge } from 'webpack-merge'
55
import makeDefaultWebpackConfig from './webpack.config'
6-
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptions } from './plugin'
6+
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptionsWithEmitter } from './plugin'
77

88
const debug = debugFn('cypress:webpack-dev-server:makeWebpackConfig')
99

@@ -17,7 +17,7 @@ export interface UserWebpackDevServerOptions {
1717
disableLazyCompilation?: boolean
1818
}
1919

20-
interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions, UserWebpackDevServerOptions {
20+
interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptionsWithEmitter, UserWebpackDevServerOptions {
2121
devServerPublicPathRoute: string
2222
isOpenMode: boolean
2323
template?: string

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable no-console */
2-
import * as webpack from 'webpack'
2+
import type { Configuration } from 'webpack'
33
import path from 'path'
44
import fs from 'fs'
55
import chalk from 'chalk'
6+
7+
// @ts-ignore
68
import SpeedMeasurePlugin from 'speed-measure-webpack-plugin'
79

8-
export function measureWebpackPerformance (webpackConfig: webpack.Configuration): webpack.Configuration {
10+
export function measureWebpackPerformance (webpackConfig: Configuration): Configuration {
911
if (!process.env.WEBPACK_PERF_MEASURE) {
1012
throw new Error('Performance monitoring is possible only with WEBPACK_PERF_MEASURE env variable set')
1113
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import _ from 'lodash'
44
import semver from 'semver'
55
import fs, { PathLike } from 'fs'
66
import path from 'path'
7+
// eslint-disable-next-line no-duplicate-imports
8+
import type { Compilation } from 'webpack'
79

810
type UtimesSync = (path: PathLike, atime: string | number | Date, mtime: string | number | Date) => void
911

@@ -14,10 +16,23 @@ export interface CypressCTOptionsPluginOptions {
1416
devServerEvents?: EventEmitter
1517
}
1618

19+
export type CypressCTOptionsPluginOptionsWithEmitter = CypressCTOptionsPluginOptions & {
20+
devServerEvents: EventEmitter
21+
}
22+
1723
export interface CypressCTWebpackContext {
1824
_cypress: CypressCTOptionsPluginOptions
1925
}
2026

27+
export type Webpack45Compilation = Compilation & {
28+
// TODO: Drop these additional Webpack 4 types
29+
inputFileSystem: {
30+
fileSystem: {
31+
utimesSync: UtimesSync
32+
}
33+
}
34+
}
35+
2136
export default class CypressCTOptionsPlugin {
2237
private files: Cypress.Cypress['spec'][] = []
2338
private supportFile: string
@@ -26,7 +41,7 @@ export default class CypressCTOptionsPlugin {
2641
private readonly projectRoot: string
2742
private readonly devServerEvents: EventEmitter
2843

29-
constructor (options: CypressCTOptionsPluginOptions) {
44+
constructor (options: CypressCTOptionsPluginOptionsWithEmitter) {
3045
this.files = options.files
3146
this.supportFile = options.supportFile
3247
this.projectRoot = options.projectRoot
@@ -49,7 +64,7 @@ export default class CypressCTOptionsPlugin {
4964

5065
if (stats.hasErrors()) {
5166
this.errorEmitted = true
52-
this.devServerEvents.emit('dev-server:compile:error', stats.toJson().errors[0])
67+
this.devServerEvents.emit('dev-server:compile:error', stats.toJson().errors?.[0])
5368
} else if (this.errorEmitted) {
5469
// compilation succeed but assets haven't emitted to the output yet
5570
this.devServerEvents.emit('dev-server:compile:error', null)
@@ -72,7 +87,7 @@ export default class CypressCTOptionsPlugin {
7287
* @param compilation webpack 4 `compilation.Compilation`, webpack 5
7388
* `Compilation`
7489
*/
75-
private plugin = (compilation) => {
90+
private plugin = (compilation: Webpack45Compilation) => {
7691
this.devServerEvents.on('dev-server:specs:changed', (specs) => {
7792
if (_.isEqual(specs, this.files)) return
7893

@@ -86,10 +101,9 @@ export default class CypressCTOptionsPlugin {
86101
// Webpack 5
87102
/* istanbul ignore next */
88103
if ('NormalModule' in webpack) {
89-
// @ts-ignore
90104
webpack.NormalModule.getCompilationHooks(compilation).loader.tap(
91105
'CypressCTOptionsPlugin',
92-
this.pluginFunc,
106+
(context) => this.pluginFunc(context as CypressCTWebpackContext),
93107
)
94108

95109
return
@@ -98,12 +112,12 @@ export default class CypressCTOptionsPlugin {
98112
// Webpack 4
99113
compilation.hooks.normalModuleLoader.tap(
100114
'CypressCTOptionsPlugin',
101-
this.pluginFunc,
115+
(context) => this.pluginFunc(context as CypressCTWebpackContext),
102116
)
103117
};
104118

105119
apply (compiler: Compiler): void {
106120
this.setupCustomHMR(compiler)
107-
compiler.hooks.compilation.tap('CypressCTOptionsPlugin', this.plugin)
121+
compiler.hooks.compilation.tap('CypressCTOptionsPlugin', (compilation) => this.plugin(compilation as Webpack45Compilation))
108122
}
109123
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function start ({ webpackConfig: userWebpackConfig, template, optio
5050

5151
debug('starting webpack dev server')
5252
let webpackDevServerConfig: WebpackDevServer.Configuration = {
53-
...userWebpackConfig.devServer,
53+
...userWebpackConfig?.devServer,
5454
hot: false,
5555
}
5656

@@ -63,7 +63,7 @@ export async function start ({ webpackConfig: userWebpackConfig, template, optio
6363
}
6464
} else if (webpackDevServerPkg.version.match(/4\./)) {
6565
webpackDevServerConfig = {
66-
...userWebpackConfig.devServer,
66+
...userWebpackConfig?.devServer,
6767
devMiddleware: {
6868
publicPath: devServerPublicPathRoute,
6969
},

npm/webpack-dev-server/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
2525

2626
/* Strict Type-Checking Options */
27-
"strict": false /* Enable all strict type-checking options. */,
27+
"strict": true /* Enable all strict type-checking options. */,
2828
// "noImplicitAny": true,
2929

3030
/* Module Resolution Options */

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7819,9 +7819,9 @@
78197819
source-map "^0.6.0"
78207820

78217821
"@types/webpack@^4.4.31", "@types/webpack@^4.41.21", "@types/webpack@^4.41.26", "@types/webpack@^4.41.8":
7822-
version "4.41.27"
7823-
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.27.tgz#f47da488c8037e7f1b2dbf2714fbbacb61ec0ffc"
7824-
integrity sha512-wK/oi5gcHi72VMTbOaQ70VcDxSQ1uX8S2tukBK9ARuGXrYM/+u4ou73roc7trXDNmCxCoerE8zruQqX/wuHszA==
7822+
version "4.41.28"
7823+
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.28.tgz#0069a2159b7ad4d83d0b5801942c17d54133897b"
7824+
integrity sha512-Nn84RAiJjKRfPFFCVR8LC4ueTtTdfWAMZ03THIzZWRJB+rX24BD3LqPSFnbMscWauEsT4segAsylPDIaZyZyLQ==
78257825
dependencies:
78267826
"@types/anymatch" "*"
78277827
"@types/node" "*"

0 commit comments

Comments
 (0)