Skip to content

Commit c78b553

Browse files
committed
chore: apply vitejs#18713
1 parent 07ad00f commit c78b553

File tree

10 files changed

+54
-19
lines changed

10 files changed

+54
-19
lines changed

packages/vite/src/module-runner/hmrHandler.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { HotPayload } from 'types/hmrPayload'
22
import { slash, unwrapId } from '../shared/utils'
3+
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../shared/constants'
34
import type { ModuleRunner } from './runner'
45

56
// updates to HMR should go one after another. It is possible to trigger another update during the invalidation for example.
@@ -56,7 +57,15 @@ export async function handleHotPayload(
5657
runner.evaluatedModules.clear()
5758

5859
for (const url of clearEntrypointUrls) {
59-
await runner.import(url)
60+
try {
61+
await runner.import(url)
62+
} catch (err) {
63+
if (err.code !== ERR_OUTDATED_OPTIMIZED_DEP) {
64+
hmrClient.logger.error(
65+
`An error happened during full reload\n${err.message}\n${err.stack}`,
66+
)
67+
}
68+
}
6069
}
6170
break
6271
}

packages/vite/src/node/constants.ts

-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,5 @@ export const METADATA_FILENAME = '_metadata.json'
184184

185185
export const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR =
186186
'ERR_OPTIMIZE_DEPS_PROCESSING_ERROR'
187-
export const ERR_OUTDATED_OPTIMIZED_DEP = 'ERR_OUTDATED_OPTIMIZED_DEP'
188187
export const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR =
189188
'ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR'

packages/vite/src/node/plugins/optimizedDeps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
DEP_VERSION_RE,
77
ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR,
88
ERR_OPTIMIZE_DEPS_PROCESSING_ERROR,
9-
ERR_OUTDATED_OPTIMIZED_DEP,
109
} from '../constants'
1110
import { createDebugger } from '../utils'
1211
import { optimizedDepInfoFromFile } from '../optimizer'
1312
import { cleanUrl } from '../../shared/utils'
13+
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
1414

1515
const debug = createDebugger('vite:optimize-deps')
1616

packages/vite/src/node/server/environment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
createExplicitDepsOptimizer,
2424
} from '../optimizer/optimizer'
2525
import { resolveEnvironmentPlugins } from '../plugin'
26-
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../constants'
26+
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
2727
import { promiseWithResolvers } from '../../shared/utils'
2828
import type { ViteDevServer } from '../server'
2929
import { EnvironmentModuleGraph } from './moduleGraph'

packages/vite/src/node/server/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ import { ssrTransform } from '../ssr/ssrTransform'
4343
import { reloadOnTsconfigChange } from '../plugins/esbuild'
4444
import { bindCLIShortcuts } from '../shortcuts'
4545
import type { BindCLIShortcutsOptions } from '../shortcuts'
46-
import {
47-
CLIENT_DIR,
48-
DEFAULT_DEV_PORT,
49-
ERR_OUTDATED_OPTIMIZED_DEP,
50-
} from '../constants'
46+
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
47+
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
5148
import type { Logger } from '../logger'
5249
import { printServerUrls } from '../logger'
5350
import { warnFutureDeprecation } from '../deprecations'

packages/vite/src/node/server/middlewares/transform.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
DEP_VERSION_RE,
2626
ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR,
2727
ERR_OPTIMIZE_DEPS_PROCESSING_ERROR,
28-
ERR_OUTDATED_OPTIMIZED_DEP,
2928
FS_PREFIX,
3029
} from '../../constants'
3130
import {
@@ -35,7 +34,10 @@ import {
3534
} from '../../plugins/css'
3635
import { ERR_CLOSED_SERVER } from '../pluginContainer'
3736
import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils'
38-
import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
37+
import {
38+
ERR_OUTDATED_OPTIMIZED_DEP,
39+
NULL_BYTE_PLACEHOLDER,
40+
} from '../../../shared/constants'
3941
import { ensureServingAccess } from './static'
4042

4143
const debugCache = createDebugger('vite:cache')

packages/vite/src/shared/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ SOURCEMAPPING_URL += 'ppingURL'
2121

2222
export const MODULE_RUNNER_SOURCEMAPPING_SOURCE =
2323
'//# sourceMappingSource=vite-generated'
24+
25+
export const ERR_OUTDATED_OPTIMIZED_DEP = 'ERR_OUTDATED_OPTIMIZED_DEP'

packages/vite/src/shared/moduleRunnerTransport.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ type InvokeableModuleRunnerTransport = Omit<ModuleRunnerTransport, 'invoke'> & {
3333
}
3434

3535
function reviveInvokeError(e: any) {
36-
return Object.assign(new Error(e.message || 'Unknown invoke error'), e)
36+
const innerError = new Error('received error stacktrace')
37+
innerError.stack = e.stack
38+
39+
// set properties to wrapped error, but use the current stacktrace
40+
const wrappedError = new Error(e.message || 'Unknown invoke error', {
41+
cause: innerError,
42+
})
43+
Object.assign(wrappedError, { ...e, stack: wrappedError.stack })
44+
return wrappedError
3745
}
3846

3947
const createInvokeableTransport = (
@@ -94,7 +102,7 @@ const createInvokeableTransport = (
94102

95103
const { e, r } = data.data
96104
if (e) {
97-
promise.reject(reviveInvokeError(e))
105+
promise.reject(e)
98106
} else {
99107
promise.resolve(r)
100108
}
@@ -161,7 +169,11 @@ const createInvokeableTransport = (
161169
})
162170
}
163171

164-
return await promise
172+
try {
173+
return await promise
174+
} catch (err) {
175+
throw reviveInvokeError(err)
176+
}
165177
},
166178
}
167179
}

playground/environment-react-ssr/__tests__/environment-react-ssr.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3+
import { stripVTControlCharacters } from 'node:util'
34
import { describe, expect, onTestFinished, test } from 'vitest'
45
import type { DepOptimizationMetadata } from 'vite'
56
import {
@@ -74,10 +75,9 @@ describe.runIf(!isBuild)('pre-bundling', () => {
7475
serverLogs
7576
.map(
7677
(log) =>
77-
log
78-
// eslint-disable-next-line no-control-regex
79-
.replace(/\x1B\[\d+m/g, '')
80-
.match(/new dependencies optimized: (react-fake-.*)/)?.[1],
78+
stripVTControlCharacters(log).match(
79+
/new dependencies optimized: (react-fake-.*)/,
80+
)?.[1],
8181
)
8282
.filter(Boolean)
8383
.join(', '),

playground/environment-react-ssr/vite.config.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,23 @@ export function vitePluginSsrMiddleware({
7575
const runner = createServerModuleRunner(server.environments.ssr, {
7676
hmr: { logger: false },
7777
})
78+
const importWithRetry = async () => {
79+
try {
80+
return await runner.import(entry)
81+
} catch (e) {
82+
if (
83+
e instanceof Error &&
84+
(e as any).code === 'ERR_OUTDATED_OPTIMIZED_DEP'
85+
) {
86+
runner.clearCache()
87+
return await importWithRetry()
88+
}
89+
throw e
90+
}
91+
}
7892
const handler: Connect.NextHandleFunction = async (req, res, next) => {
7993
try {
80-
const mod = await runner.import(entry)
94+
const mod = await importWithRetry()
8195
await mod['default'](req, res, next)
8296
} catch (e) {
8397
next(e)

0 commit comments

Comments
 (0)