Skip to content

Commit

Permalink
feat: support cloudflare deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Nov 19, 2024
1 parent ba733da commit 3a50d66
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 0 deletions.
138 changes: 138 additions & 0 deletions nuxt.cloudflare.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const generateDynamicV = () => {
const now = new Date()
const year = now.getFullYear().toString()
const month = (now.getMonth() + 1).toString().padStart(2, "0")
const day = now.getDate().toString().padStart(2, "0")
const hour = now.getHours().toString().padStart(2, "0")
const minute = now.getMinutes().toString().padStart(2, "0")
return year + month + day + hour + minute
}

const isDev = process.env.NODE_ENV === "development"
const appBase = "/"
const staticV = generateDynamicV()

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
// https://nuxt.com/docs/guide/concepts/typescript#nuxttsconfigjson
typescript: {
strict: true,
},

devtools: {
enabled: false,
},

// build modules
modules: ["@vueuse/nuxt", "@nuxtjs/i18n", "@element-plus/nuxt", "@nuxtjs/color-mode", "@pinia/nuxt", "@nuxt/image"],

// vueuse
vueuse: {
ssrHandlers: true,
},

i18n: {
vueI18n: "./i18n.ts",
},

// colorMode
// 格式是 `class="light-${classSuffix}"`,为空是 `class="light"`
colorMode: {
classSuffix: "",
},

image: {},

vite: {
define: {
"process.env.DEV_MODE": `"${isDev}"`,
"process.env.APP_BASE": `"${appBase}"`,
"process.env.SSR": `"true"`,
},
plugins: [],
css: {
preprocessorOptions: {
scss: {
api: "modern",
silenceDeprecations: ["mixed-decls", "color-functions", "global-builtin", "import", "new-global"],
},
},
},
},

elementPlus: {
icon: "ElIcon",
importStyle: "scss",
themes: ["dark"],
},

// https://github.com/nuxt/nuxt/issues/21840
css: ["~/assets/siyuan/style.styl", "~/assets/siyuan/index.styl"],

app: {
baseURL: appBase,
head: {
charset: "utf-8",
viewport: "width=device-width, initial-scale=1",
htmlAttrs: {
lang: "zh_CN",
"data-theme-mode": "light",
"data-light-theme": "Zhihu",
"data-dark-theme": "Zhihu",
},
link: [
{ rel: "stylesheet", href: appBase + "libs/fonts/webfont.css?v=" + staticV },
{
rel: "stylesheet",
href: appBase + "resources/stage/build/app/base.css?v=" + staticV,
},
{
rel: "stylesheet",
href: appBase + "libs/katex/0.16.10/katex.min.css?v=" + staticV,
crossorigin: "anonymous",
},
],
// https://nuxt.com/docs/api/configuration/nuxt-config#head
script: isDev
? [
{
src: appBase + "libs/eruda/eruda.js",
},
{
children: "eruda.init();console.log('eruda inited');",
},
{
defer: true,
src: appBase + "libs/katex/0.16.10/katex.min.js",
},
]
: [
{
defer: true,
src: appBase + "libs/katex/0.16.10/katex.min.js",
},
],
},
},

nitro: {
preset: "cloudflare_pages",
},

// 环境变量
runtimeConfig: {
siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE,
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL,
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
providerMode: process.env.NUXT_PUBLIC_PROVIDER_MODE,
providerUrl: process.env.NUXT_PUBLIC_PROVIDER_URL,
},
},

compatibilityDate: {
cloudflare: "2024-09-02",
},
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"pluginDev": "zhi-build --watch",
"build": "bash scripts/build.sh",
"nodeBuild": "bash scripts/node.sh",
"cloudflareBuild": "bash scripts/cloudflare.sh",
"vercelBuild": "bash scripts/vercel.sh",
"pluginBuild": "zhi-build --production",
"dockerPackage": "bash scripts/push_docker_image.sh",
Expand Down
Empty file modified scripts/build.sh
100644 → 100755
Empty file.
6 changes: 6 additions & 0 deletions scripts/cloudflare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# 使用 Cloudflare 构建配置
echo "Using Cloudflare build config as SSR build."
cp nuxt.cloudflare.config.ts nuxt.config.ts
nuxt build
Empty file modified scripts/dev.sh
100644 → 100755
Empty file.
Empty file modified scripts/docker.sh
100644 → 100755
Empty file.
Empty file modified scripts/node.sh
100644 → 100755
Empty file.
Empty file modified scripts/push_docker_image.sh
100644 → 100755
Empty file.
Empty file modified scripts/serve.sh
100644 → 100755
Empty file.
Empty file modified scripts/vercel.sh
100644 → 100755
Empty file.

0 comments on commit 3a50d66

Please sign in to comment.