forked from KoriIku/luxirty-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request KoriIku#34 from boy-lin/main
feat: generate open search xml
- Loading branch information
Showing
4 changed files
with
76 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
}, | ||
}, | ||
}); | ||
}; |