Skip to content

Commit

Permalink
refactor: clean up preTransformRequest (#12672)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Mar 31, 2023
1 parent 49674b5 commit 561227c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -53,7 +53,6 @@ import {
cjsShouldExternalizeForSSR,
shouldExternalizeForSSR,
} from '../ssr/ssrExternal'
import { transformRequest } from '../server/transformRequest'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import { checkPublicFile } from './asset'
import {
Expand Down Expand Up @@ -276,7 +275,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
let s: MagicString | undefined
const str = () => s || (s = new MagicString(source))
const importedUrls = new Set<string>()
const staticImportedUrls = new Set<{ url: string; id: string }>()
const staticImportedUrls = new Set<string>()
const acceptedUrls = new Set<{
url: string
start: number
Expand Down Expand Up @@ -617,7 +616,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {

if (!isDynamicImport && isLocalImport) {
// for pre-transforming
staticImportedUrls.add({ url: hmrUrl, id: resolvedId })
staticImportedUrls.add(hmrUrl)
}
} else if (!importer.startsWith(clientDir)) {
if (!isInNodeModules(importer)) {
Expand Down Expand Up @@ -764,17 +763,17 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// These requests will also be registered in transformRequest to be awaited
// by the deps optimizer
if (config.server.preTransformRequests && staticImportedUrls.size) {
staticImportedUrls.forEach(({ url }) => {
for (let url of staticImportedUrls) {
url = removeImportQuery(url)
transformRequest(url, server, { ssr }).catch((e) => {
server.transformRequest(url, { ssr }).catch((e) => {
if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) {
// This are expected errors
return
}
// Unexpected error, log the issue but avoid an unhandled exception
config.logger.error(e.message)
})
})
}
}

if (s) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -326,6 +326,6 @@ function preTransformRequest(server: ViteDevServer, url: string, base: string) {
// transform all url as non-ssr as html includes client-side assets only
server.transformRequest(url).catch((e) => {
// Unexpected error, log the issue but avoid an unhandled exception
server.config.logger.error(e)
server.config.logger.error(e.message)
})
}

0 comments on commit 561227c

Please sign in to comment.