feat: minify head inline scripts

pull/478/head
Evan You 3 years ago
parent a355b20f90
commit e61db62a1c

@ -3,6 +3,14 @@ export default {
title: 'VitePress',
description: 'Vite & Vue powered static site generator.',
head: [
[
'script',
{},
'(() => { const afsefe = window.foo;\n console.log(afsefe);})()'
]
],
themeConfig: {
repo: 'vuejs/vitepress',
docsDir: 'docs',

@ -2,7 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import { SiteConfig, resolveSiteDataByRoute } from '../config'
import { HeadConfig } from '../shared'
import { normalizePath } from 'vite'
import { normalizePath, transformWithEsbuild } from 'vite'
import { RollupOutput, OutputChunk, OutputAsset } from 'rollup'
import { slash } from '../utils/slash'
import escape from 'escape-html'
@ -121,7 +121,7 @@ export async function renderPage(
${stylesheetLink}
${preloadLinksString}
${prefetchLinkString}
${renderHead(head)}
${await renderHead(head)}
</head>
<body>
<div id="app">${content}</div>
@ -165,17 +165,24 @@ function resolvePageImports(
]
}
function renderHead(head: HeadConfig[]) {
return head
.map(([tag, attrs = {}, innerHTML = '']) => {
function renderHead(head: HeadConfig[]): Promise<string> {
return Promise.all(
head.map(async ([tag, attrs = {}, innerHTML = '']) => {
const openTag = `<${tag}${renderAttrs(attrs)}>`
if (tag !== 'link' && tag !== 'meta') {
if (tag === 'script') {
innerHTML = (
await transformWithEsbuild(innerHTML, 'inline-script.js', {
minify: true
})
).code.trim()
}
return `${openTag}${innerHTML}</${tag}>`
} else {
return openTag
}
})
.join('\n ')
).then((tags) => tags.join('\n '))
}
function renderAttrs(attrs: Record<string, string>): string {

Loading…
Cancel
Save