feat: reporter plugin for isolated builds

This commit is contained in:
alex8088 2025-10-29 22:32:35 +08:00
parent 7c7f31b2a3
commit cfd9812a91
3 changed files with 35 additions and 38 deletions

View File

@ -0,0 +1,27 @@
import { type Plugin } from 'vite'
type BuildReporterApi = {
getWatchFiles: () => string[]
}
export default function buildReporterPlugin(): Plugin<BuildReporterApi> {
const moduleIds: string[] = []
return {
name: 'vite:build-reporter',
buildEnd() {
const allModuleIds = Array.from(this.getModuleIds())
const sourceFiles = allModuleIds.filter(id => {
const info = this.getModuleInfo(id)
return info && !info.isExternal
})
moduleIds.push(...sourceFiles)
},
api: {
getWatchFiles() {
return moduleIds
}
}
}
}

View File

@ -1,6 +1,7 @@
import { type InlineConfig, type Plugin, type Logger, build as viteBuild, mergeConfig } from 'vite'
import type { InputOptions, RollupOutput } from 'rollup'
import colors from 'picocolors'
import buildReporterPlugin from './buildReporter'
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
@ -90,9 +91,9 @@ async function bundleEntryFile(
config: InlineConfig,
watch: boolean
): Promise<{ bundles: RollupOutput; watchFiles: string[]; transformedCount: number }> {
const moduleIds: string[] = []
let transformedCount = 0
const reporter = watch ? buildReporterPlugin() : undefined
const viteConfig = mergeConfig(config, {
build: {
write: false,
@ -105,23 +106,7 @@ async function bundleEntryFile(
transformedCount++
}
} as Plugin,
...(watch
? [
{
name: 'vite:get-watch-files',
buildEnd(): void {
const allModuleIds = Array.from(this.getModuleIds())
const sourceFiles = allModuleIds.filter(id => {
const info = this.getModuleInfo(id)
return info && !info.isExternal
})
moduleIds.push(...sourceFiles)
}
} as Plugin
]
: [])
reporter
],
logLevel: 'warn',
configFile: false
@ -134,7 +119,7 @@ async function bundleEntryFile(
return {
bundles: bundles as RollupOutput,
watchFiles: moduleIds,
watchFiles: reporter?.api?.getWatchFiles() || [],
transformedCount
}
}

View File

@ -2,6 +2,7 @@ import path from 'node:path'
import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from 'vite'
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
import MagicString from 'magic-string'
import buildReporterPlugin from './buildReporter'
import { cleanUrl, toRelativePath } from '../utils'
import { supportImportMetaPaths } from '../electron'
@ -84,7 +85,7 @@ async function bundleEntryFile(
config: InlineConfig,
watch: boolean
): Promise<{ bundles: RollupOutput; watchFiles: string[] }> {
const moduleIds: string[] = []
const reporter = watch ? buildReporterPlugin() : undefined
const viteConfig = mergeConfig(config, {
build: {
rollupOptions: { input },
@ -101,23 +102,7 @@ async function bundleEntryFile(
return output
}
},
...(watch
? [
{
name: 'vite:get-watch-files',
buildEnd(): void {
const allModuleIds = Array.from(this.getModuleIds())
const sourceFiles = allModuleIds.filter(id => {
const info = this.getModuleInfo(id)
return info && !info.isExternal
})
moduleIds.push(...sourceFiles)
}
} as Plugin
]
: [])
reporter
],
logLevel: 'warn',
configFile: false
@ -126,6 +111,6 @@ async function bundleEntryFile(
return {
bundles: bundles as RollupOutput,
watchFiles: moduleIds
watchFiles: reporter?.api?.getWatchFiles() || []
}
}