-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
build.ts
205 lines (196 loc) · 6.44 KB
/
build.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
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
198
199
200
201
202
203
204
205
import { defineUntypedSchema } from 'untyped'
import { defu } from 'defu'
import { join } from 'pathe'
import { isTest } from 'std-env'
export default defineUntypedSchema({
/**
* The builder to use for bundling the Vue part of your application.
*
* @type {'vite' | 'webpack' | { bundle: (nuxt: typeof import('../src/types/nuxt').Nuxt) => Promise<void> }}
*/
builder: {
$resolve: async (val, get) => {
if (typeof val === 'object') {
return val
}
const map: Record<string, string> = {
vite: '@nuxt/vite-builder',
webpack: '@nuxt/webpack-builder',
}
return map[val] || val || (await get('vite') === false ? map.webpack : map.vite)
}
},
/**
* Whether to generate sourcemaps.
*
* @type {boolean | { server?: boolean, client?: boolean }}
*/
sourcemap: {
$resolve: async (val, get) => {
if (typeof val === 'boolean') {
return { server: val, client: val }
}
return defu(val, {
server: true,
client: await get('dev')
})
},
},
/**
* Log level when building logs.
*
* Defaults to 'silent' when running in CI or when a TTY is not available.
* This option is then used as 'silent' in Vite and 'none' in Webpack
*
* @type {'silent' | 'info' | 'verbose'}
*/
logLevel: {
$resolve: (val) => {
if (val && !['silent', 'info', 'verbose'].includes(val)) {
console.warn(`Invalid \`logLevel\` option: \`${val}\`. Must be one of: \`silent\`, \`info\`, \`verbose\`.`)
}
return val ?? (isTest ? 'silent' : 'info')
}
},
/**
* Shared build configuration.
*/
build: {
/**
* If you want to transpile specific dependencies with Babel, you can add them here.
* Each item in transpile can be a package name, a function, a string or regex object matching the
* dependency's file name.
*
* You can also use a function to conditionally transpile. The function will receive an object ({ isDev, isServer, isClient, isModern, isLegacy }).
*
* @example
* ```js
transpile: [({ isLegacy }) => isLegacy && 'ky']
* ```
* @type {Array<string | RegExp | ((ctx: { isClient?: boolean; isServer?: boolean; isDev: boolean }) => string | RegExp | false)>}
*/
transpile: {
$resolve: val => [].concat(val).filter(Boolean)
},
/**
* You can provide your own templates which will be rendered based
* on Nuxt configuration. This feature is specially useful for using with modules.
*
* Templates are rendered using [`lodash/template`](https://lodash.com/docs/4.17.15#template).
*
* @example
* ```js
* templates: [
* {
* src: '~/modules/support/plugin.js', // `src` can be absolute or relative
* dst: 'support.js', // `dst` is relative to project `.nuxt` dir
* options: {
* // Options are provided to template as `options` key
* live_chat: false
* }
* }
* ]
* ```
*/
templates: [],
/**
* Nuxt uses `webpack-bundle-analyzer` to visualize your bundles and how to optimize them.
*
* Set to `true` to enable bundle analysis, or pass an object with options: [for webpack](https://github.com/webpack-contrib/webpack-bundle-analyzer#options-for-plugin) or [for vite](https://github.com/btd/rollup-plugin-visualizer#options).
*
* @example
* ```js
* analyze: {
* analyzerMode: 'static'
* }
* ```
* @type {boolean | typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options | typeof import('rollup-plugin-visualizer').PluginVisualizerOptions}
*
*/
analyze: {
$resolve: async (val, get) => {
if (val !== true) {
return val ?? false
}
const rootDir = await get('rootDir')
const analyzeDir = await get('analyzeDir')
return {
template: 'treemap',
projectRoot: rootDir,
filename: join(analyzeDir, '{name}.html')
}
}
},
},
/**
* Build time optimization configuration.
*/
optimization: {
/**
* Functions to inject a key for.
*
* As long as the number of arguments passed to the function is less than `argumentLength`, an
* additional magic string will be injected that can be used to deduplicate requests between server
* and client. You will need to take steps to handle this additional key.
*
* The key will be unique based on the location of the function being invoked within the file.
*
* @type {Array<{ name: string, source?: string | RegExp, argumentLength: number }>}
*/
keyedComposables: {
$resolve: (val) => [
{ name: 'defineNuxtComponent', argumentLength: 2 },
{ name: 'useState', argumentLength: 2 },
{ name: 'useFetch', argumentLength: 3 },
{ name: 'useAsyncData', argumentLength: 3 },
{ name: 'useLazyAsyncData', argumentLength: 3 },
{ name: 'useLazyFetch', argumentLength: 3 },
].concat(val).filter(Boolean)
},
/**
* Tree shake code from specific builds.
*/
treeShake: {
/**
* Tree shake composables from the server or client builds.
*
* @example
* ```js
* treeShake: { client: { myPackage: ['useServerOnlyComposable'] } }
* ```
*/
composables: {
server: {
$resolve: async (val, get) => defu(val || {},
await get('dev') ? {} : {
vue: ['onBeforeMount', 'onMounted', 'onBeforeUpdate', 'onRenderTracked', 'onRenderTriggered', 'onActivated', 'onDeactivated', 'onBeforeUnmount'],
'#app': ['definePayloadReviver', 'definePageMeta']
}
)
},
client: {
$resolve: async (val, get) => defu(val || {},
await get('dev') ? {} : {
vue: ['onServerPrefetch', 'onRenderTracked', 'onRenderTriggered'],
'#app': ['definePayloadReducer', 'definePageMeta']
}
)
}
}
},
/**
* Options passed directly to the transformer from `unctx` that preserves async context
* after `await`.
*
* @type {import('unctx').TransformerOptions}
*/
asyncTransforms: {
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
objectDefinitions: {
defineNuxtComponent: ['asyncData', 'setup'],
defineNuxtPlugin: ['setup'],
definePageMeta: ['middleware', 'validate']
}
}
}
})