|
1 |
| -import { writeFileSync, mkdirSync, existsSync, readFileSync } from 'node:fs' |
2 |
| -import { join, parse as parsePath } from 'node:path' |
| 1 | +import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs' |
| 2 | +import { join, isAbsolute, parse as parsePath } from 'node:path' |
3 | 3 | import { HTMLRewriter } from 'html-rewriter-wasm'
|
4 | 4 |
|
5 |
| -const imageFile = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/ |
| 5 | +const imageFileRE = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/ |
6 | 6 |
|
7 |
| -export async function closeBundle() { |
8 |
| - if (!this.resolvedConfig.build.ssr) { |
9 |
| - const distDir = join(this.root, this.resolvedConfig.build.outDir) |
10 |
| - const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8') |
11 |
| - const pages = Object.fromEntries( |
12 |
| - Object.entries(this.resolvedBundle ?? {}) |
13 |
| - .filter(([id, meta]) => { |
14 |
| - if (meta.facadeModuleId?.includes('/pages/')) { |
15 |
| - meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.vue$/, 'html/$1.html') |
16 |
| - return true |
17 |
| - } |
18 |
| - }) |
19 |
| - ) |
20 |
| - for (const page of Object.values(pages)) { |
21 |
| - const jsImports = page.imports |
22 |
| - const cssImports = page.viteMetadata.importedCss |
23 |
| - const images = page.moduleIds.filter(_ => imageFile.test(_)) |
24 |
| - let imagePreloads = '\n' |
25 |
| - for (let image of images) { |
26 |
| - image = image.slice(this.root.length + 1) |
27 |
| - imagePreloads += ` <link rel="preload" as="image" crossorigin href="${this.resolvedConfig.base}${image}">\n` |
28 |
| - } |
29 |
| - let cssPreloads = '' |
30 |
| - for (const css of cssImports) { |
31 |
| - cssPreloads += ` <link rel="preload" as="style" crossorigin href="${this.resolvedConfig.base}${css}">\n` |
32 |
| - } |
33 |
| - let jsPreloads = '' |
34 |
| - for (const js of jsImports) { |
35 |
| - jsPreloads += ` <link rel="modulepreload" crossorigin href="${this.resolvedConfig.base}${js}">\n` |
36 |
| - } |
37 |
| - const pageHtml = await appendHead( |
38 |
| - indexHtml, |
39 |
| - imagePreloads, |
40 |
| - cssPreloads, |
41 |
| - jsPreloads |
42 |
| - ) |
43 |
| - writeHtml(page, pageHtml, distDir) |
| 7 | +export async function closeBundle(resolvedBundle) { |
| 8 | + if (this.environment.name !== 'client') { |
| 9 | + return |
| 10 | + } |
| 11 | + const { assetsInlineLimit } = this.environment.config.build |
| 12 | + const { root, base } = this.environment.config |
| 13 | + let distDir |
| 14 | + if (isAbsolute(this.environment.config.build.outDir)) { |
| 15 | + distDir = this.environment.config.build.outDir |
| 16 | + } else { |
| 17 | + distDir = join(root, this.environment.config.build.outDir) |
| 18 | + } |
| 19 | + const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8') |
| 20 | + const pages = Object.fromEntries( |
| 21 | + Object.entries(resolvedBundle ?? {}) |
| 22 | + .filter(([id, meta]) => { |
| 23 | + if (meta.facadeModuleId?.includes('/pages/')) { |
| 24 | + meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.vue$/, 'html/$1.html') |
| 25 | + return true |
| 26 | + } |
| 27 | + }) |
| 28 | + ) |
| 29 | + for (const page of Object.values(pages)) { |
| 30 | + const jsImports = page.imports |
| 31 | + const cssImports = page.viteMetadata.importedCss |
| 32 | + const images = page.moduleIds.filter((img) => { |
| 33 | + return (page.modules[img].originalLength > assetsInlineLimit) && imageFileRE.test(img) |
| 34 | + }) |
| 35 | + let imagePreloads = '\n' |
| 36 | + for (let image of images) { |
| 37 | + image = image.slice(root.length + 1) |
| 38 | + imagePreloads += ` <link rel="preload" as="image" crossorigin href="${base}${image}">\n` |
44 | 39 | }
|
| 40 | + let cssPreloads = '' |
| 41 | + for (const css of cssImports) { |
| 42 | + cssPreloads += ` <link rel="preload" as="style" crossorigin href="${base}${css}">\n` |
| 43 | + } |
| 44 | + let jsPreloads = '' |
| 45 | + for (const js of jsImports) { |
| 46 | + jsPreloads += ` <link rel="modulepreload" crossorigin href="${base}${js}">\n` |
| 47 | + } |
| 48 | + const pageHtml = await appendHead( |
| 49 | + indexHtml, |
| 50 | + imagePreloads, |
| 51 | + cssPreloads, |
| 52 | + jsPreloads |
| 53 | + ) |
| 54 | + writeHtml(page, pageHtml, distDir) |
45 | 55 | }
|
46 | 56 | }
|
47 | 57 |
|
|
0 commit comments