diff --git a/.env b/.env index d7d1402..2f7af05 100644 --- a/.env +++ b/.env @@ -1,2 +1,7 @@ # .env VITE_GOOGLE_CSE_CX=d0753b9ad66c34097 +VITE_BASE_URL='./' +# OpenSearch 描述文件 +VITE_OPEN_SEARCH_ShortName=Luxirty Search +VITE_OPEN_SEARCH_UrlTemplateBase=https://search.luxirty.com + diff --git a/public/opensearch.xml b/public/opensearch.bak.xml similarity index 100% rename from public/opensearch.xml rename to public/opensearch.bak.xml diff --git a/scripts/vite-plugin-generate-opensearch.js b/scripts/vite-plugin-generate-opensearch.js new file mode 100644 index 0000000..6820173 --- /dev/null +++ b/scripts/vite-plugin-generate-opensearch.js @@ -0,0 +1,50 @@ +/** + * 跟具配置生成 OpenSearch 描述文件 + * OpenSearch 描述文件 https://developer.mozilla.org/zh-CN/docs/Web/OpenSearch + * @returns + */ +function vitePluginGenerateOpensearch() { + let xml = ""; + + return { + name: "vite-plugin-generate-opensearch", + configResolved() { + const shortName = process.env.VITE_OPEN_SEARCH_ShortName; + const urlTemplateBase = process.env.VITE_OPEN_SEARCH_UrlTemplateBase; + xml = `\n`; + xml += `${shortName}\n`; + xml += `${shortName}\n`; + xml += "UTF-8\n"; + xml += + '/favicon.ico\n'; + xml += `\n`; + xml += "/search\n"; + xml += ""; + }, + configureServer(server) { + // dev,开发服务器时返回 opensearch.xml + return () => { + server.middlewares.use((req, res, next) => { + if (req.originalUrl === "/opensearch.xml") { + res.setHeader("Content-Type", "text/xml"); + res.end(xml); + } else { + next(); + } + }); + }; + }, + // 构建结束后执行的钩子函数 + generateBundle() { + this.emitFile({ + type: "asset", + fileName: "opensearch.xml", + source: xml, + }); + }, + + getXmlContent() {}, + }; +} + +export default vitePluginGenerateOpensearch; diff --git a/vite.config.js b/vite.config.js index 5c45e1d..cafabfa 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,16 +1,22 @@ -import { fileURLToPath, URL } from 'node:url' +import { fileURLToPath, URL } from "node:url"; +import { defineConfig, loadEnv } from "vite"; +import vue from "@vitejs/plugin-vue"; +import vitePluginGenerateOpensearch from "./scripts/vite-plugin-generate-opensearch"; -import { defineConfig } from 'vite' -import vue from '@vitejs/plugin-vue' - -// https://vitejs.dev/config/ -export default defineConfig({ - plugins: [ - vue(), - ], - resolve: { - alias: { - '@': fileURLToPath(new URL('./src', import.meta.url)) - } - } -}) +export default ({ mode, command, ssrBuild, isSsrBuild }) => { + process.env = { + ...process.env, + ...loadEnv(mode, process.cwd()), + }; + // console.log('process.env', mode, process.env) + // https://vitejs.dev/config/ + return defineConfig({ + // base: process.env.VITE_BASE_URL, + plugins: [vue(), vitePluginGenerateOpensearch()], + resolve: { + alias: { + "@": fileURLToPath(new URL("./src", import.meta.url)), + }, + }, + }); +};