mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-11-02 14:51:51 +08:00
feat: add Vite 8 support
Vite 8 replaces Rollup with Rolldown, which removes the `resolveImportMeta` hook. Add a `renderChunk` fallback in the importMeta plugin so `import.meta.url/filename/dirname` are still rewritten to CJS equivalents. The existing `resolveImportMeta` hook is kept for Vite 5-7 backward compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a9a496dc1a
commit
8660e7ac5e
@ -61,7 +61,7 @@
|
|||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@swc/core": "^1.0.0",
|
"@swc/core": "^1.0.0",
|
||||||
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
||||||
},
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
"@swc/core": {
|
"@swc/core": {
|
||||||
|
|||||||
@ -1,10 +1,17 @@
|
|||||||
|
import MagicString from 'magic-string'
|
||||||
|
import type { SourceMapInput } from 'rollup'
|
||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
|
|
||||||
|
const importMetaUrlRE = /\bimport\.meta\.url\b/g
|
||||||
|
const importMetaFilenameRE = /\bimport\.meta\.filename\b/g
|
||||||
|
const importMetaDirnameRE = /\bimport\.meta\.dirname\b/g
|
||||||
|
|
||||||
export default function importMetaPlugin(): Plugin {
|
export default function importMetaPlugin(): Plugin {
|
||||||
return {
|
return {
|
||||||
name: 'vite:import-meta',
|
name: 'vite:import-meta',
|
||||||
apply: 'build',
|
apply: 'build',
|
||||||
enforce: 'pre',
|
enforce: 'pre',
|
||||||
|
// For Vite 5-7 (Rollup) - this hook is removed in Vite 8
|
||||||
resolveImportMeta(property, { format }): string | null {
|
resolveImportMeta(property, { format }): string | null {
|
||||||
if (property === 'url' && format === 'cjs') {
|
if (property === 'url' && format === 'cjs') {
|
||||||
return `require("url").pathToFileURL(__filename).href`
|
return `require("url").pathToFileURL(__filename).href`
|
||||||
@ -16,6 +23,34 @@ export default function importMetaPlugin(): Plugin {
|
|||||||
return `__dirname`
|
return `__dirname`
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
|
},
|
||||||
|
// Fallback for Vite 8+ (Rolldown) where resolveImportMeta is removed
|
||||||
|
renderChunk(code, _chunk, { format, sourcemap }): { code: string; map?: SourceMapInput } | null {
|
||||||
|
if (format !== 'cjs') return null
|
||||||
|
if (!code.includes('import.meta.')) return null
|
||||||
|
|
||||||
|
const s = new MagicString(code)
|
||||||
|
let hasReplacements = false
|
||||||
|
|
||||||
|
for (const match of code.matchAll(importMetaUrlRE)) {
|
||||||
|
s.overwrite(match.index, match.index + match[0].length, 'require("url").pathToFileURL(__filename).href')
|
||||||
|
hasReplacements = true
|
||||||
|
}
|
||||||
|
for (const match of code.matchAll(importMetaFilenameRE)) {
|
||||||
|
s.overwrite(match.index, match.index + match[0].length, '__filename')
|
||||||
|
hasReplacements = true
|
||||||
|
}
|
||||||
|
for (const match of code.matchAll(importMetaDirnameRE)) {
|
||||||
|
s.overwrite(match.index, match.index + match[0].length, '__dirname')
|
||||||
|
hasReplacements = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasReplacements) return null
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: s.toString(),
|
||||||
|
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user