Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up preTransformRequest #12672

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
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>()
Comment on lines -279 to +278
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're only tracking the url now

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
Original file line number Diff line number Diff line change
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to e.message to be consistent with import analysis. I also find this to be sufficient for pre-transform errors instead of logging the whole thing.

})
}