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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: start preprocessing static imports before updating module graph #12723

Merged
merged 1 commit into from Apr 3, 2023
Merged
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
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