fix: use the environment api in plugins

This commit is contained in:
alex8088 2026-04-09 21:51:42 +08:00
parent 517d703cbf
commit 2b7c4c33bc
3 changed files with 10 additions and 30 deletions

View File

@ -27,7 +27,6 @@ export default async function loadWasm(file, importObject = {}) {
`
export default function assetPlugin(): Plugin {
let publicDir = ''
const publicAssetPathCache = new Map<string, string>()
const assetCache = new Map<string, string>()
const isImportMetaPathSupported = supportImportMetaPaths()
@ -39,9 +38,6 @@ export default function assetPlugin(): Plugin {
publicAssetPathCache.clear()
assetCache.clear()
},
configResolved(config): void {
publicDir = config.publicDir
},
resolveId(id): string | void {
if (id === wasmHelperId) {
return id
@ -57,7 +53,11 @@ export default function assetPlugin(): Plugin {
}
let referenceId: string
const file = cleanUrl(id)
const { publicDir } = this.environment.config
if (publicDir && file.startsWith(publicDir)) {
const hash = getHash(file)
if (!publicAssetPathCache.get(hash)) {

View File

@ -2,7 +2,7 @@ import path from 'node:path'
import { spawn } from 'node:child_process'
import { createRequire } from 'node:module'
import colors from 'picocolors'
import { type Plugin, type Logger, type LibraryOptions, type Rolldown, normalizePath } from 'vite'
import { type Plugin, type LibraryOptions, type Rolldown, normalizePath } from 'vite'
import * as babel from '@babel/core'
import MagicString from 'magic-string'
import { getElectronPath } from '../electron'
@ -189,7 +189,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const useStrict = '"use strict";'
const bytecodeModuleLoader = 'bytecode-loader.cjs'
let logger: Logger
let supported = false
return {
@ -200,7 +199,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
if (supported) {
return
}
logger = config.logger
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
if (useInRenderer) {
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
@ -339,7 +337,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
writeBundle(_, output): void {
if (supported) {
const bytecodeChunkCount = Object.keys(output).filter(chunk => bytecodeChunkExtensionRE.test(chunk)).length
logger.info(`${colors.green(``)} ${bytecodeChunkCount} chunks compiled into bytecode.`)
this.environment.logger.info(`${colors.green(``)} ${bytecodeChunkCount} chunks compiled into bytecode.`)
}
}
}

View File

@ -1,14 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type */
import path from 'node:path'
import {
type InlineConfig,
type Plugin,
type Logger,
type LogLevel,
type Rolldown,
build as viteBuild,
mergeConfig
} from 'vite'
import { type InlineConfig, type Plugin, type LogLevel, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
import colors from 'picocolors'
import { cleanUrl } from '../utils'
@ -22,8 +14,6 @@ const LogLevels: Record<LogLevel, number> = {
}
export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
let logger: Logger
let entries: string[] | { [x: string]: string }[]
let transformedCount = 0
@ -34,10 +24,6 @@ export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
name: 'vite:isolate-entries',
apply: 'build',
configResolved(config): void {
logger = config.logger
},
options(opts): Rolldown.InputOptions | void {
const { input } = opts
if (input && typeof input === 'object') {
@ -114,7 +100,7 @@ export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
renderStart(): void {
clearLine(-1)
logger.info(`${colors.green(``)} ${transformedCount} modules transformed.`)
this.environment.logger.info(`${colors.green(``)} ${transformedCount} modules transformed.`)
},
generateBundle(_, bundle): void {
@ -161,20 +147,16 @@ function transformReporterPlugin(
shouldLog = true
): Plugin<{ getTransformedCount: () => number }> {
let transformedCount = 0
let root
const log = throttle(id => {
const log = throttle((id, root) => {
writeLine(`transforming (${preTransformedCount + transformedCount}) ${colors.dim(path.relative(root, id))}`)
})
return {
name: 'vite:transform-reporter',
configResolved(config) {
root = config.root
},
transform(_, id) {
transformedCount++
if (!shouldLog) return
if (id.includes('?')) return
log(id)
log(id, this.environment.config.root)
},
api: {
getTransformedCount() {