Skip to content

Commit

Permalink
feat: ssr build using optimized deps (#8403)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored May 31, 2022
1 parent e784365 commit 6a5a5b5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
pre: [
...(options.watch ? [ensureWatchPlugin()] : []),
watchPackageDataPlugin(config),
...(!isDepsOptimizerEnabled(config) || options.ssr
...(!isDepsOptimizerEnabled(config)
? [commonjsPlugin(options.commonjsOptions)]
: []),
dataURIPlugin(),
Expand Down Expand Up @@ -402,7 +402,7 @@ async function doBuild(
external = await cjsSsrResolveExternal(config, userExternal)
}

if (isDepsOptimizerEnabled(config) && !ssr) {
if (isDepsOptimizerEnabled(config)) {
await initDepsOptimizer(config)
}

Expand Down
14 changes: 12 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,23 @@ export function getOptimizedDepPath(
)
}

function getDepsCacheSuffix(config: ResolvedConfig): string {
let suffix = ''
if (config.command === 'build') {
suffix += '_build'
if (config.build.ssr) {
suffix += '_ssr'
}
}
return suffix
}
export function getDepsCacheDir(config: ResolvedConfig): string {
const dirName = config.command === 'build' ? 'depsBuild' : 'deps'
const dirName = 'deps' + getDepsCacheSuffix(config)
return normalizePath(path.resolve(config.cacheDir, dirName))
}

function getProcessingDepsCacheDir(config: ResolvedConfig) {
const dirName = config.command === 'build' ? 'processingBuild' : 'processing'
const dirName = 'deps' + getDepsCacheSuffix(config) + '_temp'
return normalizePath(path.resolve(config.cacheDir, dirName))
}

Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface InternalResolveOptions extends ResolveOptions {
export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
const {
root,
isBuild,
isProduction,
asSrc,
ssrConfig,
Expand Down Expand Up @@ -266,7 +267,7 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
!external &&
asSrc &&
depsOptimizer &&
!ssr &&
(isBuild || !ssr) &&
!options.scan &&
(res = await tryOptimizedResolve(depsOptimizer, id, importer))
) {
Expand Down Expand Up @@ -668,7 +669,7 @@ export function tryNodeResolve(
exclude?.includes(pkgId) ||
exclude?.includes(nestedPath) ||
SPECIAL_QUERY_RE.test(resolved) ||
ssr
(!isBuild && ssr)
) {
// excluded from optimization
// Inject a version query to npm deps so that the browser
Expand All @@ -682,7 +683,6 @@ export function tryNodeResolve(
}
}
} else {
// TODO: depsBuild
// this is a missing import, queue optimize-deps re-run and
// get a resolved its optimized info
const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved)
Expand Down
28 changes: 16 additions & 12 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,7 @@ export async function createServer(
},
transformIndexHtml: null!, // to be immediately set
async ssrLoadModule(url, opts?: { fixStacktrace?: boolean }) {
if (!server._ssrExternals) {
let knownImports: string[] = []
const depsOptimizer = getDepsOptimizer(config)
if (depsOptimizer) {
await depsOptimizer.scanProcessing
knownImports = [
...Object.keys(depsOptimizer.metadata.optimized),
...Object.keys(depsOptimizer.metadata.discovered)
]
}
server._ssrExternals = cjsSsrResolveExternals(config, knownImports)
}
await updateCjsSsrExternals(server)
return ssrLoadModule(
url,
server,
Expand Down Expand Up @@ -755,3 +744,18 @@ async function restartServer(server: ViteDevServer) {
// new server (the current server) can restart now
newServer._restartPromise = null
}

async function updateCjsSsrExternals(server: ViteDevServer) {
if (!server._ssrExternals) {
let knownImports: string[] = []
const depsOptimizer = getDepsOptimizer(server.config)
if (depsOptimizer) {
await depsOptimizer.scanProcessing
knownImports = [
...Object.keys(depsOptimizer.metadata.optimized),
...Object.keys(depsOptimizer.metadata.discovered)
]
}
server._ssrExternals = cjsSsrResolveExternals(server.config, knownImports)
}
}

0 comments on commit 6a5a5b5

Please sign in to comment.