Skip to content

Commit 079030b

Browse files
feat: support vite v5 (#29518)
* feat: support vite v5 [run ci] * make sure to use correct path in windows if no cypress public path is provided [run ci] * update snapshot [run ci] * update docs on devServerPublicPathRoute for vite-dev-server * update comments * refactor resolveConfig test --------- Co-authored-by: Jennifer Shehane <[email protected]>
1 parent 3a3d877 commit 079030b

File tree

30 files changed

+2938
-887
lines changed

30 files changed

+2938
-887
lines changed

.circleci/workflows.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ mainBuildFilters: &mainBuildFilters
3030
- /^release\/\d+\.\d+\.\d+$/
3131
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
3232
- 'update-v8-snapshot-cache-on-develop'
33-
- chore/fix_kitchen_sink
33+
- 'publish-binary'
34+
- 'feat/vite_5_support'
3435

3536
# usually we don't build Mac app - it takes a long time
3637
# but sometimes we want to really confirm we are doing the right thing
@@ -41,7 +42,7 @@ macWorkflowFilters: &darwin-workflow-filters
4142
- equal: [ develop, << pipeline.git.branch >> ]
4243
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
4344
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
44-
- equal: [ 'bump-electron-27.3.10', << pipeline.git.branch >> ]
45+
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
4546
- matches:
4647
pattern: /^release\/\d+\.\d+\.\d+$/
4748
value: << pipeline.git.branch >>
@@ -52,7 +53,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters
5253
- equal: [ develop, << pipeline.git.branch >> ]
5354
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
5455
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
55-
- equal: [ 'bump-electron-27.3.10', << pipeline.git.branch >> ]
56+
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
5657
- matches:
5758
pattern: /^release\/\d+\.\d+\.\d+$/
5859
value: << pipeline.git.branch >>
@@ -75,7 +76,7 @@ windowsWorkflowFilters: &windows-workflow-filters
7576
- equal: [ develop, << pipeline.git.branch >> ]
7677
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
7778
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
78-
- equal: [ 'chore/update_internal_browser_images', << pipeline.git.branch >> ]
79+
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
7980
- matches:
8081
pattern: /^release\/\d+\.\d+\.\d+$/
8182
value: << pipeline.git.branch >>
@@ -151,7 +152,7 @@ commands:
151152
name: Set environment variable to determine whether or not to persist artifacts
152153
command: |
153154
echo "Setting SHOULD_PERSIST_ARTIFACTS variable"
154-
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "bump-electron-27.3.10" ]]; then
155+
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/vite_5_support" ]]; then
155156
export SHOULD_PERSIST_ARTIFACTS=true
156157
fi' >> "$BASH_ENV"
157158
# You must run `setup_should_persist_artifacts` command and be using bash before running this command

cli/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
2-
## 13.9.1
2+
## 13.10.0
33

44
_Released 5/21/2024 (PENDING)_
55

