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

feat: avoid scanner during build and only optimize CJS in SSR #8932

Merged
merged 4 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/vite/LICENSE.md
Expand Up @@ -975,7 +975,7 @@ Repository: http://github.com/bripkens/connect-history-api-fallback.git

> The MIT License
>
> Copyright (c) 2012 Ben Ripkens http://bripkens.de
> Copyright (c) 2022 Ben Blackmore and contributors
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -545,7 +545,9 @@ export async function resolveConfig(
]
}))
}
return (await container.resolveId(id, importer, { ssr }))?.id
return (
await container.resolveId(id, importer, { ssr, scan: options?.scan })
)?.id
}
}

Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -54,11 +54,7 @@ export type ExportsData = {
export interface DepsOptimizer {
metadata: DepOptimizationMetadata
scanProcessing?: Promise<void>
registerMissingImport: (
id: string,
resolved: string,
ssr?: boolean
) => OptimizedDepInfo
registerMissingImport: (id: string, resolved: string) => OptimizedDepInfo
run: () => void

isOptimizedDepFile: (id: string) => boolean
Expand Down
9 changes: 4 additions & 5 deletions packages/vite/src/node/optimizer/optimizer.ts
Expand Up @@ -183,11 +183,10 @@ async function createDepsOptimizer(
newDepsDiscovered = true
}

// TODO: We need the scan during build time, until preAliasPlugin
// is refactored to work without the scanned deps. We could skip
// this for build later.

runScanner()
if (!isBuild) {
// The scanner is dev only
runScanner()
}
}

async function runScanner() {
Expand Down
37 changes: 35 additions & 2 deletions packages/vite/src/node/plugins/preAlias.ts
@@ -1,6 +1,7 @@
import path from 'node:path'
import type { Alias, AliasOptions, ResolvedConfig } from '..'
import type { Plugin } from '../plugin'
import { bareImportRE } from '../utils'
import { bareImportRE, isOptimizable, moduleListContains } from '../utils'
import { getDepsOptimizer } from '../optimizer'
import { tryOptimizedResolve } from './resolve'

Expand All @@ -21,7 +22,39 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
!options?.scan
) {
if (findPatterns.find((pattern) => matches(pattern, id))) {
return await tryOptimizedResolve(depsOptimizer, id, importer)
const optimizedId = await tryOptimizedResolve(
bluwy marked this conversation as resolved.
Show resolved Hide resolved
depsOptimizer,
id,
importer
)
if (optimizedId) {
return optimizedId // aliased dep already optimized
}
const resolved = await this.resolve(id, importer, {
skipSelf: true,
...options
})
if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
const optimizeDeps = depsOptimizer.options
const resolvedId = resolved.id
const isVirtual = resolvedId === id || resolvedId.includes('\0')
if (
!isVirtual &&
!moduleListContains(optimizeDeps.exclude, id) &&
path.isAbsolute(resolvedId) &&
(resolvedId.includes('node_modules') ||
optimizeDeps.include?.includes(id)) &&
isOptimizable(resolvedId, optimizeDeps)
) {
// aliased dep has not yet been optimized
const optimizedInfo = depsOptimizer!.registerMissingImport(
id,
resolvedId
)
return { id: depsOptimizer!.getOptimizedDepId(optimizedInfo) }
}
}
return resolved
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -698,7 +698,7 @@ export function tryNodeResolve(
} else {
// this is a missing import, queue optimize-deps re-run and
// get a resolved its optimized info
const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved, ssr)
const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved)
resolved = depsOptimizer.getOptimizedDepId(optimizedInfo)
}

Expand Down