Skip to content

Commit

Permalink
Merge pull request KoriIku#34 from boy-lin/main
Browse files Browse the repository at this point in the history
feat: generate open search xml
  • Loading branch information
KoriIku authored Dec 12, 2024
2 parents 5c9b757 + b62420c commit 3b2fcc1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 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

File renamed without changes.
50 changes: 50 additions & 0 deletions scripts/vite-plugin-generate-opensearch.js
Original file line number Diff line number Diff line change
@@ -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 = `<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">\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>";
},
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;
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 3b2fcc1

Please sign in to comment.