This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
vue.config.js
197 lines (189 loc) · 7.77 KB
/
vue.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const path = require('node:path')
const process = require('node:process')
const SWPrecachePlugin = require('sw-precache-webpack-plugin')
const { createProxyMiddleware } = require('http-proxy-middleware')
const port = process.env.NODE_ENV === 'development' ? 18080 : 8080
console.log(port)
module.exports = {
configureWebpack: {
devtool: 'source-map',
performance: {
hints: 'warning', // 枚举
maxAssetSize: 30000000, // 整数类型(以字节为单位)
maxEntrypointSize: 50000000, // 整数类型(以字节为单位)
assetFilter(assetFilename) {
// 提供资源文件名的断言函数
return assetFilename.endsWith('.css') || assetFilename.endsWith('.js')
},
},
plugins: [
new SWPrecachePlugin({
cacheId: 'mmf-blog-vue2-pwa-ssr',
filename: 'service-worker.js',
minify: true,
dontCacheBustUrlsMatching: /./,
staticFileGlobsIgnorePatterns: [/\.html$/, /\.map$/, /\.json$/],
runtimeCaching: [
{
urlPattern: /api/,
handler: 'networkFirst',
options: {
networkTimeoutSeconds: 1,
cacheName: 'api-cache',
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /^https:\/\/cdn\.jsdelivr\.net/,
handler: 'networkFirst',
options: {
networkTimeoutSeconds: 1,
cacheName: 'cdn-cache',
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /^https:\/\/fdn\.geekzu\.org/,
handler: 'NetworkFirst',
options: {
networkTimeoutSeconds: 1,
cacheName: 'avatar-cache',
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
}),
],
},
chainWebpack: (config) => {
const htmlSsrPlugin = config.plugins.get('html-ssr')
if (htmlSsrPlugin)
htmlSsrPlugin.store.get('args')[0].chunks = []
config.module
.rule('vue')
.use('vue-loader')
.tap((options) => {
options.compilerOptions.preserveWhitespace = true
return options
})
config.module.rule('eslint').uses.clear()
config.module.rule('eslint').clear()
config.resolve.alias.set('~', path.resolve('src'))
if (process.env.VUE_CLI_SSR_TARGET === 'client')
config.resolve.alias.set('~api', path.resolve('src/api/index-client.js'))
else
config.resolve.alias.set('~api', path.resolve('src/api/index-server.js'))
},
css: {
loaderOptions: {},
},
pluginOptions: {
ssr: {
// ===== Listening port for `serve` command
port,
// ===== Listening host for `serve` command
host: null,
// ===== 指定公共文件路径以禁用资源预取提示
// shouldNotPrefetch: [],
// ===== 指定公共文件路径以禁用资源预加载提示
// shouldNotPreload: [],
// ===== Entry for each target
entry: target => `./src/entry-${target}`,
// ===== 默认的标题
defaultTitle: 'M.M.F 小屋',
// ===== icon 路径
// favicon: './static/img/icons/favicon.ico',
// ===== Enable Critical CSS
// criticalCSS: true,
// ===== 跳过服务器端渲染的一些请求
skipRequests: (req) => {
return req.originalUrl.includes('/css/') || req.originalUrl.includes('/js/')
},
// ===== See https://ssr.vuejs.org/guide/build-config.html#externals-caveats
nodeExternalsWhitelist: [/\.css$/, /\?vue&type=style/],
// ===== 为生产服务器启用节点集群
// clustered: false,
// ===== 静态文件缓存控制maxAge值
// staticCacheTtl: 1000 * 60 * 60 * 24 * 30,
// ===== 指令回调
directives: {
// See 'Directive' chapter
},
// ===== See https://ssr.vuejs.org/guide/caching.html
lruCacheOptions: {},
// ===== 应用默认的中间件,如压缩,提供静态文件
// applyDefaultServer: true,
// ===== 扩展应用程序上下文对象
// extendContext: (req, res, process) => ({ appMode: process.env.APP_MODE }),
// ===== 连接自定义中间件
extendServer: (app) => {
const logger = require('morgan')
app.use(
logger('[:remote-addr] ":method :url" :status :res[content-length] ":referrer" ":date[web]"', {
skip(req) {
return req.url.includes('.map')
},
}),
)
const express = require('express')
app.set('views', path.join(__dirname, 'dist'))
app.engine('.html', require('ejs').__express)
app.set('view engine', 'ejs')
// 反向代理 => 4000端口
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:4000',
changeOrigin: true,
pathRewrite: {
'^/api': '/api',
},
}),
)
// parse application/json
app.use(express.json())
// parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }))
const cookieParser = require('cookie-parser')
app.use(cookieParser())
},
// ===== 在启动时将URL复制到系统剪贴板
// copyUrlOnStart: true,
// ==== 在渲染完成后调用
// onRender: (res, context) => {
// res.setHeader(`Cache-Control', 'public, max-age=${context.maxAge}`)
// },
// ==== 发送到错误监控服务
// onError: error => {},
// ===== Paths
distPath: path.resolve(__dirname, './dist'),
error500Html: null,
templatePath: path.resolve(__dirname, './dist/index.ssr.html'),
serviceWorkerPath: path.resolve(__dirname, './dist/service-worker.js'),
},
},
pwa: {
name: 'M.M.F小屋',
themeColor: '#54d9e0',
msTileColor: '#000000',
appleMobileWebAppCapable: 'yes',
appleMobileWebAppStatusBarStyle: 'black',
manifestPath: 'static/manifest.json',
manifestOptions: {
start_url: '/',
},
iconPaths: {
favicon32: 'static/img/icons/favicon-32x32.png',
favicon16: 'static/img/icons/favicon-16x16.png',
appleTouchIcon: 'static/img/icons/apple-touch-icon-152x152.png',
maskIcon: 'static/img/icons/safari-pinned-tab.svg',
msTileImage: 'static/img/icons/msapplication-icon-144x144.png',
},
},
}