Skip to content

Commit

Permalink
feat: support vite v5 (#29518)
Browse files Browse the repository at this point in the history
* 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 <jennifer@cypress.io>
  • Loading branch information
AtofStryker and jennifer-shehane authored May 21, 2024
1 parent 3a3d877 commit 079030b
Show file tree
Hide file tree
Showing 30 changed files with 2,938 additions and 887 deletions.
11 changes: 6 additions & 5 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ mainBuildFilters: &mainBuildFilters
- /^release\/\d+\.\d+\.\d+$/
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- 'update-v8-snapshot-cache-on-develop'
- chore/fix_kitchen_sink
- 'publish-binary'
- 'feat/vite_5_support'

# usually we don't build Mac app - it takes a long time
# but sometimes we want to really confirm we are doing the right thing
Expand All @@ -41,7 +42,7 @@ macWorkflowFilters: &darwin-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'bump-electron-27.3.10', << pipeline.git.branch >> ]
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand All @@ -52,7 +53,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'bump-electron-27.3.10', << pipeline.git.branch >> ]
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand All @@ -75,7 +76,7 @@ windowsWorkflowFilters: &windows-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'chore/update_internal_browser_images', << pipeline.git.branch >> ]
- equal: [ 'feat/vite_5_support', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand Down Expand Up @@ -151,7 +152,7 @@ commands:
name: Set environment variable to determine whether or not to persist artifacts
command: |
echo "Setting SHOULD_PERSIST_ARTIFACTS variable"
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "bump-electron-27.3.10" ]]; then
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/vite_5_support" ]]; then
export SHOULD_PERSIST_ARTIFACTS=true
fi' >> "$BASH_ENV"
# You must run `setup_should_persist_artifacts` command and be using bash before running this command
Expand Down
6 changes: 5 additions & 1 deletion cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 13.9.1
## 13.10.0

_Released 5/21/2024 (PENDING)_

**Features:**

- Added support for `vite` `v5` to `@cypress/vite-dev-server`. Addresses [#28347](https://github.com/cypress-io/cypress/issues/28347).

**Bugfixes:**

- 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).
Expand Down
17 changes: 16 additions & 1 deletion npm/vite-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@ We then merge the sourced config with the user's vite config, and layer on our o
| <= v2 | <= v9 |
| >= v3 | >= v10 |

#### `devServerPublicPathRoute` for Vite v5

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:

```ts
import { defineConfig } from 'cypress'

export default defineConfig({
component: {
// If wanting a publicPath the same as the default in Vite 5
devServerPublicPathRoute: ''
}
})
```

## License

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

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

## [Changelog](./CHANGELOG.md)
## [Changelog](./CHANGELOG.md)
25 changes: 20 additions & 5 deletions npm/vite-dev-server/client/initCypressTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,45 @@ const supportFile = CypressInstance.config('supportFile')
const projectRoot = CypressInstance.config('projectRoot')
const devServerPublicPathRoute = CypressInstance.config('devServerPublicPathRoute')

let devServerPublicPathBase = devServerPublicPathRoute

// In the case the devServerPublicPathRoute is set to the root, make sure we configure the loaders correctly to load relative paths
// 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)
if (devServerPublicPathRoute === '') {
devServerPublicPathBase = '.'
}

if (supportFile) {
let supportRelativeToProjectRoot = supportFile.replace(projectRoot, '')

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

supportRelativeToProjectRoot = supportFile.replace(platformProjectRoot, '')

// Support relative path (as well as in some cases absolute path) lookup is done with unix style operators.
supportRelativeToProjectRoot = supportRelativeToProjectRoot.replaceAll('\\', '/')
}

// We need a slash before /cypress/supportFile.js, this happens by default
// with the current string replacement logic.
// We need a slash before /cypress/supportFile.js if the devServerPublicPathRoute is populated, this happens by default
// with the current string replacement logic. Otherwise, we need to specify the relative path to look up if devServerPublicPathRoute
// is not defined as it would be in the base directory

const relativeUrl = `${devServerPublicPathBase}${supportRelativeToProjectRoot}`

importsToLoad.push({
load: () => import(`${devServerPublicPathRoute}${supportRelativeToProjectRoot}`),
load: () => import(relativeUrl),
absolute: supportFile,
relative: supportRelativeToProjectRoot,
relativeUrl: `${devServerPublicPathRoute}${supportRelativeToProjectRoot}`,
relativeUrl,
})
}

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

