-
Notifications
You must be signed in to change notification settings - Fork 82
/
nuxt.config.js
156 lines (138 loc) · 4.17 KB
/
nuxt.config.js
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
import path from "path";
import config from "./config";
import * as fs from "fs";
import dayjs, {getNowDayjs} from "./utils/_dayjs";
import { execSync } from "child_process";
export default {
// Target: https://go.nuxtjs.dev/config-target
telemetry: false,
target: 'static',
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
meta: [
{charset: 'utf-8'},
{
name: 'viewport',
content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no'
},
{name: 'description', content: config.SEO_description},
{name: 'keywords', content: config.SEO_keywords}
],
link: [
{rel: 'shortcut icon', href: '/favicon.jpg'},
],
script: [...(config.CloudflareAnalyze ?
[{
src: 'https://static.cloudflareinsights.com/beacon.min.js',
async: false,
defer: true,
'data-cf-beacon': `{"token": "${config.CloudflareAnalyze}"}`
}] : [])
],
title: config.title
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
'~/assets/style/main.scss'
],
env: {
NUXT_ENV_CURRENT_GIT_SHA: execSync('git rev-parse --short HEAD').toString().trim()
},
loading: {
color: '#00a5d7',
height: '2px'
},
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
{src: '~/plugins/filters'},
{src: '~/plugins/viewer'},
{src: '~/plugins/global-comp'},
{src: '~/plugins/stickers.server'},
{src: '~/plugins/logme.client'},
],
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [],
hooks: {
generate: {
done() {
fs.writeFileSync(path.resolve(__dirname, './dist/sitemap.xml'),
genRss(JSON.parse(fs.readFileSync(path.resolve(__dirname, './rebuild/json/article.json')).toString())));
fs.writeFileSync(path.resolve(__dirname, './dist/timestamp.txt'), getNowDayjs().format('YYYY-MM-DD HH:mm:ss'));
console.log('√ sitemap.xml generated');
}
}
},
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config, {isDev, isClient}) {
if (isClient) {
config.node = {
// 跳过dependence检查
fs: 'empty',
}
}
const svgFolder = path.resolve(__dirname, 'assets/svg');
const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
svgRule.exclude = [svgFolder]
config.module
.rules.push({
test: /\.svg$/,
loader: 'svg-sprite-loader',
include: [svgFolder],
options: {
symbolId: 'icon-[name]'
}
})
}
},
}
export function escapeHtml(s) {
return s.toString()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
class Node {
tag;
content;
children = [];
constructor(tag, content) {
this.tag = tag;
this.content = content;
}
addChild(node) {
this.children.push(node)
}
toString() {
if (this.children.length) {
return `<${this.tag}>${
this.children.map(node => node.toString()).join('\n')
}</${this.tag}>`
}
return `<${this.tag}>${escapeHtml(this.content)}</${this.tag}>`
}
}
function genRss(json) {
const origin = config.domain,
startStr = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">',
endStr = '</rss>';
const channel = new Node('channel');
channel.addChild(new Node('title', config.title))
channel.addChild(new Node('link', origin))
channel.addChild(new Node('description', config.SEO_description))
channel.addChild(new Node('language', 'zh-cn'))
for (const i of json.filter(i => !i.encrypt)) {
const item = new Node('item');
item.addChild(new Node('author', config.githubName));
item.addChild(new Node('title', i.title));
item.addChild(new Node('link', origin + '/articles/' + i.id));
item.addChild(new Node('pubDate', dayjs.utc(i.time).toString()));
item.addChild(new Node('guid', i.id));
channel.addChild(item);
}
return `${startStr}${channel.toString()}${endStr}`;
}