diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 8dff7daf..8ef0d400 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -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', diff --git a/src/node/build/render.ts b/src/node/build/render.ts index ae851343..8bfe54d5 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -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)}
${content}
@@ -165,17 +165,24 @@ function resolvePageImports( ] } -function renderHead(head: HeadConfig[]) { - return head - .map(([tag, attrs = {}, innerHTML = '']) => { +function renderHead(head: HeadConfig[]): Promise { + 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}` } else { return openTag } }) - .join('\n ') + ).then((tags) => tags.join('\n ')) } function renderAttrs(attrs: Record): string {