diff --git a/src/plugins/buildReporter.ts b/src/plugins/buildReporter.ts new file mode 100644 index 0000000..b13a395 --- /dev/null +++ b/src/plugins/buildReporter.ts @@ -0,0 +1,27 @@ +import { type Plugin } from 'vite' + +type BuildReporterApi = { + getWatchFiles: () => string[] +} + +export default function buildReporterPlugin(): Plugin { + 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 + } + } + } +} diff --git a/src/plugins/isolateEntries.ts b/src/plugins/isolateEntries.ts index 9a6d631..7e24d31 100644 --- a/src/plugins/isolateEntries.ts +++ b/src/plugins/isolateEntries.ts @@ -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 } } diff --git a/src/plugins/modulePath.ts b/src/plugins/modulePath.ts index f32ec7e..9ec4d73 100644 --- a/src/plugins/modulePath.ts +++ b/src/plugins/modulePath.ts @@ -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() || [] } }