feat: allow plugins to scan the base HTML by using transformIndexHtml

pull/433/head
Maximo Mussini 4 years ago
parent 773472d867
commit a4ef7f3f3c

@ -2,7 +2,7 @@ import ora from 'ora'
import path from 'path' import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import { slash } from '../utils/slash' import { slash } from '../utils/slash'
import { APP_PATH } from '../alias' import { APP_PATH, APP_HTML } from '../alias'
import { SiteConfig } from '../config' import { SiteConfig } from '../config'
import { RollupOutput } from 'rollup' import { RollupOutput } from 'rollup'
import { build, BuildOptions, UserConfig as ViteUserConfig } from 'vite' import { build, BuildOptions, UserConfig as ViteUserConfig } from 'vite'

@ -5,7 +5,8 @@ import {
createMarkdownToVueRenderFn, createMarkdownToVueRenderFn,
MarkdownCompileResult MarkdownCompileResult
} from './markdownToVue' } from './markdownToVue'
import { DIST_CLIENT_PATH, APP_PATH, SITE_DATA_REQUEST_PATH } from './alias' import { DIST_CLIENT_PATH, SITE_DATA_REQUEST_PATH } from './alias'
import { APP_HTML } from './utils/html'
import createVuePlugin from '@vitejs/plugin-vue' import createVuePlugin from '@vitejs/plugin-vue'
import { slash } from './utils/slash' import { slash } from './utils/slash'
import { OutputAsset, OutputChunk } from 'rollup' import { OutputAsset, OutputChunk } from 'rollup'
@ -160,23 +161,15 @@ export function createVitePressPlugin(
// serve our index.html after vite history fallback // serve our index.html after vite history fallback
return () => { return () => {
server.middlewares.use((req, res, next) => { server.middlewares.use(async (req, res, next) => {
if (req.url!.endsWith('.html')) { if (req.url!.endsWith('.html')) {
res.statusCode = 200 res.statusCode = 200
res.end(` const html = await server.transformIndexHtml(
<!DOCTYPE html> req.url!,
<html> APP_HTML,
<head> req.originalUrl
<title></title> )
<meta charset="utf-8"> res.end(html)
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
</head>
<body>
<div id="app"></div>
<script type="module" src="/@fs/${APP_PATH}/index.js"></script>
</body>
</html>`)
return return
} }
next() next()

@ -0,0 +1,16 @@
import { APP_PATH } from '../alias'
export const APP_HTML = `
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
</head>
<body>
<div id="app"></div>
<script type="module" src="/@fs/${APP_PATH}/index.js"></script>
</body>
</html>`
Loading…
Cancel
Save