Nuxt/packages/schema/src/config/build.ts

198 lines
6.7 KiB
TypeScript
Raw Normal View History

import { defineUntypedSchema } from 'untyped'
import { defu } from 'defu'
import { join } from 'pathe'
import { isTest } from 'std-env'
import { consola } from 'consola'
export default defineUntypedSchema({
2021-04-15 18:49:29 +00:00
/**
* 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> }}
2021-04-15 18:49:29 +00:00
*/
builder: {
$resolve: async (val, get) => {
if (typeof val === 'object') {
return val
}
const map: Record<string, string> = {
vite: '@nuxt/vite-builder',
2023-08-08 20:55:29 +00:00
webpack: '@nuxt/webpack-builder'
}
return map[val] || val || (await get('vite') === false ? map.webpack : map.vite)
}
},
/**
* Whether to generate sourcemaps.
* @type {boolean | { server?: boolean | 'hidden', client?: boolean | 'hidden' }}
*/
sourcemap: {
$resolve: async (val, get) => {
if (typeof val === 'boolean') {
return { server: val, client: val }
}
return defu(val, {
server: true,
client: await get('dev')
})
2023-08-08 20:55:29 +00:00
}
},
/**
* 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)) {
consola.warn(`Invalid \`logLevel\` option: \`${val}\`. Must be one of: \`silent\`, \`info\`, \`verbose\`.`)
}
return val ?? (isTest ? 'silent' : 'info')
}
},
/**
* Shared build configuration.
2021-04-15 18:49:29 +00:00
*/
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.
2021-04-15 18:49:29 +00:00
*
* You can also use a function to conditionally transpile. The function will receive an object ({ isDev, isServer, isClient, isModern, isLegacy }).
2021-04-15 18:49:29 +00:00
* @example
* ```js
transpile: [({ isLegacy }) => isLegacy && 'ky']
2021-04-15 18:49:29 +00:00
* ```
* @type {Array<string | RegExp | ((ctx: { isClient?: boolean; isServer?: boolean; isDev: boolean }) => string | RegExp | false)>}
2021-04-15 18:49:29 +00:00
*/
transpile: {
$resolve: val => [].concat(val).filter(Boolean)
},
2021-04-15 18:49:29 +00:00
/**
* You can provide your own templates which will be rendered based
* on Nuxt configuration. This feature is specially useful for using with modules.
2021-04-15 18:49:29 +00:00
*
* Templates are rendered using [`lodash/template`](https://lodash.com/docs/4.17.15#template).
2021-04-15 18:49:29 +00:00
* @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
* }
* }
* ]
2021-04-15 18:49:29 +00:00
* ```
* @type {typeof import('../src/types/nuxt').NuxtTemplate<any>[]}
2021-04-15 18:49:29 +00:00
*/
templates: [],
/**
* Nuxt allows visualizing 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 | { enabled?: boolean } & ((0 extends 1 & typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options ? {} : typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options) | typeof import('rollup-plugin-visualizer').PluginVisualizerOptions)}
*/
analyze: {
$resolve: async (val, get) => {
const [rootDir, analyzeDir] = await Promise.all([get('rootDir'), get('analyzeDir')])
return defu(typeof val === 'boolean' ? { enabled: val } : val, {
template: 'treemap',
projectRoot: rootDir,
filename: join(analyzeDir, '{name}.html')
})
}
2023-08-08 20:55:29 +00:00
}
},
/**
* 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: {
2023-08-08 20:55:29 +00:00
$resolve: val => [
{ name: 'defineNuxtComponent', argumentLength: 2 },
{ name: 'useState', argumentLength: 2 },
{ name: 'useFetch', argumentLength: 3 },
{ name: 'useAsyncData', argumentLength: 3 },
{ name: 'useLazyAsyncData', argumentLength: 3 },
2023-08-08 20:55:29 +00:00
{ 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 || {},
2023-08-08 20:55:29 +00:00
await get('dev')
? {}
: {
vue: ['onBeforeMount', 'onMounted', 'onBeforeUpdate', 'onRenderTracked', 'onRenderTriggered', 'onActivated', 'onDeactivated', 'onBeforeUnmount'],
'#app': ['definePayloadReviver', 'definePageMeta']
}
)
},
client: {
$resolve: async (val, get) => defu(val || {},
2023-08-08 20:55:29 +00:00
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 {typeof import('unctx/transform').TransformerOptions}
*/
asyncTransforms: {
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
objectDefinitions: {
defineNuxtComponent: ['asyncData', 'setup'],
defineNuxtPlugin: ['setup'],
definePageMeta: ['middleware', 'validate']
}
}
}
})