mirror of
https://gitee.com/bootx/dax-pay-ui.git
synced 2025-09-09 13:40:06 +00:00
refactor: add vite-plugin-html. Delete updateHtml related logic
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
// 百度统计代码 用于站点部署
|
||||
// 只在build:site开启
|
||||
export const hmScript = `<script>
|
||||
var _hmt = _hmt || [];
|
||||
(function() {
|
||||
var hm = document.createElement("script");
|
||||
hm.src = "https://hm.baidu.com/hm.js?384d6046e02f6ac4ea075357bd0e9b43";
|
||||
var s = document.getElementsByTagName("script")[0];
|
||||
s.parentNode.insertBefore(hm, s);
|
||||
})();
|
||||
</script>
|
||||
`;
|
@@ -4,15 +4,15 @@ import { sh } from 'tasksfile';
|
||||
|
||||
import { argv } from 'yargs';
|
||||
import { runBuildConfig } from './buildConf';
|
||||
import { runUpdateHtml } from './updateHtml';
|
||||
// import { runUpdateHtml } from './updateHtml';
|
||||
import { errorConsole, successConsole } from '../utils';
|
||||
import { startGzipStyle } from '../plugin/gzip/compress';
|
||||
import { startGzipStyle } from '../vite/plugin/gzip/compress';
|
||||
|
||||
export const runBuild = async (preview = false) => {
|
||||
try {
|
||||
const argvList = argv._;
|
||||
if (preview) {
|
||||
let cmd = `npm run build`;
|
||||
let cmd = `cross-env NODE_ENV=production vite build`;
|
||||
await sh(cmd, {
|
||||
async: true,
|
||||
nopipe: true,
|
||||
@@ -23,7 +23,7 @@ export const runBuild = async (preview = false) => {
|
||||
if (!argvList.includes('no-conf')) {
|
||||
await runBuildConfig();
|
||||
}
|
||||
await runUpdateHtml();
|
||||
// await runUpdateHtml();
|
||||
if (!preview) {
|
||||
await startGzipStyle();
|
||||
}
|
||||
|
@@ -1,103 +0,0 @@
|
||||
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}</title>`);
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
function injectConfigScript(html: string) {
|
||||
const tag = `\t\t<script src='${viteConfig.base || './'}${GLOB_CONFIG_FILE_NAME}?v=${
|
||||
pkg.version
|
||||
}-${new Date().getTime()}'></script>`;
|
||||
|
||||
if (/<\/head>/.test(html)) {
|
||||
return html.replace(/<\/head>/, `${tag}\n\t\t</head>`);
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
function injectHmScript(html: string) {
|
||||
if (/<head>/.test(html)) {
|
||||
return html.replace(/<head>/, `<head>\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 += `<link rel="stylesheet" href="${cssLink}">`;
|
||||
}
|
||||
if (/<\/head>/.test(html)) {
|
||||
return html.replace(/<\/head>/, `${cdnCssTag}\n\t\t</head>`);
|
||||
}
|
||||
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
|
||||
// <script type="importmap">
|
||||
// { "imports": {
|
||||
// "vue": "https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0/vue.esm-browser.js",
|
||||
// "vue-router": "https://cdnjs.cloudflare.com/ajax/libs/vue-router/4.0.0-alpha.13/vue-router.esm.js",
|
||||
// "vuex": "https://cdnjs.cloudflare.com/ajax/libs/vuex/4.0.0-beta.2/vuex.esm-browser.js"
|
||||
// } }
|
||||
// </script>
|
||||
cdnJsTag += `\t<script type="text/javascript" src="${src}"></script>\n`;
|
||||
}
|
||||
if (/<\/body>/.test(html)) {
|
||||
return html.replace(/<\/body>/, `${cdnJsTag}\n</body>`);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user