mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-04-29 17:28:15 +08:00
fix(asset): isolate asset caches per environment
This commit is contained in:
parent
2b7c4c33bc
commit
a041f42e0b
@ -1,7 +1,7 @@
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import fs from 'node:fs/promises'
|
import fs from 'node:fs/promises'
|
||||||
import type { SourceMapInput } from 'rollup'
|
import type { SourceMapInput } from 'rollup'
|
||||||
import { type Plugin, normalizePath } from 'vite'
|
import { type Plugin, type Environment, normalizePath } from 'vite'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { cleanUrl, getHash, toRelativePath } from '../utils'
|
import { cleanUrl, getHash, toRelativePath } from '../utils'
|
||||||
import { supportImportMetaPaths } from '../electron'
|
import { supportImportMetaPaths } from '../electron'
|
||||||
@ -26,17 +26,18 @@ export default async function loadWasm(file, importObject = {}) {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const assetCache = new WeakMap<Environment, Map<string, string>>()
|
||||||
|
const publicAssetPathCache = new WeakMap<Environment, Map<string, string>>()
|
||||||
|
|
||||||
export default function assetPlugin(): Plugin {
|
export default function assetPlugin(): Plugin {
|
||||||
const publicAssetPathCache = new Map<string, string>()
|
|
||||||
const assetCache = new Map<string, string>()
|
|
||||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||||
return {
|
return {
|
||||||
name: 'vite:node-asset',
|
name: 'vite:node-asset',
|
||||||
apply: 'build',
|
apply: 'build',
|
||||||
enforce: 'pre',
|
enforce: 'pre',
|
||||||
buildStart(): void {
|
buildStart(): void {
|
||||||
publicAssetPathCache.clear()
|
publicAssetPathCache.set(this.environment, new Map())
|
||||||
assetCache.clear()
|
assetCache.set(this.environment, new Map())
|
||||||
},
|
},
|
||||||
resolveId(id): string | void {
|
resolveId(id): string | void {
|
||||||
if (id === wasmHelperId) {
|
if (id === wasmHelperId) {
|
||||||
@ -60,12 +61,14 @@ export default function assetPlugin(): Plugin {
|
|||||||
|
|
||||||
if (publicDir && file.startsWith(publicDir)) {
|
if (publicDir && file.startsWith(publicDir)) {
|
||||||
const hash = getHash(file)
|
const hash = getHash(file)
|
||||||
if (!publicAssetPathCache.get(hash)) {
|
const publicAssetPathMap = publicAssetPathCache.get(this.environment)!
|
||||||
publicAssetPathCache.set(hash, file)
|
if (!publicAssetPathMap.get(hash)) {
|
||||||
|
publicAssetPathMap.set(hash, file)
|
||||||
}
|
}
|
||||||
referenceId = `__VITE_NODE_PUBLIC_ASSET__${hash}__`
|
referenceId = `__VITE_NODE_PUBLIC_ASSET__${hash}__`
|
||||||
} else {
|
} else {
|
||||||
const cached = assetCache.get(file)
|
const cache = assetCache.get(this.environment)!
|
||||||
|
const cached = cache.get(id)
|
||||||
if (cached) {
|
if (cached) {
|
||||||
referenceId = cached
|
referenceId = cached
|
||||||
} else {
|
} else {
|
||||||
@ -76,7 +79,7 @@ export default function assetPlugin(): Plugin {
|
|||||||
source: source as unknown as Uint8Array
|
source: source as unknown as Uint8Array
|
||||||
})
|
})
|
||||||
referenceId = `__VITE_NODE_ASSET__${hash}__`
|
referenceId = `__VITE_NODE_ASSET__${hash}__`
|
||||||
assetCache.set(file, referenceId)
|
cache.set(file, referenceId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,11 +122,13 @@ export default function assetPlugin(): Plugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const publicAssetPathMap = publicAssetPathCache.get(this.environment)!
|
||||||
|
|
||||||
nodePublicAssetRE.lastIndex = 0
|
nodePublicAssetRE.lastIndex = 0
|
||||||
while ((match = nodePublicAssetRE.exec(code))) {
|
while ((match = nodePublicAssetRE.exec(code))) {
|
||||||
s ||= new MagicString(code)
|
s ||= new MagicString(code)
|
||||||
const [full, hash] = match
|
const [full, hash] = match
|
||||||
const filename = publicAssetPathCache.get(hash)!
|
const filename = publicAssetPathMap.get(hash)!
|
||||||
const outputFilepath = toRelativePath(filename, normalizePath(path.join(dir!, chunk.fileName)))
|
const outputFilepath = toRelativePath(filename, normalizePath(path.join(dir!, chunk.fileName)))
|
||||||
const replacement = JSON.stringify(outputFilepath)
|
const replacement = JSON.stringify(outputFilepath)
|
||||||
s.overwrite(match.index, match.index + full.length, replacement, {
|
s.overwrite(match.index, match.index + full.length, replacement, {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user