forked from yunyuyuan/nuxt3-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuxt.config.ts
166 lines (158 loc) · 4.6 KB
/
nuxt.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import path from "path";
import fs from "fs";
import { execSync } from "child_process";
import { generateSiteMap, generateTimestamp } from "./scripts/generate";
import config from "./config";
import { allPlugins, buildPlugins } from "./vite-plugins";
const isDev = process.env.NODE_ENV === "development";
// themeColor
fs.writeFileSync("./assets/style/_theme.scss", `$theme: ${config.themeColor};`);
// stickers
const stickers: Record<string, any> = {};
fs.readdirSync("./public/sticker").forEach((dir) => {
stickers[dir] = fs.readdirSync(`./public/sticker/${dir}`).length;
});
// svgs
const svgs = fs
.readdirSync("./assets/svg")
.map(file => file.replace(/\.svg$/, ""));
const scripts = [];
const cfAnalyzeId = config.CloudflareAnalyze || process.env.CloudflareAnalyze;
const msAnalyzeId = config.MSClarityId || process.env.MSClarityId;
if (cfAnalyzeId && !isDev) {
scripts.push({
src: "https://static.cloudflareinsights.com/beacon.min.js",
async: false,
defer: true,
"data-cf-beacon": `{"token": "${cfAnalyzeId}"}`
});
}
if (msAnalyzeId && !isDev) {
scripts.push({
children: `(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);})(window, document, "clarity", "script", "${msAnalyzeId}");`
});
}
const timestamp = Date.now();
let githubBranch = "main";
for (const b of [
// vercel
process.env.VERCEL_GIT_COMMIT_REF,
// cloudflare page
process.env.CF_PAGES_BRANCH,
// netlify
process.env.BRANCH,
// XXX Why `git rev-parse --abbrev-ref HEAD` not works as expected?
// git cli for fallback
execSync("git rev-parse --abbrev-ref HEAD").toString().trim()
]) {
if (b) {
githubBranch = b;
break;
}
}
// const prefix = "monaco-editor/esm/vs";
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
telemetry: false,
app: {
head: {
meta: [
{ charset: "utf-8" },
{
name: "viewport",
content:
"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"
},
{ name: "keywords", content: config.SEO_keywords },
{ name: "author", content: config.nickName }
],
link: [{ rel: "shortcut icon", href: "/icon.png" }],
script: scripts,
title: config.title
}
},
css: [
"~/assets/style/main.scss",
"~/node_modules/katex/dist/katex.min.css",
"~/node_modules/viewerjs/dist/viewer.css"
],
runtimeConfig: {
public: {
stickers,
svgs: isDev ? svgs : [],
timestamp
},
app: {
NUXT_ENV_CURRENT_GIT_SHA: execSync("git rev-parse HEAD")
.toString()
.trim(),
githubBranch,
mongoDBEnabled: !!process.env.MONGODB_URI || !!process.env.MONGODB_USER,
cmtRepId: config.CommentRepoId || process.env.CommentRepoId,
cmtRepCateId:
config.CommentDiscussionCategoryId ||
process.env.CommentDiscussionCategoryId
}
},
nitro: {
prerender: {
crawlLinks: true,
failOnError: false,
ignore: ["/manage"]
},
cloudflare: {
pages: {
routes: {
include: ["/api/*"]
}
}
}
},
experimental: {
/**
* Need payload to cache the data from useAsyncData, but why?
* https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/plugins/payload.client.ts#L27
*/
// payloadExtraction: false
inlineSSRStyles: false
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
vite: {
plugins: isDev ? allPlugins : buildPlugins,
css: {
preprocessorOptions: {
scss: {
additionalData: "@use 'sass:math';@import 'assets/style/var';"
}
}
},
build: {
// minify: false
}
},
hooks: {
"vite:extendConfig" (config, { isClient }) {
if (isClient) {
(config.build?.rollupOptions?.output as any).manualChunks = {
// markdown: ["highlight.js", "katex", "marked"]
};
}
},
"nitro:build:before" (nitro) {
const apiPath = path.join(__dirname, "utils", "api");
if (["node-server"].includes(nitro.options.preset)) {
for (const file of fs.readdirSync(path.join(apiPath, "db-tcp"))) {
fs.renameSync(
path.join(apiPath, "db-tcp", file),
path.join(apiPath, "db", file)
);
}
}
},
"nitro:build:public-assets" (nitro) {
generateSiteMap(nitro.options.output.publicDir);
generateTimestamp(nitro.options.output.publicDir);
}
}
});