Skip to content

Commit

Permalink
chore: apply vitejs#18713
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 20, 2024
1 parent 07ad00f commit c78b553
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 19 deletions.
11 changes: 10 additions & 1 deletion packages/vite/src/module-runner/hmrHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HotPayload } from 'types/hmrPayload'
import { slash, unwrapId } from '../shared/utils'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../shared/constants'
import type { ModuleRunner } from './runner'

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

for (const url of clearEntrypointUrls) {
await runner.import(url)
try {
await runner.import(url)
} catch (err) {
if (err.code !== ERR_OUTDATED_OPTIMIZED_DEP) {
hmrClient.logger.error(
`An error happened during full reload\n${err.message}\n${err.stack}`,
)
}
}
}
break
}
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,5 @@ export const METADATA_FILENAME = '_metadata.json'

export const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR =
'ERR_OPTIMIZE_DEPS_PROCESSING_ERROR'
export const ERR_OUTDATED_OPTIMIZED_DEP = 'ERR_OUTDATED_OPTIMIZED_DEP'
export const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR =
'ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR'
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/optimizedDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
DEP_VERSION_RE,
ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR,
ERR_OPTIMIZE_DEPS_PROCESSING_ERROR,
ERR_OUTDATED_OPTIMIZED_DEP,
} from '../constants'
import { createDebugger } from '../utils'
import { optimizedDepInfoFromFile } from '../optimizer'
import { cleanUrl } from '../../shared/utils'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'

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

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
createExplicitDepsOptimizer,
} from '../optimizer/optimizer'
import { resolveEnvironmentPlugins } from '../plugin'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../constants'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
import { promiseWithResolvers } from '../../shared/utils'
import type { ViteDevServer } from '../server'
import { EnvironmentModuleGraph } from './moduleGraph'
Expand Down
7 changes: 2 additions & 5 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ import { ssrTransform } from '../ssr/ssrTransform'
import { reloadOnTsconfigChange } from '../plugins/esbuild'
import { bindCLIShortcuts } from '../shortcuts'
import type { BindCLIShortcutsOptions } from '../shortcuts'
import {
CLIENT_DIR,
DEFAULT_DEV_PORT,
ERR_OUTDATED_OPTIMIZED_DEP,
} from '../constants'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { warnFutureDeprecation } from '../deprecations'
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
DEP_VERSION_RE,
ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR,
ERR_OPTIMIZE_DEPS_PROCESSING_ERROR,
ERR_OUTDATED_OPTIMIZED_DEP,
FS_PREFIX,
} from '../../constants'
import {
Expand All @@ -35,7 +34,10 @@ import {
} from '../../plugins/css'
import { ERR_CLOSED_SERVER } from '../pluginContainer'
import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils'
import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants'
import {
ERR_OUTDATED_OPTIMIZED_DEP,
NULL_BYTE_PLACEHOLDER,
} from '../../../shared/constants'
import { ensureServingAccess } from './static'

const debugCache = createDebugger('vite:cache')
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ SOURCEMAPPING_URL += 'ppingURL'

export const MODULE_RUNNER_SOURCEMAPPING_SOURCE =
'//# sourceMappingSource=vite-generated'

export const ERR_OUTDATED_OPTIMIZED_DEP = 'ERR_OUTDATED_OPTIMIZED_DEP'
18 changes: 15 additions & 3 deletions packages/vite/src/shared/moduleRunnerTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ type InvokeableModuleRunnerTransport = Omit<ModuleRunnerTransport, 'invoke'> & {
}

function reviveInvokeError(e: any) {
return Object.assign(new Error(e.message || 'Unknown invoke error'), e)
const innerError = new Error('received error stacktrace')
innerError.stack = e.stack

// set properties to wrapped error, but use the current stacktrace
const wrappedError = new Error(e.message || 'Unknown invoke error', {
cause: innerError,
})
Object.assign(wrappedError, { ...e, stack: wrappedError.stack })
return wrappedError
}

const createInvokeableTransport = (
Expand Down Expand Up @@ -94,7 +102,7 @@ const createInvokeableTransport = (

const { e, r } = data.data
if (e) {
promise.reject(reviveInvokeError(e))
promise.reject(e)
} else {
promise.resolve(r)
}
Expand Down Expand Up @@ -161,7 +169,11 @@ const createInvokeableTransport = (
})
}

return await promise
try {
return await promise
} catch (err) {
throw reviveInvokeError(err)
}
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import { stripVTControlCharacters } from 'node:util'
import { describe, expect, onTestFinished, test } from 'vitest'
import type { DepOptimizationMetadata } from 'vite'
import {
Expand Down Expand Up @@ -74,10 +75,9 @@ describe.runIf(!isBuild)('pre-bundling', () => {
serverLogs
.map(
(log) =>
log
// eslint-disable-next-line no-control-regex
.replace(/\x1B\[\d+m/g, '')
.match(/new dependencies optimized: (react-fake-.*)/)?.[1],
stripVTControlCharacters(log).match(
/new dependencies optimized: (react-fake-.*)/,
)?.[1],
)
.filter(Boolean)
.join(', '),
Expand Down
16 changes: 15 additions & 1 deletion playground/environment-react-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,23 @@ export function vitePluginSsrMiddleware({
const runner = createServerModuleRunner(server.environments.ssr, {
hmr: { logger: false },
})
const importWithRetry = async () => {
try {
return await runner.import(entry)
} catch (e) {
if (
e instanceof Error &&
(e as any).code === 'ERR_OUTDATED_OPTIMIZED_DEP'
) {
runner.clearCache()
return await importWithRetry()
}
throw e
}
}
const handler: Connect.NextHandleFunction = async (req, res, next) => {
try {
const mod = await runner.import(entry)
const mod = await importWithRetry()
await mod['default'](req, res, next)
} catch (e) {
next(e)
Expand Down

0 comments on commit c78b553

Please sign in to comment.