From f74404373497b18cf71675e50836ae2937536db6 Mon Sep 17 00:00:00 2001 From: zhangchuanlong Date: Tue, 25 Jan 2022 15:25:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=89=93=E5=8C=85=E6=97=B6=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?CNAME?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 5 ++++- build/script/build-cname.js | 14 ++++++++++++++ build/script/index.js | 2 ++ build/utils.js | 10 ++++++---- 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 build/script/build-cname.js diff --git a/.env b/.env index 6495fcd..398a1f4 100644 --- a/.env +++ b/.env @@ -1,3 +1,6 @@ VITE_APP_TITLE = 'Vue Naive Admin' -VITE_PORT = 3100 \ No newline at end of file +VITE_PORT = 3100 + +# 打包时自动生成CNAME文件,用于配置github pages自定义域名,如不需要可注释或者直接删除 +VITE_APP_GLOB_CNAME = 'template.qszone.com' \ No newline at end of file diff --git a/build/script/build-cname.js b/build/script/build-cname.js new file mode 100644 index 0000000..dbcbc2e --- /dev/null +++ b/build/script/build-cname.js @@ -0,0 +1,14 @@ +import chalk from 'chalk' +import { writeFileSync } from 'fs-extra' +import { OUTPUT_DIR } from '../constant' +import { getEnvConfig, getRootPath } from '../utils' + +export function runBuildCNAME() { + const { VITE_APP_GLOB_CNAME } = getEnvConfig() + if (!VITE_APP_GLOB_CNAME) return + try { + writeFileSync(getRootPath(`${OUTPUT_DIR}/CNAME`), VITE_APP_GLOB_CNAME) + } catch (error) { + console.log(chalk.red('CNAME file failed to package:\n' + error)) + } +} diff --git a/build/script/index.js b/build/script/index.js index 4f71808..c208e25 100644 --- a/build/script/index.js +++ b/build/script/index.js @@ -1,9 +1,11 @@ import chalk from 'chalk' import { runBuildConfig } from './build-config' +import { runBuildCNAME } from './build-cname' export const runBuild = async () => { try { runBuildConfig() + runBuildCNAME() console.log(`✨ ${chalk.cyan('build successfully!')}`) } catch (error) { console.log(chalk.red('vite build error:\n' + error)) diff --git a/build/utils.js b/build/utils.js index 39ebabf..178e594 100644 --- a/build/utils.js +++ b/build/utils.js @@ -40,17 +40,19 @@ function getConfFiles() { const result = reg.exec(script) if (result) { const mode = result[1] - return ['.env', `.env.${mode}`] + return ['.env', '.env.local', `.env.${mode}`] } - return ['.env', '.env.production'] + return ['.env', '.env.local', '.env.production'] } export function getEnvConfig(match = 'VITE_APP_GLOB_', confFiles = getConfFiles()) { let envConfig = {} confFiles.forEach((item) => { try { - const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item))) - envConfig = { ...envConfig, ...env } + if (fs.existsSync(path.resolve(process.cwd(), item))) { + const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item))) + envConfig = { ...envConfig, ...env } + } } catch (e) { console.error(`Error in parsing ${item}`, e) }