Skip to content

Commit

Permalink
feat: generate open search xml
Browse files Browse the repository at this point in the history
  • Loading branch information
boy-lin committed Dec 11, 2024
1 parent 5c9b757 commit 7978293
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -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

8 changes: 0 additions & 8 deletions public/opensearch.xml

This file was deleted.

44 changes: 44 additions & 0 deletions scripts/vite-plugin-generate-opensearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from "fs";
import path from "path";

/**
* 跟具配置生成 OpenSearch 描述文件
* OpenSearch 描述文件 https://developer.mozilla.org/zh-CN/docs/Web/OpenSearch
* @returns
*/
function vitePluginGenerateOpensearch() {
let config;
return {
name: "vite-plugin-generate-opensearch",
configResolved(resolvedConfig) {
// 存储最终解析的配置
config = resolvedConfig;
},
// 在Vite构建结束后执行的钩子函数
closeBundle() {
// if (process.env.NODE_ENV !== "production") return;
const outDir = path.resolve(config.build.outDir);
const shortName = process.env.VITE_OPEN_SEARCH_ShortName;
const urlTemplateBase = process.env.VITE_OPEN_SEARCH_UrlTemplateBase;

// console.log(" this.meta", outDir);
try {
let xml = "<OpenSearchDescription>\n";
xml += `<ShortName>${shortName}</ShortName>\n`;
xml += `<Description>${shortName}</Description>\n`;
xml += "<InputEncoding>UTF-8</InputEncoding>\n";
xml +=
'<Image width="16" height="16" type="image/x-icon">/favicon.ico</Image>\n';
xml += `<Url type="text/html" method="get" template="${urlTemplateBase}/search?q={searchTerms}"/>\n`;
xml += "<moz:SearchForm>/search</moz:SearchForm>\n";
xml += "</OpenSearchDescription>";
const xmlFilePath = path.join(outDir, "opensearch.xml");
fs.writeFileSync(xmlFilePath, xml);
} catch (error) {
console.error("writing opensearch.xml error", error);
}
},
};
}

export default vitePluginGenerateOpensearch;
36 changes: 21 additions & 15 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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)),
},
},
});
};

0 comments on commit 7978293

Please sign in to comment.