mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-10 14:32:44 +08:00
feat: reporter plugin for isolated builds
This commit is contained in:
parent
7c7f31b2a3
commit
cfd9812a91
27
src/plugins/buildReporter.ts
Normal file
27
src/plugins/buildReporter.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import { type InlineConfig, type Plugin, type Logger, build as viteBuild, mergeConfig } from 'vite'
|
import { type InlineConfig, type Plugin, type Logger, build as viteBuild, mergeConfig } from 'vite'
|
||||||
import type { InputOptions, RollupOutput } from 'rollup'
|
import type { InputOptions, RollupOutput } from 'rollup'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
|
import buildReporterPlugin from './buildReporter'
|
||||||
|
|
||||||
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
|
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
|
||||||
|
|
||||||
@ -90,9 +91,9 @@ async function bundleEntryFile(
|
|||||||
config: InlineConfig,
|
config: InlineConfig,
|
||||||
watch: boolean
|
watch: boolean
|
||||||
): Promise<{ bundles: RollupOutput; watchFiles: string[]; transformedCount: number }> {
|
): Promise<{ bundles: RollupOutput; watchFiles: string[]; transformedCount: number }> {
|
||||||
const moduleIds: string[] = []
|
|
||||||
let transformedCount = 0
|
let transformedCount = 0
|
||||||
|
|
||||||
|
const reporter = watch ? buildReporterPlugin() : undefined
|
||||||
const viteConfig = mergeConfig(config, {
|
const viteConfig = mergeConfig(config, {
|
||||||
build: {
|
build: {
|
||||||
write: false,
|
write: false,
|
||||||
@ -105,23 +106,7 @@ async function bundleEntryFile(
|
|||||||
transformedCount++
|
transformedCount++
|
||||||
}
|
}
|
||||||
} as Plugin,
|
} as Plugin,
|
||||||
...(watch
|
reporter
|
||||||
? [
|
|
||||||
{
|
|
||||||
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
|
|
||||||
]
|
|
||||||
: [])
|
|
||||||
],
|
],
|
||||||
logLevel: 'warn',
|
logLevel: 'warn',
|
||||||
configFile: false
|
configFile: false
|
||||||
@ -134,7 +119,7 @@ async function bundleEntryFile(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
bundles: bundles as RollupOutput,
|
bundles: bundles as RollupOutput,
|
||||||
watchFiles: moduleIds,
|
watchFiles: reporter?.api?.getWatchFiles() || [],
|
||||||
transformedCount
|
transformedCount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import path from 'node:path'
|
|||||||
import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from 'vite'
|
import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from 'vite'
|
||||||
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
|
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
|
import buildReporterPlugin from './buildReporter'
|
||||||
import { cleanUrl, toRelativePath } from '../utils'
|
import { cleanUrl, toRelativePath } from '../utils'
|
||||||
import { supportImportMetaPaths } from '../electron'
|
import { supportImportMetaPaths } from '../electron'
|
||||||
|
|
||||||
@ -84,7 +85,7 @@ async function bundleEntryFile(
|
|||||||
config: InlineConfig,
|
config: InlineConfig,
|
||||||
watch: boolean
|
watch: boolean
|
||||||
): Promise<{ bundles: RollupOutput; watchFiles: string[] }> {
|
): Promise<{ bundles: RollupOutput; watchFiles: string[] }> {
|
||||||
const moduleIds: string[] = []
|
const reporter = watch ? buildReporterPlugin() : undefined
|
||||||
const viteConfig = mergeConfig(config, {
|
const viteConfig = mergeConfig(config, {
|
||||||
build: {
|
build: {
|
||||||
rollupOptions: { input },
|
rollupOptions: { input },
|
||||||
@ -101,23 +102,7 @@ async function bundleEntryFile(
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
...(watch
|
reporter
|
||||||
? [
|
|
||||||
{
|
|
||||||
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
|
|
||||||
]
|
|
||||||
: [])
|
|
||||||
],
|
],
|
||||||
logLevel: 'warn',
|
logLevel: 'warn',
|
||||||
configFile: false
|
configFile: false
|
||||||
@ -126,6 +111,6 @@ async function bundleEntryFile(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
bundles: bundles as RollupOutput,
|
bundles: bundles as RollupOutput,
|
||||||
watchFiles: moduleIds
|
watchFiles: reporter?.api?.getWatchFiles() || []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user