mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-09 22:16:01 +08:00
perf(plugin): enhance path resolution using import.meta.dirname for ES modules
This commit is contained in:
parent
7587d2c674
commit
a9b5326544
@ -41,9 +41,9 @@ export function supportESM(): boolean {
|
||||
return parseInt(majorVer) >= 28
|
||||
}
|
||||
|
||||
export function getElectronMajorVersion(): number {
|
||||
export function supportImportMetaPaths(): boolean {
|
||||
const majorVer = getElectronMajorVer()
|
||||
return parseInt(majorVer)
|
||||
return parseInt(majorVer) >= 30
|
||||
}
|
||||
|
||||
export function getElectronPath(): string {
|
||||
|
||||
@ -4,6 +4,7 @@ import type { SourceMapInput } from 'rollup'
|
||||
import { type Plugin, normalizePath } from 'vite'
|
||||
import MagicString from 'magic-string'
|
||||
import { cleanUrl, getHash, toRelativePath } from '../utils'
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
|
||||
const nodeAssetRE = /__VITE_NODE_ASSET__([\w$]+)__/g
|
||||
const nodePublicAssetRE = /__VITE_NODE_PUBLIC_ASSET__([a-z\d]{8})__/g
|
||||
@ -30,6 +31,7 @@ export default function assetPlugin(): Plugin {
|
||||
let outDir = ''
|
||||
const publicAssetPathCache = new Map<string, string>()
|
||||
const assetCache = new Map<string, string>()
|
||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||
return {
|
||||
name: 'vite:node-asset',
|
||||
apply: 'build',
|
||||
@ -81,14 +83,15 @@ export default function assetPlugin(): Plugin {
|
||||
}
|
||||
|
||||
if (assetRE.test(id)) {
|
||||
const dirnameExpr = isImportMetaPathSupported ? 'import.meta.dirname' : '__dirname'
|
||||
if (assetUnpackRE.test(id)) {
|
||||
return `
|
||||
import { join } from 'path'
|
||||
export default join(__dirname, ${referenceId}).replace('app.asar', 'app.asar.unpacked')`
|
||||
export default join(${dirnameExpr}, ${referenceId}).replace('app.asar', 'app.asar.unpacked')`
|
||||
} else {
|
||||
return `
|
||||
import { join } from 'path'
|
||||
export default join(__dirname, ${referenceId})`
|
||||
export default join(${dirnameExpr}, ${referenceId})`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import MagicString from 'magic-string'
|
||||
import type { SourceMapInput } from 'rollup'
|
||||
import type { Plugin } from 'vite'
|
||||
|
||||
import { getElectronMajorVersion } from '../electron'
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
|
||||
const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/
|
||||
|
||||
@ -46,7 +46,7 @@ function findStaticImports(code: string): StaticImport[] {
|
||||
}
|
||||
|
||||
export default function esmShimPlugin(): Plugin {
|
||||
const CJSShim = getElectronMajorVersion() >= 30 ? CJSShim_node_20_11 : CJSShim_normal
|
||||
const CJSShim = supportImportMetaPaths() ? CJSShim_node_20_11 : CJSShim_normal
|
||||
|
||||
return {
|
||||
name: 'vite:esm-shim',
|
||||
|
||||
@ -3,6 +3,7 @@ import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from
|
||||
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
|
||||
import MagicString from 'magic-string'
|
||||
import { cleanUrl, toRelativePath } from '../utils'
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
|
||||
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
||||
|
||||
@ -10,6 +11,7 @@ const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
||||
* Resolve `?modulePath` import and return the module bundle path.
|
||||
*/
|
||||
export default function modulePathPlugin(config: InlineConfig): Plugin {
|
||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||
return {
|
||||
name: 'vite:module-path',
|
||||
apply: 'build',
|
||||
@ -32,9 +34,10 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
|
||||
})
|
||||
})
|
||||
const refId = `__VITE_MODULE_PATH__${hash}__`
|
||||
const dirnameExpr = isImportMetaPathSupported ? 'import.meta.dirname' : '__dirname'
|
||||
return `
|
||||
import { join } from 'path'
|
||||
export default join(__dirname, ${refId})`
|
||||
export default join(${dirnameExpr}, ${refId})`
|
||||
}
|
||||
},
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user