diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 0c94465e1690be..5ef7bb84f6e274 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -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, diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index fa2dd4167fce5d..87251615e18f32 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -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, @@ -69,10 +68,12 @@ import { createDebugger, ensureWatchedFile, generateCodeFrame, + initRollupParseAst, isExternalUrl, isObject, normalizePath, numberToPos, + parseAst, prettifyUrl, timeFrom, unwrapId, @@ -164,6 +165,8 @@ export async function createPluginContainer( const { getSortedPluginHooks, getSortedPlugins } = createPluginHookUtils(plugins) + const initRollupParseAstPromise = initRollupParseAst() + const seenResolves: Record = {} const debugResolve = createDebugger('vite:resolve') const debugPluginResolve = createDebugger('vite:plugin-resolve', { @@ -302,7 +305,7 @@ export async function createPluginContainer( } parse(code: string, opts: any) { - return rollupParseAst(code, opts) + return parseAst(code, opts) } async resolve( @@ -655,6 +658,7 @@ export async function createPluginContainer( getModuleInfo, async buildStart() { + await initRollupParseAstPromise await handleHookPromise( hookParallel( 'buildStart', diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 73c3806cc9fd44..89e02c73423c2b 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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, @@ -41,6 +41,46 @@ import { } from './packages' import type { CommonServerOptions } from '.' +let rollupParseAst: ParseAst +let rollupParseAstAsync: ParseAstAsync +let rollupInited: Promise | boolean = false +export async function initRollupParseAst(): Promise { + 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 { + if (rollupInited === false) { + await initRollupParseAst() + } + return rollupParseAstAsync(code, options) +} + /** * Inlined to keep `@rollup/pluginutils` in devDependencies */