From 8c7a2cbdd1848d8d31c940767415de44a3b8407a Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Sun, 7 Jul 2024 19:40:43 +0200 Subject: [PATCH 1/6] add test --- .../__tests__/proxy-hmr-same-server.spec.ts | 25 ++++++++ playground/proxy-hmr-same-server/index.html | 2 + .../other-app/create-vite-config.js | 6 ++ .../other-app/index.html | 1 + .../other-app/package.json | 11 ++++ playground/proxy-hmr-same-server/package.json | 11 ++++ .../proxy-hmr-same-server/vite.config.js | 57 +++++++++++++++++++ pnpm-lock.yaml | 4 ++ 8 files changed, 117 insertions(+) create mode 100644 playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts create mode 100644 playground/proxy-hmr-same-server/index.html create mode 100644 playground/proxy-hmr-same-server/other-app/create-vite-config.js create mode 100644 playground/proxy-hmr-same-server/other-app/index.html create mode 100644 playground/proxy-hmr-same-server/other-app/package.json create mode 100644 playground/proxy-hmr-same-server/package.json create mode 100644 playground/proxy-hmr-same-server/vite.config.js diff --git a/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts b/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts new file mode 100644 index 00000000000000..ebdf561409b95d --- /dev/null +++ b/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts @@ -0,0 +1,25 @@ +import { test } from 'vitest' +import { + editFile, + page, + untilBrowserLogAfter, + untilUpdated, + viteTestUrl, +} from '~utils' + +test('proxy-hmr-same-server', async () => { + await untilBrowserLogAfter( + () => page.goto(viteTestUrl), + // wait for both main and sub app HMR connection + [/connected/, /connected/], + ) + const otherAppTextLocator = page.frameLocator('iframe').locator('.content') + await untilUpdated(() => otherAppTextLocator.textContent(), 'other app') + editFile('other-app/index.html', (code) => + code.replace('app', 'modified app'), + ) + await untilUpdated( + () => otherAppTextLocator.textContent(), + 'other modified app', + ) +}) diff --git a/playground/proxy-hmr-same-server/index.html b/playground/proxy-hmr-same-server/index.html new file mode 100644 index 00000000000000..f14fde8e428635 --- /dev/null +++ b/playground/proxy-hmr-same-server/index.html @@ -0,0 +1,2 @@ +root app
+ diff --git a/playground/proxy-hmr-same-server/other-app/create-vite-config.js b/playground/proxy-hmr-same-server/other-app/create-vite-config.js new file mode 100644 index 00000000000000..c3e7734619555c --- /dev/null +++ b/playground/proxy-hmr-same-server/other-app/create-vite-config.js @@ -0,0 +1,6 @@ +export default function () { + const root = __dirname + return { + root, + } +} diff --git a/playground/proxy-hmr-same-server/other-app/index.html b/playground/proxy-hmr-same-server/other-app/index.html new file mode 100644 index 00000000000000..18f42b0b93d11c --- /dev/null +++ b/playground/proxy-hmr-same-server/other-app/index.html @@ -0,0 +1 @@ +other app diff --git a/playground/proxy-hmr-same-server/other-app/package.json b/playground/proxy-hmr-same-server/other-app/package.json new file mode 100644 index 00000000000000..4457d4da6ae217 --- /dev/null +++ b/playground/proxy-hmr-same-server/other-app/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitejs/test-other-app", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + } +} diff --git a/playground/proxy-hmr-same-server/package.json b/playground/proxy-hmr-same-server/package.json new file mode 100644 index 00000000000000..e2a6443447b520 --- /dev/null +++ b/playground/proxy-hmr-same-server/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitejs/test-proxy-hmr-same-server", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + } +} diff --git a/playground/proxy-hmr-same-server/vite.config.js b/playground/proxy-hmr-same-server/vite.config.js new file mode 100644 index 00000000000000..018e73828c1eea --- /dev/null +++ b/playground/proxy-hmr-same-server/vite.config.js @@ -0,0 +1,57 @@ +import { defineConfig, createServer } from 'vite' +import createOtherAppViteConfig from './other-app/create-vite-config' + +const port = 9616 + +/** + * @type {() => import('vite').PluginOption} + * @returns + */ +function addOtherApp() { + return { + name: 'vite-plugin-add-other-app', + async configureServer(viteDevServer) { + const otherAppConfig = createOtherAppViteConfig() + + /** + * @type {import('vite').InlineConfig['server']['middlewareMode']} + */ + const middlewareMode = { + server: viteDevServer.httpServer, + } + + /** + * @type {import('vite').InlineConfig['server']['hmr']} + */ + const hmr = { + port, + server: viteDevServer.httpServer, + } + + const extendedOtherAppConfig = { + ...otherAppConfig, + base: '/anotherApp', + server: { + ...(otherAppConfig.server || {}), + middlewareMode, + hmr, + }, + } + const otherAppServer = await createServer(extendedOtherAppConfig) + viteDevServer.httpServer.on('close', () => { + otherAppServer.close() + }) + viteDevServer.middlewares.use('/anotherApp', otherAppServer.middlewares) + }, + } +} + +export default defineConfig(() => { + return { + server: { + port, + strictPort: true, + }, + plugins: [addOtherApp()], + } +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 80823d55fcf871..eae920bec80947 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1126,6 +1126,10 @@ importers: playground/proxy-hmr: {} + playground/proxy-hmr-same-server: {} + + playground/proxy-hmr-same-server/other-app: {} + playground/proxy-hmr/other-app: {} playground/resolve: From 2af315b2a6b191f87133d3e2dc02462f1b42ff8c Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Sun, 7 Jul 2024 19:41:02 +0200 Subject: [PATCH 2/6] make sure test has no type warnings --- packages/vite/src/node/server/hmr.ts | 4 ++-- packages/vite/src/node/server/index.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 040495673e8836..0c659a1d507e20 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -1,6 +1,5 @@ import fsp from 'node:fs/promises' import path from 'node:path' -import type { Server } from 'node:http' import { EventEmitter } from 'node:events' import colors from 'picocolors' import type { CustomPayload, HMRPayload, Update } from 'types/hmrPayload' @@ -14,6 +13,7 @@ import { isExplicitImportRequired } from '../plugins/importAnalysis' import { getEnvFilesForMode } from '../env' import { withTrailingSlash, wrapId } from '../../shared/utils' import type { ModuleNode } from './moduleGraph' +import type { HttpServer } from '.' import { restartServerWithUrls } from '.' export const debugHmr = createDebugger('vite:hmr') @@ -30,7 +30,7 @@ export interface HmrOptions { path?: string timeout?: number overlay?: boolean - server?: Server + server?: HttpServer /** @internal */ channels?: HMRChannel[] } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index de4f75ad42d246..b5e1d9c57e5f19 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -139,7 +139,7 @@ export interface ServerOptions extends CommonServerOptions { * * This is needed to proxy WebSocket connections to the parent server. */ - server: http.Server + server: HttpServer } /** * Options for files served via '/\@fs/'. From 7160fd6c23e3d2c842538687c8fa0e6c8d82647c Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Sun, 7 Jul 2024 21:08:16 +0200 Subject: [PATCH 3/6] fix one linting issue --- playground/proxy-hmr-same-server/vite.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/proxy-hmr-same-server/vite.config.js b/playground/proxy-hmr-same-server/vite.config.js index 018e73828c1eea..9a86a926666d51 100644 --- a/playground/proxy-hmr-same-server/vite.config.js +++ b/playground/proxy-hmr-same-server/vite.config.js @@ -1,4 +1,4 @@ -import { defineConfig, createServer } from 'vite' +import { createServer, defineConfig } from 'vite' import createOtherAppViteConfig from './other-app/create-vite-config' const port = 9616 From 35cc27bcddeac52297d4631c892cd03d1cc78b78 Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Sun, 7 Jul 2024 21:21:43 +0200 Subject: [PATCH 4/6] fix test by adding custom serve --- .../proxy-hmr-same-server/__tests__/serve.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 playground/proxy-hmr-same-server/__tests__/serve.ts diff --git a/playground/proxy-hmr-same-server/__tests__/serve.ts b/playground/proxy-hmr-same-server/__tests__/serve.ts new file mode 100644 index 00000000000000..0ad784c5ee60b5 --- /dev/null +++ b/playground/proxy-hmr-same-server/__tests__/serve.ts @@ -0,0 +1,22 @@ +// this is automatically detected by playground/vitestSetup.ts and will replace +// the default e2e test serve behavior + +import { rootDir, setViteUrl } from '~utils' + +export async function serve(): Promise<{ close(): Promise }> { + const vite = await import('vite') + const rootServer = await vite.createServer({ + root: rootDir, + logLevel: 'silent', + }) + + await rootServer.listen() + const viteUrl = rootServer.resolvedUrls.local[0] + setViteUrl(viteUrl) + + return { + async close() { + await rootServer.close() + }, + } +} From 1d37cc7480f0cc640322bc78c4193ad07129e39f Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Mon, 8 Jul 2024 18:33:25 +0200 Subject: [PATCH 5/6] export less restrictive server type as well --- packages/vite/src/node/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 3b84c34b0626a8..3d17f8737379d6 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -47,6 +47,7 @@ export type { ServerHook, ResolvedServerOptions, ResolvedServerUrls, + HttpServer, } from './server' export type { BuildOptions, From 768fcd32815e2c52f1d819c295ad936c56abe02f Mon Sep 17 00:00:00 2001 From: Emile Fokkema Date: Tue, 16 Jul 2024 11:05:12 +0000 Subject: [PATCH 6/6] revert adding of the test --- .../__tests__/proxy-hmr-same-server.spec.ts | 25 -------- .../proxy-hmr-same-server/__tests__/serve.ts | 22 ------- playground/proxy-hmr-same-server/index.html | 2 - .../other-app/create-vite-config.js | 6 -- .../other-app/index.html | 1 - .../other-app/package.json | 11 ---- playground/proxy-hmr-same-server/package.json | 11 ---- .../proxy-hmr-same-server/vite.config.js | 57 ------------------- pnpm-lock.yaml | 4 -- 9 files changed, 139 deletions(-) delete mode 100644 playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts delete mode 100644 playground/proxy-hmr-same-server/__tests__/serve.ts delete mode 100644 playground/proxy-hmr-same-server/index.html delete mode 100644 playground/proxy-hmr-same-server/other-app/create-vite-config.js delete mode 100644 playground/proxy-hmr-same-server/other-app/index.html delete mode 100644 playground/proxy-hmr-same-server/other-app/package.json delete mode 100644 playground/proxy-hmr-same-server/package.json delete mode 100644 playground/proxy-hmr-same-server/vite.config.js diff --git a/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts b/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts deleted file mode 100644 index ebdf561409b95d..00000000000000 --- a/playground/proxy-hmr-same-server/__tests__/proxy-hmr-same-server.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { test } from 'vitest' -import { - editFile, - page, - untilBrowserLogAfter, - untilUpdated, - viteTestUrl, -} from '~utils' - -test('proxy-hmr-same-server', async () => { - await untilBrowserLogAfter( - () => page.goto(viteTestUrl), - // wait for both main and sub app HMR connection - [/connected/, /connected/], - ) - const otherAppTextLocator = page.frameLocator('iframe').locator('.content') - await untilUpdated(() => otherAppTextLocator.textContent(), 'other app') - editFile('other-app/index.html', (code) => - code.replace('app', 'modified app'), - ) - await untilUpdated( - () => otherAppTextLocator.textContent(), - 'other modified app', - ) -}) diff --git a/playground/proxy-hmr-same-server/__tests__/serve.ts b/playground/proxy-hmr-same-server/__tests__/serve.ts deleted file mode 100644 index 0ad784c5ee60b5..00000000000000 --- a/playground/proxy-hmr-same-server/__tests__/serve.ts +++ /dev/null @@ -1,22 +0,0 @@ -// this is automatically detected by playground/vitestSetup.ts and will replace -// the default e2e test serve behavior - -import { rootDir, setViteUrl } from '~utils' - -export async function serve(): Promise<{ close(): Promise }> { - const vite = await import('vite') - const rootServer = await vite.createServer({ - root: rootDir, - logLevel: 'silent', - }) - - await rootServer.listen() - const viteUrl = rootServer.resolvedUrls.local[0] - setViteUrl(viteUrl) - - return { - async close() { - await rootServer.close() - }, - } -} diff --git a/playground/proxy-hmr-same-server/index.html b/playground/proxy-hmr-same-server/index.html deleted file mode 100644 index f14fde8e428635..00000000000000 --- a/playground/proxy-hmr-same-server/index.html +++ /dev/null @@ -1,2 +0,0 @@ -root app
- diff --git a/playground/proxy-hmr-same-server/other-app/create-vite-config.js b/playground/proxy-hmr-same-server/other-app/create-vite-config.js deleted file mode 100644 index c3e7734619555c..00000000000000 --- a/playground/proxy-hmr-same-server/other-app/create-vite-config.js +++ /dev/null @@ -1,6 +0,0 @@ -export default function () { - const root = __dirname - return { - root, - } -} diff --git a/playground/proxy-hmr-same-server/other-app/index.html b/playground/proxy-hmr-same-server/other-app/index.html deleted file mode 100644 index 18f42b0b93d11c..00000000000000 --- a/playground/proxy-hmr-same-server/other-app/index.html +++ /dev/null @@ -1 +0,0 @@ -other app diff --git a/playground/proxy-hmr-same-server/other-app/package.json b/playground/proxy-hmr-same-server/other-app/package.json deleted file mode 100644 index 4457d4da6ae217..00000000000000 --- a/playground/proxy-hmr-same-server/other-app/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "@vitejs/test-other-app", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "dev": "vite", - "build": "vite build", - "preview": "vite preview" - } -} diff --git a/playground/proxy-hmr-same-server/package.json b/playground/proxy-hmr-same-server/package.json deleted file mode 100644 index e2a6443447b520..00000000000000 --- a/playground/proxy-hmr-same-server/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "@vitejs/test-proxy-hmr-same-server", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "dev": "vite", - "build": "vite build", - "preview": "vite preview" - } -} diff --git a/playground/proxy-hmr-same-server/vite.config.js b/playground/proxy-hmr-same-server/vite.config.js deleted file mode 100644 index 9a86a926666d51..00000000000000 --- a/playground/proxy-hmr-same-server/vite.config.js +++ /dev/null @@ -1,57 +0,0 @@ -import { createServer, defineConfig } from 'vite' -import createOtherAppViteConfig from './other-app/create-vite-config' - -const port = 9616 - -/** - * @type {() => import('vite').PluginOption} - * @returns - */ -function addOtherApp() { - return { - name: 'vite-plugin-add-other-app', - async configureServer(viteDevServer) { - const otherAppConfig = createOtherAppViteConfig() - - /** - * @type {import('vite').InlineConfig['server']['middlewareMode']} - */ - const middlewareMode = { - server: viteDevServer.httpServer, - } - - /** - * @type {import('vite').InlineConfig['server']['hmr']} - */ - const hmr = { - port, - server: viteDevServer.httpServer, - } - - const extendedOtherAppConfig = { - ...otherAppConfig, - base: '/anotherApp', - server: { - ...(otherAppConfig.server || {}), - middlewareMode, - hmr, - }, - } - const otherAppServer = await createServer(extendedOtherAppConfig) - viteDevServer.httpServer.on('close', () => { - otherAppServer.close() - }) - viteDevServer.middlewares.use('/anotherApp', otherAppServer.middlewares) - }, - } -} - -export default defineConfig(() => { - return { - server: { - port, - strictPort: true, - }, - plugins: [addOtherApp()], - } -}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b990df8640d7e4..ffbdfe2701c301 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1126,10 +1126,6 @@ importers: playground/proxy-hmr: {} - playground/proxy-hmr-same-server: {} - - playground/proxy-hmr-same-server/other-app: {} - playground/proxy-hmr/other-app: {} playground/resolve: