dpl/frontends/hn/vite.config.mjs
2026-10-28 15:49:13 +08:00

41 lines
3.2 KiB
JavaScript

import {defineConfig} from 'vite';
import vue from '@vitejs/plugin-vue';
import fs from 'node:fs';
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, '../ez-theme');
const upstreamConfig = path.join(upstream, 'src/config/index.js');
const marker = path.join(backend, '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/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('商店', '购买套餐');
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'}}
}));