Skip to content

Commit

Permalink
perf: start preprocessing static imports before updating module graph (
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 3, 2023
1 parent 3f4d109 commit c90b46e
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -275,7 +275,6 @@ 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<string>()
const acceptedUrls = new Set<{
url: string
start: number
Expand Down Expand Up @@ -615,9 +614,23 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
)
}

if (!isDynamicImport && isLocalImport) {
// for pre-transforming
staticImportedUrls.add(hmrUrl)
if (
!isDynamicImport &&
isLocalImport &&
config.server.preTransformRequests
) {
// pre-transform known direct imports
// These requests will also be registered in transformRequest to be awaited
// by the deps optimizer
const url = removeImportQuery(hmrUrl)
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)
})
}
} else if (!importer.startsWith(clientDir)) {
if (!isInNodeModules(importer)) {
Expand Down Expand Up @@ -760,23 +773,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
)}`,
)

// pre-transform known direct imports
// These requests will also be registered in transformRequest to be awaited
// by the deps optimizer
if (config.server.preTransformRequests && staticImportedUrls.size) {
for (let url of staticImportedUrls) {
url = removeImportQuery(url)
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) {
return transformStableResult(s, importer, config)
} else {
Expand Down

0 comments on commit c90b46e

Please sign in to comment.