import { readFileSync, writeFileSync, existsSync } from 'fs-extra'; import viteConfig, { htmlConfig } from '../../vite.config'; import { getCwdPath, successConsole, errorConsole } from '../utils'; import { GLOB_CONFIG_FILE_NAME } from '../constant'; import { hmScript } from './hm'; import HtmlMinifier from 'html-minifier'; const pkg = require('../../package.json'); const { title, addHm, cdnConf, useCdn, minify } = htmlConfig; function injectTitle(html: string, htmlTitle: string) { if (/<\/title>/.test(html)) { return html.replace(/<\/title>/, `${htmlTitle}`); } return html; } function injectConfigScript(html: string) { const tag = `\t\t`; if (/<\/head>/.test(html)) { return html.replace(/<\/head>/, `${tag}\n\t\t`); } return html; } function injectHmScript(html: string) { if (//.test(html)) { return html.replace(//, `\n${hmScript}`); } return html; } function injectCdnCss(html: string) { if (!cdnConf) return html; const { css } = cdnConf; if (!css || css.length === 0) return html; let cdnCssTag = ''; for (const cssLink of css) { cdnCssTag += ``; } if (/<\/head>/.test(html)) { return html.replace(/<\/head>/, `${cdnCssTag}\n\t\t`); } return html; } function injectCdnjs(html: string) { if (!cdnConf) return html; const { js } = cdnConf; if (!js || js.length === 0) return html; let cdnJsTag = ''; for (const src of js) { // TODO // cdnJsTag += `\t\n`; } if (/<\/body>/.test(html)) { return html.replace(/<\/body>/, `${cdnJsTag}\n`); } return html; } export async function runUpdateHtml() { const outDir = viteConfig.outDir || 'dist'; const indexPath = getCwdPath(outDir, 'index.html'); if (!existsSync(indexPath)) return; try { let processedHtml = ''; const rawHtml = readFileSync(indexPath, 'utf-8'); processedHtml = rawHtml; processedHtml = injectTitle(processedHtml, title); processedHtml = injectConfigScript(processedHtml); if (addHm) { processedHtml = injectHmScript(processedHtml); } if (useCdn) { processedHtml = injectCdnCss(processedHtml); processedHtml = injectCdnjs(processedHtml); } if (minify) { const { enable, ...miniOpt } = minify; if (enable) { processedHtml = HtmlMinifier.minify(processedHtml, miniOpt); } } writeFileSync(indexPath, processedHtml); successConsole('Update Html Successfully!'); } catch (error) { errorConsole('Update Html Error\n' + error); } }