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 2 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
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
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import type { ResolveFn } from '../'
import {
initRollupParseAst,
injectQuery,
isParentDirectory,
slash,
Expand Down Expand Up @@ -55,6 +56,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg
const cleanString = stripLiteral(code)

await initRollupParseAst()

let match: RegExpExecArray | null
while ((match = assetImportMetaUrlRE.exec(cleanString))) {
const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices!
Expand Down
4 changes: 2 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 @@ -73,6 +72,7 @@ import {
isObject,
normalizePath,
numberToPos,
parseAst,
prettifyUrl,
timeFrom,
unwrapId,
Expand Down Expand Up @@ -302,7 +302,7 @@ export async function createPluginContainer(
}

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

async resolve(
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