mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-04-30 01:38:14 +08:00
fix: use the environment api in plugins
This commit is contained in:
parent
517d703cbf
commit
2b7c4c33bc
@ -27,7 +27,6 @@ export default async function loadWasm(file, importObject = {}) {
|
|||||||
`
|
`
|
||||||
|
|
||||||
export default function assetPlugin(): Plugin {
|
export default function assetPlugin(): Plugin {
|
||||||
let publicDir = ''
|
|
||||||
const publicAssetPathCache = new Map<string, string>()
|
const publicAssetPathCache = new Map<string, string>()
|
||||||
const assetCache = new Map<string, string>()
|
const assetCache = new Map<string, string>()
|
||||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||||
@ -39,9 +38,6 @@ export default function assetPlugin(): Plugin {
|
|||||||
publicAssetPathCache.clear()
|
publicAssetPathCache.clear()
|
||||||
assetCache.clear()
|
assetCache.clear()
|
||||||
},
|
},
|
||||||
configResolved(config): void {
|
|
||||||
publicDir = config.publicDir
|
|
||||||
},
|
|
||||||
resolveId(id): string | void {
|
resolveId(id): string | void {
|
||||||
if (id === wasmHelperId) {
|
if (id === wasmHelperId) {
|
||||||
return id
|
return id
|
||||||
@ -57,7 +53,11 @@ export default function assetPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let referenceId: string
|
let referenceId: string
|
||||||
|
|
||||||
const file = cleanUrl(id)
|
const file = cleanUrl(id)
|
||||||
|
|
||||||
|
const { publicDir } = this.environment.config
|
||||||
|
|
||||||
if (publicDir && file.startsWith(publicDir)) {
|
if (publicDir && file.startsWith(publicDir)) {
|
||||||
const hash = getHash(file)
|
const hash = getHash(file)
|
||||||
if (!publicAssetPathCache.get(hash)) {
|
if (!publicAssetPathCache.get(hash)) {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import path from 'node:path'
|
|||||||
import { spawn } from 'node:child_process'
|
import { spawn } from 'node:child_process'
|
||||||
import { createRequire } from 'node:module'
|
import { createRequire } from 'node:module'
|
||||||
import colors from 'picocolors'
|
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 * as babel from '@babel/core'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { getElectronPath } from '../electron'
|
import { getElectronPath } from '../electron'
|
||||||
@ -189,7 +189,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
const useStrict = '"use strict";'
|
const useStrict = '"use strict";'
|
||||||
const bytecodeModuleLoader = 'bytecode-loader.cjs'
|
const bytecodeModuleLoader = 'bytecode-loader.cjs'
|
||||||
|
|
||||||
let logger: Logger
|
|
||||||
let supported = false
|
let supported = false
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -200,7 +199,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
if (supported) {
|
if (supported) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger = config.logger
|
|
||||||
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
|
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
|
||||||
if (useInRenderer) {
|
if (useInRenderer) {
|
||||||
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
|
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
|
||||||
@ -339,7 +337,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
writeBundle(_, output): void {
|
writeBundle(_, output): void {
|
||||||
if (supported) {
|
if (supported) {
|
||||||
const bytecodeChunkCount = Object.keys(output).filter(chunk => bytecodeChunkExtensionRE.test(chunk)).length
|
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.`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,6 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type */
|
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type */
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import {
|
import { type InlineConfig, type Plugin, type LogLevel, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
|
||||||
type InlineConfig,
|
|
||||||
type Plugin,
|
|
||||||
type Logger,
|
|
||||||
type LogLevel,
|
|
||||||
type Rolldown,
|
|
||||||
build as viteBuild,
|
|
||||||
mergeConfig
|
|
||||||
} from 'vite'
|
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { cleanUrl } from '../utils'
|
import { cleanUrl } from '../utils'
|
||||||
|
|
||||||
@ -22,8 +14,6 @@ const LogLevels: Record<LogLevel, number> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
||||||
let logger: Logger
|
|
||||||
|
|
||||||
let entries: string[] | { [x: string]: string }[]
|
let entries: string[] | { [x: string]: string }[]
|
||||||
|
|
||||||
let transformedCount = 0
|
let transformedCount = 0
|
||||||
@ -34,10 +24,6 @@ export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
|||||||
name: 'vite:isolate-entries',
|
name: 'vite:isolate-entries',
|
||||||
apply: 'build',
|
apply: 'build',
|
||||||
|
|
||||||
configResolved(config): void {
|
|
||||||
logger = config.logger
|
|
||||||
},
|
|
||||||
|
|
||||||
options(opts): Rolldown.InputOptions | void {
|
options(opts): Rolldown.InputOptions | void {
|
||||||
const { input } = opts
|
const { input } = opts
|
||||||
if (input && typeof input === 'object') {
|
if (input && typeof input === 'object') {
|
||||||
@ -114,7 +100,7 @@ export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
|||||||
|
|
||||||
renderStart(): void {
|
renderStart(): void {
|
||||||
clearLine(-1)
|
clearLine(-1)
|
||||||
logger.info(`${colors.green(`✓`)} ${transformedCount} modules transformed.`)
|
this.environment.logger.info(`${colors.green(`✓`)} ${transformedCount} modules transformed.`)
|
||||||
},
|
},
|
||||||
|
|
||||||
generateBundle(_, bundle): void {
|
generateBundle(_, bundle): void {
|
||||||
@ -161,20 +147,16 @@ function transformReporterPlugin(
|
|||||||
shouldLog = true
|
shouldLog = true
|
||||||
): Plugin<{ getTransformedCount: () => number }> {
|
): Plugin<{ getTransformedCount: () => number }> {
|
||||||
let transformedCount = 0
|
let transformedCount = 0
|
||||||
let root
|
const log = throttle((id, root) => {
|
||||||
const log = throttle(id => {
|
|
||||||
writeLine(`transforming (${preTransformedCount + transformedCount}) ${colors.dim(path.relative(root, id))}`)
|
writeLine(`transforming (${preTransformedCount + transformedCount}) ${colors.dim(path.relative(root, id))}`)
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
name: 'vite:transform-reporter',
|
name: 'vite:transform-reporter',
|
||||||
configResolved(config) {
|
|
||||||
root = config.root
|
|
||||||
},
|
|
||||||
transform(_, id) {
|
transform(_, id) {
|
||||||
transformedCount++
|
transformedCount++
|
||||||
if (!shouldLog) return
|
if (!shouldLog) return
|
||||||
if (id.includes('?')) return
|
if (id.includes('?')) return
|
||||||
log(id)
|
log(id, this.environment.config.root)
|
||||||
},
|
},
|
||||||
api: {
|
api: {
|
||||||
getTransformedCount() {
|
getTransformedCount() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user