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

perf: lazy load rollup/parseAst #15639

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as Rollup from 'rollup'

export type { Rollup }
export { parseAst, parseAstAsync } from 'rollup/parseAst'
export { initRollupParseAst, parseAst, parseAstAsync } from './utils'
export {
defineConfig,
loadConfigFromFile,
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import fs from 'node:fs'
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import { VERSION as rollupVersion } from 'rollup'
import { parseAst as rollupParseAst } from 'rollup/parseAst'
import type {
AsyncPluginHooks,
CustomPluginOptions,
Expand Down Expand Up @@ -69,10 +68,12 @@ import {
createDebugger,
ensureWatchedFile,
generateCodeFrame,
initRollupParseAst,
isExternalUrl,
isObject,
normalizePath,
numberToPos,
parseAst,
prettifyUrl,
timeFrom,
unwrapId,
Expand Down Expand Up @@ -164,6 +165,8 @@ export async function createPluginContainer(
const { getSortedPluginHooks, getSortedPlugins } =
createPluginHookUtils(plugins)

const initRollupParseAstPromise = initRollupParseAst()

const seenResolves: Record<string, true | undefined> = {}
const debugResolve = createDebugger('vite:resolve')
const debugPluginResolve = createDebugger('vite:plugin-resolve', {
Expand Down Expand Up @@ -302,7 +305,7 @@ export async function createPluginContainer(
}

parse(code: string, opts: any) {
return rollupParseAst(code, opts)
return parseAst(code, opts)
}

async resolve(
Expand Down Expand Up @@ -655,6 +658,7 @@ export async function createPluginContainer(
getModuleInfo,

async buildStart() {
await initRollupParseAstPromise
await handleHookPromise(
hookParallel(
'buildStart',
Expand Down
42 changes: 41 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import debug from 'debug'
import type { Alias, AliasOptions } from 'dep-types/alias'
import type MagicString from 'magic-string'

import type { TransformResult } from 'rollup'
import type { AstNode, ParseAst, ParseAstAsync, TransformResult } from 'rollup'
import { createFilter as _createFilter } from '@rollup/pluginutils'
import {
CLIENT_ENTRY,
Expand All @@ -41,6 +41,46 @@ import {
} from './packages'
import type { CommonServerOptions } from '.'

let rollupParseAst: ParseAst
let rollupParseAstAsync: ParseAstAsync
let rollupInited: Promise<any> | boolean = false
export async function initRollupParseAst(): Promise<any | boolean> {
if (!rollupInited) {
const importRollupParseAst = import('rollup/parseAst')
rollupInited = importRollupParseAst
const { parseAst, parseAstAsync } = await importRollupParseAst
rollupParseAst = parseAst
rollupParseAstAsync = parseAstAsync
rollupInited = true
}
return rollupInited
}
export function parseAst(
code: string,
options?: {
allowReturnOutsideFunction?: boolean
},
): AstNode {
if (!rollupInited) {
throw new Error(
`rollup/parseAst is not initialized yet, call initRollupParseAst() first`,
)
}
return rollupParseAst(code, options)
}

export async function parseAstAsync(
code: string,
options?: {
allowReturnOutsideFunction?: boolean
},
): Promise<AstNode> {
if (rollupInited === false) {
await initRollupParseAst()
}
return rollupParseAstAsync(code, options)
}

/**
* Inlined to keep `@rollup/pluginutils` in devDependencies
*/
Expand Down