This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathclient.ts
111 lines (94 loc) · 3.22 KB
/
client.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
import querystring from 'node:querystring'
import { resolve } from 'pathe'
import webpack from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import { logger } from '@nuxt/kit'
import { joinURL } from 'ufo'
import ForkTSCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import type { WebpackConfigContext } from '../utils/config'
import { applyPresets } from '../utils/config'
import { nuxt } from '../presets/nuxt'
export function client (ctx: WebpackConfigContext) {
ctx.name = 'client'
ctx.isClient = true
applyPresets(ctx, [
nuxt,
clientPlugins,
clientOptimization,
clientDevtool,
clientPerformance,
clientHMR
])
}
function clientDevtool (ctx: WebpackConfigContext) {
if (!ctx.nuxt.options.sourcemap.client) {
ctx.config.devtool = false
return
}
if (!ctx.isDev) {
ctx.config.devtool = 'source-map'
return
}
ctx.config.devtool = 'eval-cheap-module-source-map'
}
function clientPerformance (ctx: WebpackConfigContext) {
ctx.config.performance = {
maxEntrypointSize: 1000 * 1024,
hints: ctx.isDev ? false : 'warning',
...ctx.config.performance
}
}
function clientHMR (ctx: WebpackConfigContext) {
const { options, config } = ctx
if (!ctx.isDev) {
return
}
const clientOptions = options.webpack.hotMiddleware?.client || {}
const hotMiddlewareClientOptions = {
reload: true,
timeout: 30000,
path: joinURL(options.app.baseURL, '__webpack_hmr', ctx.name),
...clientOptions,
ansiColors: JSON.stringify(clientOptions.ansiColors || {}),
overlayStyles: JSON.stringify(clientOptions.overlayStyles || {}),
name: ctx.name
}
const hotMiddlewareClientOptionsStr = querystring.stringify(hotMiddlewareClientOptions)
// Add HMR support
const app = (config.entry as any).app as any
app.unshift(
// https://github.com/glenjamin/webpack-hot-middleware#config
`webpack-hot-middleware/client?${hotMiddlewareClientOptionsStr}`
)
config.plugins = config.plugins || []
config.plugins.push(new webpack.HotModuleReplacementPlugin())
}
function clientOptimization (_ctx: WebpackConfigContext) {
// TODO: Improve optimization.splitChunks.cacheGroups
}
function clientPlugins (ctx: WebpackConfigContext) {
const { options, config } = ctx
// webpack Bundle Analyzer
// https://github.com/webpack-contrib/webpack-bundle-analyzer
if (!ctx.isDev && ctx.name === 'client' && options.webpack.analyze) {
const statsDir = resolve(options.buildDir, 'stats')
config.plugins!.push(new BundleAnalyzerPlugin({
analyzerMode: 'static',
defaultSizes: 'gzip',
generateStatsFile: true,
openAnalyzer: true,
reportFilename: resolve(statsDir, `${ctx.name}.html`),
statsFilename: resolve(statsDir, `${ctx.name}.json`),
...options.webpack.analyze === true ? {} : options.webpack.analyze
}))
}
// Normally type checking runs in server config, but in `ssr: false` there is
// no server build, so we inject here instead.
if (!ctx.nuxt.options.ssr) {
if (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev)) {
config.plugins!.push(new ForkTSCheckerWebpackPlugin({
logger
}))
}
}
}