-
richard.petersen authored
Root cause: Version has was updated before file cache Solution: Keep all updates in one place and switch to new version at the same time
richard.petersen authoredRoot cause: Version has was updated before file cache Solution: Keep all updates in one place and switch to new version at the same time
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
util.js 1.71 KiB
import path from 'path'
// totaly awesome hash function. Do not use this for encryption (crypto.subtle.digest etc would be overkill for this)
export function hash (array) {
const string = JSON.stringify(array)
if (!string.length) throw new Error('TypeError: Unexpected data to calculate hash from')
let hash = 0
let char
for (let i = 0; i < string.length; i++) {
char = string.charCodeAt(i)
hash = ((hash << 5) - hash) + char
hash |= 0
}
return new Uint32Array([hash]).toString()
}
export function isJSFile (name) {
const extname = path.extname(name)
return extname === '.js'
}
export function viteManifestToDeps (viteManifest) {
const deps = {}
for (const [codePoint, { isEntry, file, imports, css, assets }] of Object.entries(viteManifest)) {
if (isEntry && codePoint.endsWith('.html')) deps[codePoint] = [file]
if (Array.isArray(assets)) assets.forEach(asset => { deps[asset] = [] })
if (Array.isArray(css)) css.forEach(css => { deps[css] = [] })
deps[file] = []
.concat(imports?.map(path => viteManifest[path].file))
.concat(css)
.concat(assets)
.filter(Boolean)
}
return deps
}
export function viteToOxManifest (viteManifests) {
const deps = viteManifestToDeps(viteManifests)
return Object.values(viteManifests)
.filter(manifest => Array.isArray(manifest?.meta?.manifests))
.map(manifest =>
manifest.meta.manifests.map(oxManifest => {
const dependencies = deps[manifest.file]
const data = {
...oxManifest,
path: manifest.file.slice(0, -path.parse(manifest.file).ext.length)
}
if (dependencies?.length > 0) data.dependencies = dependencies
return data
})
)
.flat()
}