/* Spec file import logic */
// We need a slash before /src/my-spec.js, this does not happen by default.
Expand Down
6 changes: 4 additions & 2 deletions npm/vite-dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
},
"devDependencies": {
"chai": "^4.3.6",
"decache": "^4.6.2",
"dedent": "^0.7.0",
"mocha": "^9.2.2",
"sinon": "^13.0.1",
"ts-node": "^10.9.2",
"vite": "4.5.2",
"vite-plugin-inspect": "0.7.24"
"vite-4": "npm:vite@^4.5.2",
"vite-5": "npm:vite@^5.2.8",
"vite-plugin-inspect": "0.8.4"
},
"files": [
"dist",
Expand Down
13 changes: 11 additions & 2 deletions npm/vite-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debugFn from 'debug'
import type { UserConfig } from 'vite'
import semverMajor from 'semver/functions/major'
import type { UserConfig } from 'vite-5'
import { getVite, Vite } from './getVite'
import { createViteDevServerConfig } from './resolveConfig'

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

type ConfigHandler = UserConfig | (() => UserConfig | Promise<UserConfig>)

export type ViteDevServerConfig = {
specs: Cypress.Spec[]
cypressConfig: Cypress.PluginConfigOptions
Expand All @@ -23,6 +23,15 @@ export async function devServer (config: ViteDevServerConfig): Promise<Cypress.R
// This has to be the first thing we do as we need to source vite from their project's dependencies
const vite = getVite(config)

let majorVersion: number | undefined = undefined

if (vite.version) {
majorVersion = semverMajor(vite.version)
debug(`Found vite version v${majorVersion}`)
} else {
debug(`vite version not found`)
}

debug('Creating Vite Server')
const server = await devServer.create(config, vite)

Expand Down
2 changes: 1 addition & 1 deletion npm/vite-dev-server/src/getVite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ViteDevServerConfig } from './devServer'

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

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

// "vite-dev-server" is bundled in the binary, so we need to require.resolve "vite"
// from root of the active project since we don't bundle vite internally but rather
Expand Down
4 changes: 2 additions & 2 deletions npm/vite-dev-server/src/plugins/cypress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debugFn from 'debug'
import type { ModuleNode, Plugin, ViteDevServer } from 'vite'
import type { ModuleNode, PluginOption, ViteDevServer } from 'vite-5'
import type { Vite } from '../getVite'
import { parse, HTMLElement } from 'node-html-parser'
import fs from 'fs'
Expand Down Expand Up @@ -27,7 +27,7 @@ function getSpecsPathsSet (specs: Spec[]) {
export const Cypress = (
options: ViteDevServerConfig,
vite: Vite,
): Plugin => {
): PluginOption => {
let base = '/'

const projectRoot = options.cypressConfig.projectRoot
Expand Down
4 changes: 2 additions & 2 deletions npm/vite-dev-server/src/plugins/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debugFn from 'debug'
import type { Plugin } from 'vite'
import type { PluginOption } from 'vite-5'
import type { Vite } from '../getVite'

import type { ViteDevServerConfig } from '../devServer'
Expand All @@ -9,7 +9,7 @@ const debug = debugFn('cypress:vite-dev-server:plugins:sourcemap')
export const CypressSourcemap = (
options: ViteDevServerConfig,
vite: Vite,
): Plugin => {
): PluginOption => {
return {
name: 'cypress:sourcemap',
enforce: 'post',
Expand Down
4 changes: 2 additions & 2 deletions npm/vite-dev-server/src/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* You can find it here https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/create.ts
*/
import debugFn from 'debug'
import type { InlineConfig } from 'vite'
import type { InlineConfig } from 'vite-5'
import path from 'path'
import semverGte from 'semver/functions/gte'

Expand Down Expand Up @@ -62,7 +62,7 @@ export const createViteDevServerConfig = async (config: ViteDevServerConfig, vit
return finalConfig
}

function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig {
function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig | InlineConfig {
const {
cypressConfig: {
port,
Expand Down
Loading

4 comments on commit 079030b

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 079030b May 21, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.10.0/linux-x64/develop-079030bb5f25b0983b9046d0f692e79790d10bcf/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 079030b May 21, 2024

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.10.0/linux-arm64/develop-079030bb5f25b0983b9046d0f692e79790d10bcf/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 079030b May 21, 2024

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.10.0/darwin-arm64/develop-079030bb5f25b0983b9046d0f692e79790d10bcf/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 079030b May 21, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.10.0/darwin-x64/develop-079030bb5f25b0983b9046d0f692e79790d10bcf/cypress.tgz

Please sign in to comment.