-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
23 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 | ||
|
This file was deleted.
Oops, something went wrong.
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,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; |
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)), | ||
}, | ||
}, | ||
}); | ||
}; |