Files
TBW/scripts/inject-preloads.js

43 lines
1.2 KiB
JavaScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs'
import path from 'path'
const distDir = path.resolve('dist')
const manifestPath = path.join(distDir, '.vite/manifest.json')
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'))
const indexPath = path.join(distDir, 'index.html')
let html = fs.readFileSync(indexPath, 'utf-8')
const filesToPreload = new Set()
function collectFiles(entryKey) {
const chunk = manifest[entryKey]
if (!chunk) return
filesToPreload.add(chunk.file)
for (const imp of chunk.imports || []) {
if (imp === 'index.html') continue // skip entrypoint
const dep = manifest[imp]
if (dep) {
filesToPreload.add(dep.file)
collectFiles(imp) // recurse for transitive deps
}
}
}
// automatically find all .vue entries that are dynamic (lazyloaded)
for (const key of Object.keys(manifest)) {
if (key.endsWith('.vue') && manifest[key].isDynamicEntry) {
collectFiles(key)
}
}
for (const f of filesToPreload) {
const tag = `<link rel="modulepreload" href="/${f}">`
if (!html.includes(tag)) {
html = html.replace('</head>', ` ${tag}\n</head>`)
}
}
fs.writeFileSync(indexPath, html)
console.log('Preload tags injected')