diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index bdba4f9fd8ef41..47f83fce04c706 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -9,6 +9,7 @@ import type { ModuleFormat, OutputOptions, Plugin, + RolldownPlugin, RollupBuild, RollupError, RollupLog, @@ -17,6 +18,10 @@ import type { // RollupWatcher, // WatcherOptions, } from 'rolldown' +import { + loadFallbackPlugin as nativeLoadFallbackPlugin, + manifestPlugin as nativeManifestPlugin, +} from 'rolldown/experimental' import type { RollupCommonJSOptions } from 'dep-types/commonjs' import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars' import type { TransformOptions } from 'esbuild' @@ -425,7 +430,7 @@ export function resolveBuildOptions( export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ pre: Plugin[] - post: Plugin[] + post: RolldownPlugin[] }> { const options = config.build // Note: The rolldown internal support commonjs @@ -434,6 +439,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ // !Array.isArray(commonjsOptions?.include) || // commonjsOptions?.include.length !== 0 const rollupOptionsPlugins = options.rollupOptions.plugins + const enableNativePlugin = config.experimental.enableNativePlugin return { pre: [ completeSystemWrapPlugin(), @@ -445,17 +451,23 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ ...(config.isWorker ? [webWorkerPostPlugin()] : []), ], post: [ - buildImportAnalysisPlugin(config), + ...buildImportAnalysisPlugin(config), ...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []), ...(options.minify ? [terserPlugin(config)] : []), ...(!config.isWorker ? [ - ...(options.manifest ? [manifestPlugin(config)] : []), + ...(options.manifest + ? [ + enableNativePlugin + ? nativeManifestPlugin() + : manifestPlugin(config), + ] + : []), ...(options.ssrManifest ? [ssrManifestPlugin(config)] : []), buildReporterPlugin(config), ] : []), - loadFallbackPlugin(), + enableNativePlugin ? nativeLoadFallbackPlugin() : loadFallbackPlugin(), ], } } diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 8e57bdc3015e61..09dea3296a2892 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -8,6 +8,7 @@ import { init, parse as parseImports } from 'es-module-lexer' import type { SourceMap } from 'rolldown' import type { RawSourceMap } from '@ampproject/remapping' import convertSourceMap from 'convert-source-map' +import { buildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental' import { combineSourcemaps, generateCodeFrame, @@ -162,9 +163,10 @@ function preload( /** * Build only. During serve this is performed as part of ./importAnalysis. */ -export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { +export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] { const ssr = !!config.build.ssr const isWorker = config.isWorker + const enableNativePlugin = config.experimental.enableNativePlugin const insertPreload = !(ssr || !!config.build.lib || isWorker) const renderBuiltUrl = config.experimental.renderBuiltUrl @@ -194,7 +196,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { `function(dep) { return ${JSON.stringify(config.base)}+dep }` const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}` - return { + const jsPlugin = { name: 'vite:build-import-analysis', resolveId(id) { if (id === preloadHelperId) { @@ -710,5 +712,23 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { } } }, + } as Plugin + if (enableNativePlugin) { + delete jsPlugin.transform + delete jsPlugin.resolveId + delete jsPlugin.load } + return [ + jsPlugin, + enableNativePlugin + ? nativeBuildImportAnalysisPlugin({ + preloadCode: preloadCode, + insertPreload: insertPreload, + /// this field looks redundant, put a dummy value for now + optimizeModulePreloadRelativePaths: false, + renderBuiltUrl: Boolean(renderBuiltUrl), + isRelativeBase: isRelativeBase, + }) + : null, + ].filter(Boolean) as [Plugin] } diff --git a/playground/html/__tests__/html.spec.ts b/playground/html/__tests__/html.spec.ts index 85ceea961752ee..52c6132b8631d2 100644 --- a/playground/html/__tests__/html.spec.ts +++ b/playground/html/__tests__/html.spec.ts @@ -299,7 +299,7 @@ describe.runIf(isServe)('invalid', () => { test('should reload when fixed', async () => { await page.goto(viteTestUrl + '/invalid.html') - await editFile('invalid.html', (content) => { + editFile('invalid.html', (content) => { return content.replace('
Good') }) const content = await page.waitForSelector('text=Good HTML')