Skip to content

Commit

Permalink
fix: Properly typecheck webpack-dev-server and fix several undefined …
Browse files Browse the repository at this point in the history
…issues (#16503)
  • Loading branch information
agg23 authored May 14, 2021
1 parent f2ad12f commit 4bb1ecd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/src/aut-runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env browser */

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

if (!Cypress) {
Expand Down
4 changes: 2 additions & 2 deletions npm/webpack-dev-server/src/makeWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import * as webpack from 'webpack'
import { merge } from 'webpack-merge'
import makeDefaultWebpackConfig from './webpack.config'
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptions } from './plugin'
import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptionsWithEmitter } from './plugin'

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

Expand All @@ -17,7 +17,7 @@ export interface UserWebpackDevServerOptions {
disableLazyCompilation?: boolean
}

interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions, UserWebpackDevServerOptions {
interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptionsWithEmitter, UserWebpackDevServerOptions {
devServerPublicPathRoute: string
isOpenMode: boolean
template?: string
Expand Down
6 changes: 4 additions & 2 deletions npm/webpack-dev-server/src/measureWebpackPerformance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable no-console */
import * as webpack from 'webpack'
import type { Configuration } from 'webpack'
import path from 'path'
import fs from 'fs'
import chalk from 'chalk'

// @ts-ignore
import SpeedMeasurePlugin from 'speed-measure-webpack-plugin'

export function measureWebpackPerformance (webpackConfig: webpack.Configuration): webpack.Configuration {
export function measureWebpackPerformance (webpackConfig: Configuration): Configuration {
if (!process.env.WEBPACK_PERF_MEASURE) {
throw new Error('Performance monitoring is possible only with WEBPACK_PERF_MEASURE env variable set')
}
Expand Down
28 changes: 21 additions & 7 deletions npm/webpack-dev-server/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import _ from 'lodash'
import semver from 'semver'
import fs, { PathLike } from 'fs'
import path from 'path'
// eslint-disable-next-line no-duplicate-imports
import type { Compilation } from 'webpack'

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

Expand All @@ -14,10 +16,23 @@ export interface CypressCTOptionsPluginOptions {
devServerEvents?: EventEmitter
}

export type CypressCTOptionsPluginOptionsWithEmitter = CypressCTOptionsPluginOptions & {
devServerEvents: EventEmitter
}

export interface CypressCTWebpackContext {
_cypress: CypressCTOptionsPluginOptions
}

export type Webpack45Compilation = Compilation & {
// TODO: Drop these additional Webpack 4 types
inputFileSystem: {
fileSystem: {
utimesSync: UtimesSync
}
}
}

export default class CypressCTOptionsPlugin {
private files: Cypress.Cypress['spec'][] = []
private supportFile: string
Expand All @@ -26,7 +41,7 @@ export default class CypressCTOptionsPlugin {
private readonly projectRoot: string
private readonly devServerEvents: EventEmitter

constructor (options: CypressCTOptionsPluginOptions) {
constructor (options: CypressCTOptionsPluginOptionsWithEmitter) {
this.files = options.files
this.supportFile = options.supportFile
this.projectRoot = options.projectRoot
Expand All @@ -49,7 +64,7 @@ export default class CypressCTOptionsPlugin {

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

Expand All @@ -86,10 +101,9 @@ export default class CypressCTOptionsPlugin {
// Webpack 5
/* istanbul ignore next */
if ('NormalModule' in webpack) {
// @ts-ignore
webpack.NormalModule.getCompilationHooks(compilation).loader.tap(
'CypressCTOptionsPlugin',
this.pluginFunc,
(context) => this.pluginFunc(context as CypressCTWebpackContext),
)

return
Expand All @@ -98,12 +112,12 @@ export default class CypressCTOptionsPlugin {
// Webpack 4
compilation.hooks.normalModuleLoader.tap(
'CypressCTOptionsPlugin',
this.pluginFunc,
(context) => this.pluginFunc(context as CypressCTWebpackContext),
)
};

apply (compiler: Compiler): void {
this.setupCustomHMR(compiler)
compiler.hooks.compilation.tap('CypressCTOptionsPlugin', this.plugin)
compiler.hooks.compilation.tap('CypressCTOptionsPlugin', (compilation) => this.plugin(compilation as Webpack45Compilation))
}
}
4 changes: 2 additions & 2 deletions npm/webpack-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function start ({ webpackConfig: userWebpackConfig, template, optio

debug('starting webpack dev server')
let webpackDevServerConfig: WebpackDevServer.Configuration = {
...userWebpackConfig.devServer,
...userWebpackConfig?.devServer,
hot: false,
}

Expand All @@ -63,7 +63,7 @@ export async function start ({ webpackConfig: userWebpackConfig, template, optio
}
} else if (webpackDevServerPkg.version.match(/4\./)) {
webpackDevServerConfig = {
...userWebpackConfig.devServer,
...userWebpackConfig?.devServer,
devMiddleware: {
publicPath: devServerPublicPathRoute,
},
Expand Down
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

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

/* Module Resolution Options */
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7819,9 +7819,9 @@
source-map "^0.6.0"

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

4 comments on commit 4bb1ecd

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4bb1ecd May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.1/circle-develop-4bb1ecd077fc3724e6c127982f98e1e6b0f1bb98/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4bb1ecd May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.1/appveyor-develop-4bb1ecd077fc3724e6c127982f98e1e6b0f1bb98/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4bb1ecd May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.1/appveyor-develop-4bb1ecd077fc3724e6c127982f98e1e6b0f1bb98/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4bb1ecd May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.1/circle-develop-4bb1ecd077fc3724e6c127982f98e1e6b0f1bb98/cypress.tgz

Please sign in to comment.