72 lines
5.8 KiB
JavaScript
72 lines
5.8 KiB
JavaScript
import {defineConfig} from 'vite';
|
|
import vue from '@vitejs/plugin-vue';
|
|
import fs from 'node:fs';
|
|
import {adaptPaymentModal, paymentModalMessages} from './payment-modal.mjs';
|
|
import path from 'node:path';
|
|
import {fileURLToPath} from 'node:url';
|
|
const root = path.dirname(fileURLToPath(import.meta.url));
|
|
const backend = path.resolve(root, '../../..');
|
|
const upstream = path.resolve(root, '../source');
|
|
const upstreamConfig = path.join(upstream, 'src/config/index.js');
|
|
const marker = path.join(backend, 'instances/hn/storage/framework/ez-vite.hot');
|
|
export default defineConfig(({mode}) => ({
|
|
root, envDir: false, base: mode === 'production' ? '/theme/EZ/assets/' : '/__hn_vite/', publicDir: false,
|
|
plugins: [{name:'hn-ez-adapter', enforce:'pre',
|
|
load(id) {
|
|
if (id === upstreamConfig) return `import {config as original} from ${JSON.stringify(upstreamConfig+'?hn-base')}; import {adaptConfig} from ${JSON.stringify(path.join(root,'runtime-config.mjs'))}; export const config=adaptConfig(original,window.settings||{},window.location);`;
|
|
},
|
|
transform(code, id) {
|
|
if (!id.startsWith(upstream + '/src/')) return;
|
|
code = code.replaceAll('src="/images/logo.png"', ':src="hnLogo"').replaceAll('/images/logo.png', '/brand/hn/logo.svg');
|
|
if (id === path.join(upstream,'src/views/shop/Payment.vue')) code = adaptPaymentModal(code);
|
|
const paymentLocale = Object.keys(paymentModalMessages).find(locale => id === path.join(upstream, 'src/i18n/locales', locale + '.js'));
|
|
if (paymentLocale) {
|
|
const [notice, button] = paymentModalMessages[paymentLocale];
|
|
code = code.replace(/(\s+scan_qrcode:)/, `\n open_cashier_notice: ${JSON.stringify(notice)},\n open_cashier_button: ${JSON.stringify(button)},$1`);
|
|
}
|
|
if (id === path.join(upstream,'src/views/shop/Shop.vue')) {
|
|
code = code.replace('v-for="(price, type) in getPlanPrices(plan)"', 'v-for="(price, type) in Object.fromEntries(Object.entries(getPlanPrices(plan)).filter(([, price]) => price !== null))"');
|
|
}
|
|
if (id === path.join(upstream,'src/i18n/locales/zh-CN.js')) {
|
|
code = code.replaceAll('商店', '购买套餐')
|
|
.replace("welcome: '欢迎回来'", "welcome: '欢迎来到 hardnerd'")
|
|
.replace("siteAnnouncement: '网站公告'", "siteAnnouncement: '公告'")
|
|
.replace("welcomeDesc: '查看您的服务和使用情况'", `welcomeDesc: ${JSON.stringify("务必,请收藏我们的邮箱地址:mail{'@'}hardnerd.dev,当前域名被墙时,可以发送任意邮件至该邮箱,将自动回复最新地址。")}`);
|
|
}
|
|
if (id === path.join(upstream,'src/views/dashboard/Dashboard.vue')) {
|
|
code = code.replace(`<p class="">{{ $t('dashboard.welcomeDesc') }}</p>`, '<HnWelcomeNotice />')
|
|
.replace('<script>', `<script>\nimport HnWelcomeNotice from ${JSON.stringify(path.join(root,'components/HnWelcomeNotice.vue'))};`)
|
|
.replace('components: {', 'components: { HnWelcomeNotice,');
|
|
code += '\n<style scoped>\n.stats-card.doc-card::after { content: none; animation: none; }\n</style>\n';
|
|
}
|
|
if (id === path.join(upstream,'src/components/common/Toast.vue')) {
|
|
code += '\n<style scoped>\n.toast.toast-info { --info-color: var(--theme-color, #8bcf36); }\n</style>\n';
|
|
}
|
|
if (id === path.join(upstream,'src/components/common/LanguageSelector.vue')) {
|
|
code = code.replace('<span :key="currentLanguage" class="flag-container" v-html="getCurrentLanguageFlag()"></span>', `<span :key="currentLanguage" class="hn-language-code" style="font-size:13px;font-weight:700;letter-spacing:.5px">{{ ({'zh-CN':'CN','zh-TW':'TW','en-US':'EN','ja-JP':'JP','ko-KR':'KR','ru-RU':'RU','fa-IR':'IR','vi-VN':'VN'})[currentLanguage] || 'EN' }}</span>`);
|
|
code = code.replace('<style', `<style>
|
|
.language-btn.language-btn { transition: all .3s ease; }
|
|
.language-btn .hn-language-code { color: inherit; }
|
|
.language-btn.language-btn:hover, .language-btn.language-btn:focus-visible { color: var(--theme-color, #a3cc4b); }
|
|
.language-btn.language-btn:active { transform: translateY(0); transition: all .1s ease; }
|
|
</style>
|
|
<style`);
|
|
}
|
|
if (id === path.join(upstream,'src/appInit.js')) code = code.replace("app.mount('#app');", "app.config.globalProperties.hnLogo = window.settings?.logo || '/brand/hn/logo.svg'; app.mount('#app');");
|
|
if (id === path.join(upstream,'src/api/user.js')) code = code.replace(/export function getIpLocationInfo\(\)\s*\{[\s\S]*?\n\}/, 'export function getIpLocationInfo() { return Promise.resolve({data: null}); }');
|
|
return code;
|
|
},
|
|
configureServer(server) {
|
|
server.httpServer?.once('listening', () => {fs.mkdirSync(path.dirname(marker), {recursive:true}); fs.writeFileSync(marker,'http://hn.loc/__hn_vite');});
|
|
server.httpServer?.once('close', () => {if(fs.existsSync(marker))fs.unlinkSync(marker);});
|
|
}
|
|
}, vue({template:{transformAssetUrls:{includeAbsolute:false}}})],
|
|
resolve:{alias:{'@':path.join(upstream,'src')}, dedupe:['vue']},
|
|
define:{'process.env':{NODE_ENV:mode,VUE_APP_CONFIGJS:'false',VUE_APP_DEBUGGING:'false'},__VUE_OPTIONS_API__:true,__VUE_PROD_DEVTOOLS__:false,__VUE_PROD_HYDRATION_MISMATCH_DETAILS__:false},
|
|
css:{preprocessorOptions:{scss:{additionalData:'@use "@/assets/styles/base/variables.scss" as *;'}}},
|
|
optimizeDeps:{include:['axios','vue','vue-router','pinia','vue-i18n']},
|
|
build:{outDir:path.join(backend,'theme/EZ/assets'),assetsDir:'chunks',manifest:true,emptyOutDir:true,sourcemap:false},
|
|
server:{host:'127.0.0.1',port:5184,strictPort:true,origin:'http://hn.loc',allowedHosts:['hn.loc'],hmr:{host:'hn.loc',clientPort:80},cors:{origin:/^http:\/\/(127\.0\.0\.1|localhost|hn\.loc)(:\d+)?$/},
|
|
fs:{allow:[path.resolve(root,'../..')],deny:['**/.env','**/.env.*','**/.git','**/.git/**','**/.npmrc']},proxy:{'/api':'http://127.0.0.1:8592','/brand':'http://127.0.0.1:8592'}}
|
|
}));
|