1
+ import * as crypto from 'crypto' ;
1
2
import * as path from 'path' ;
2
3
3
4
import { defineConfig } from 'cypress' ;
4
5
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin' ;
5
6
import type { Configuration } from 'webpack' ;
6
7
7
- /**
8
- * use this as base webpack config if you need to customize devServer webpack configuration
9
- */
8
+ const projectRoot = process . cwd ( ) ;
9
+
10
+ // Use a high port range unlikely to collide with other services: 20000-29999
11
+ const deterministicPort = 20000 + ( hashToInt ( projectRoot ) % 10000 ) ;
12
+
10
13
export const baseWebpackConfig : Configuration = {
11
14
resolve : {
12
15
extensions : [ '.js' , '.ts' , '.jsx' , '.tsx' ] ,
13
16
} ,
14
17
mode : 'development' ,
15
18
devtool : 'eval' ,
19
+ // Ensure parallel Cypress component runs don't collide on a fixed port (8080 is webpack-dev-server default).
20
+ // Pick a deterministic port per project (can be overridden) since some CI setups ignore 'auto'.
21
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
22
+ // @ts -ignore - devServer is provided by webpack-dev-server typings
23
+ devServer : {
24
+ port : process . env . WEBPACK_DEV_SERVER_PORT ? Number ( process . env . WEBPACK_DEV_SERVER_PORT ) : deterministicPort ,
25
+ host : '127.0.0.1' ,
26
+ } ,
16
27
output : {
17
28
publicPath : '/' ,
18
29
chunkFilename : '[name].bundle.js' ,
@@ -64,7 +75,6 @@ interface BaseConfig extends Cypress.ConfigOptions {
64
75
} ;
65
76
}
66
77
67
- const projectRoot = process . cwd ( ) ;
68
78
/**
69
79
* Programmatically create relative support support path, because Cypress bug
70
80
* @see https://github.com/cypress-io/cypress/issues/31819
@@ -85,7 +95,6 @@ export const baseConfig = defineConfig({
85
95
bundler : 'webpack' ,
86
96
webpackConfig : cypressWebpackConfig ( ) ,
87
97
} ,
88
-
89
98
supportFile : path . join ( projectSupportDir , './component.js' ) ,
90
99
indexHtmlFile : path . join ( projectSupportDir , './component-index.html' ) ,
91
100
} ,
@@ -98,3 +107,16 @@ export const baseConfig = defineConfig({
98
107
// screenshotOnRunFailure: isLocalRun && argv.mode === 'run',
99
108
fixturesFolder : path . join ( __dirname , './fixtures' ) ,
100
109
} ) as BaseConfig ;
110
+
111
+ /**
112
+ * use this as base webpack config if you need to customize devServer webpack configuration
113
+ *
114
+ * Generate a deterministic, project-scoped port to avoid collisions when multiple Cypress component
115
+ * test servers start in parallel on the same machine/agent. Allows override via WEBPACK_DEV_SERVER_PORT.
116
+ */
117
+ function hashToInt ( str : string ) {
118
+ // Use Node.js crypto module for better hashing
119
+ const hash = crypto . createHash ( 'sha256' ) . update ( str ) . digest ( 'hex' ) ;
120
+ // Convert first 8 hex characters to integer
121
+ return parseInt ( hash . slice ( 0 , 8 ) , 16 ) ;
122
+ }
0 commit comments