6+
**Features:**
7+
8+
- Added support for `vite` `v5` to `@cypress/vite-dev-server`. Addresses [#28347](https://github.com/cypress-io/cypress/issues/28347).
9+
610
**Bugfixes:**
711

812
- Fixed an issue where orphaned Electron processes were inadvertently terminating the browser's CRI client. Fixes [#28397](https://github.com/cypress-io/cypress/issues/28397). Fixed in [#29515](https://github.com/cypress-io/cypress/pull/29515).

npm/vite-dev-server/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,25 @@ We then merge the sourced config with the user's vite config, and layer on our o
5656
| <= v2 | <= v9 |
5757
| >= v3 | >= v10 |
5858

59+
#### `devServerPublicPathRoute` for Vite v5
60+
61+
If using Vite version 5, setting `devServerPublicPathRoute` may be needed if directly referencing public path url assets in components under test. This is due to Cypress using its own public path, `/__cypress/src`, when running component tests. This can be configured within the `component` namespace below if you wish you set your public path to be the same as your app:
62+
63+
```ts
64+
import { defineConfig } from 'cypress'
65+
66+
export default defineConfig({
67+
component: {
68+
// If wanting a publicPath the same as the default in Vite 5
69+
devServerPublicPathRoute: ''
70+
}
71+
})
72+
```
73+
5974
## License
6075

6176
[![license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/cypress-io/cypress/blob/develop/LICENSE)
6277

6378
This project is licensed under the terms of the [MIT license](/LICENSE).
6479

65-
## [Changelog](./CHANGELOG.md)
80+
## [Changelog](./CHANGELOG.md)

npm/vite-dev-server/client/initCypressTests.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,45 @@ const supportFile = CypressInstance.config('supportFile')
1313
const projectRoot = CypressInstance.config('projectRoot')
1414
const devServerPublicPathRoute = CypressInstance.config('devServerPublicPathRoute')
1515

16+
let devServerPublicPathBase = devServerPublicPathRoute
17+
18+
// In the case the devServerPublicPathRoute is set to the root, make sure we configure the loaders correctly to load relative paths
19+
// This can be a case in vite 5 if a user wishes to have the same public path as their app (which is quite common)
20+
if (devServerPublicPathRoute === '') {
21+
devServerPublicPathBase = '.'
22+
}
23+
1624
if (supportFile) {
1725
let supportRelativeToProjectRoot = supportFile.replace(projectRoot, '')
1826

1927
if (CypressInstance.config('platform') === 'win32') {
2028
const platformProjectRoot = projectRoot.replaceAll('/', '\\')
2129

2230
supportRelativeToProjectRoot = supportFile.replace(platformProjectRoot, '')
31+
32+
// Support relative path (as well as in some cases absolute path) lookup is done with unix style operators.
33+
supportRelativeToProjectRoot = supportRelativeToProjectRoot.replaceAll('\\', '/')
2334
}
2435

25-
// We need a slash before /cypress/supportFile.js, this happens by default
26-
// with the current string replacement logic.
36+
// We need a slash before /cypress/supportFile.js if the devServerPublicPathRoute is populated, this happens by default
37+
// with the current string replacement logic. Otherwise, we need to specify the relative path to look up if devServerPublicPathRoute
38+
// is not defined as it would be in the base directory
39+
40+
const relativeUrl = `${devServerPublicPathBase}${supportRelativeToProjectRoot}`
41+
2742
importsToLoad.push({
28-
load: () => import(`${devServerPublicPathRoute}${supportRelativeToProjectRoot}`),
43+
load: () => import(relativeUrl),
2944
absolute: supportFile,
3045
relative: supportRelativeToProjectRoot,
31-
relativeUrl: `${devServerPublicPathRoute}${supportRelativeToProjectRoot}`,
46+
relativeUrl,
3247
})
3348
}
3449

3550
// Using relative path wouldn't allow to load tests outside Vite project root folder
3651
// So we use the "@fs" bit to load the test file using its absolute path
3752
// Normalize path to not include a leading slash (different on Win32 vs Unix)
3853
const normalizedAbsolutePath = CypressInstance.spec.absolute.replace(/^\//, '')
39-
const testFileAbsolutePathRoute = `${devServerPublicPathRoute}/@fs/${normalizedAbsolutePath}`
54+
const testFileAbsolutePathRoute = `${devServerPublicPathBase}/@fs/${normalizedAbsolutePath}`
4055

4156
/* Spec file import logic */
4257
// We need a slash before /src/my-spec.js, this does not happen by default.

npm/vite-dev-server/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
},
2323
"devDependencies": {
2424
"chai": "^4.3.6",
25+
"decache": "^4.6.2",
2526
"dedent": "^0.7.0",
2627
"mocha": "^9.2.2",
2728
"sinon": "^13.0.1",
2829
"ts-node": "^10.9.2",
29-
"vite": "4.5.2",
30-
"vite-plugin-inspect": "0.7.24"
30+
"vite-4": "npm:vite@^4.5.2",
31+
"vite-5": "npm:vite@^5.2.8",
32+
"vite-plugin-inspect": "0.8.4"
3133
},
3234
"files": [
3335
"dist",

npm/vite-dev-server/src/devServer.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import debugFn from 'debug'
2-
import type { UserConfig } from 'vite'
2+
import semverMajor from 'semver/functions/major'
3+
import type { UserConfig } from 'vite-5'
34
import { getVite, Vite } from './getVite'
45
import { createViteDevServerConfig } from './resolveConfig'
56

@@ -8,7 +9,6 @@ const debug = debugFn('cypress:vite-dev-server:devServer')
89
const ALL_FRAMEWORKS = ['react', 'vue'] as const
910

1011
type ConfigHandler = UserConfig | (() => UserConfig | Promise<UserConfig>)
11-
1212
export type ViteDevServerConfig = {
1313
specs: Cypress.Spec[]
1414
cypressConfig: Cypress.PluginConfigOptions
@@ -23,6 +23,15 @@ export async function devServer (config: ViteDevServerConfig): Promise<Cypress.R
2323
// This has to be the first thing we do as we need to source vite from their project's dependencies
2424
const vite = getVite(config)
2525

26+
let majorVersion: number | undefined = undefined
27+
28+
if (vite.version) {
29+
majorVersion = semverMajor(vite.version)
30+
debug(`Found vite version v${majorVersion}`)
31+
} else {
32+
debug(`vite version not found`)
33+
}
34+
2635
debug('Creating Vite Server')
2736
const server = await devServer.create(config, vite)
2837

npm/vite-dev-server/src/getVite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ViteDevServerConfig } from './devServer'
33

44
const debug = debugFn('cypress:vite-dev-server:getVite')
55

6-
export type Vite = typeof import('vite')
6+
export type Vite = typeof import('vite-5')
77

88
// "vite-dev-server" is bundled in the binary, so we need to require.resolve "vite"
99
// from root of the active project since we don't bundle vite internally but rather

npm/vite-dev-server/src/plugins/cypress.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import debugFn from 'debug'
2-
import type { ModuleNode, Plugin, ViteDevServer } from 'vite'
2+
import type { ModuleNode, PluginOption, ViteDevServer } from 'vite-5'
33
import type { Vite } from '../getVite'
44
import { parse, HTMLElement } from 'node-html-parser'
55
import fs from 'fs'
@@ -27,7 +27,7 @@ function getSpecsPathsSet (specs: Spec[]) {
2727
export const Cypress = (
2828
options: ViteDevServerConfig,
2929
vite: Vite,
30-
): Plugin => {
30+
): PluginOption => {
3131
let base = '/'
3232

3333
const projectRoot = options.cypressConfig.projectRoot

npm/vite-dev-server/src/plugins/sourcemap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import debugFn from 'debug'
2-
import type { Plugin } from 'vite'
2+
import type { PluginOption } from 'vite-5'
33
import type { Vite } from '../getVite'
44

55
import type { ViteDevServerConfig } from '../devServer'
@@ -9,7 +9,7 @@ const debug = debugFn('cypress:vite-dev-server:plugins:sourcemap')
99
export const CypressSourcemap = (
1010
options: ViteDevServerConfig,
1111
vite: Vite,
12-
): Plugin => {
12+
): PluginOption => {
1313
return {
1414
name: 'cypress:sourcemap',
1515
enforce: 'post',

npm/vite-dev-server/src/resolveConfig.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* You can find it here https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/create.ts
55
*/
66
import debugFn from 'debug'
7-
import type { InlineConfig } from 'vite'
7+
import type { InlineConfig } from 'vite-5'
88
import path from 'path'
99
import semverGte from 'semver/functions/gte'
1010

@@ -62,7 +62,7 @@ export const createViteDevServerConfig = async (config: ViteDevServerConfig, vit
6262
return finalConfig
6363
}
6464

65-
function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig {
65+
function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig | InlineConfig {
6666
const {
6767
cypressConfig: {
6868
port,

0 commit comments

Comments
 (0)