2021-01-22 22:02:33 +00:00
|
|
|
import * as vite from 'vite'
|
2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-04-29 11:51:54 +00:00
|
|
|
import consola from 'consola'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Nuxt } from '@nuxt/schema'
|
2021-05-20 10:58:30 +00:00
|
|
|
import type { InlineConfig, SSROptions } from 'vite'
|
|
|
|
import type { Options } from '@vitejs/plugin-vue'
|
2021-11-03 13:04:42 +00:00
|
|
|
import { sanitizeFilePath } from 'mlly'
|
2021-05-24 11:14:10 +00:00
|
|
|
import { buildClient } from './client'
|
|
|
|
import { buildServer } from './server'
|
2021-07-15 10:18:34 +00:00
|
|
|
import virtual from './plugins/virtual'
|
2021-05-24 11:14:10 +00:00
|
|
|
import { warmupViteServer } from './utils/warmup'
|
2021-10-13 20:08:26 +00:00
|
|
|
import { resolveCSSOptions } from './css'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export interface ViteOptions extends InlineConfig {
|
|
|
|
vue?: Options
|
|
|
|
ssr?: SSROptions
|
|
|
|
}
|
2021-01-22 22:02:33 +00:00
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
export interface ViteBuildContext {
|
2021-01-22 22:02:33 +00:00
|
|
|
nuxt: Nuxt
|
2021-04-29 11:51:54 +00:00
|
|
|
config: ViteOptions
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function bundle (nuxt: Nuxt) {
|
|
|
|
const ctx: ViteBuildContext = {
|
|
|
|
nuxt,
|
2021-04-29 11:51:54 +00:00
|
|
|
config: vite.mergeConfig(
|
|
|
|
{
|
2021-10-14 11:17:39 +00:00
|
|
|
root: nuxt.options.srcDir,
|
2021-04-29 11:51:54 +00:00
|
|
|
mode: nuxt.options.dev ? 'development' : 'production',
|
|
|
|
logLevel: 'warn',
|
|
|
|
define: {
|
|
|
|
'process.dev': nuxt.options.dev
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
|
|
|
|
alias: {
|
|
|
|
...nuxt.options.alias,
|
|
|
|
'#app': nuxt.options.appDir,
|
2021-07-23 14:58:38 +00:00
|
|
|
// We need this resolution to be present before the following entry, but it
|
|
|
|
// will be filled in client/server configs
|
2021-10-27 08:03:10 +00:00
|
|
|
'#build/plugins': '',
|
2021-07-15 10:50:09 +00:00
|
|
|
'#build': nuxt.options.buildDir,
|
2021-07-21 20:05:22 +00:00
|
|
|
'/entry.mjs': resolve(nuxt.options.appDir, 'entry'),
|
2021-04-29 11:51:54 +00:00
|
|
|
'web-streams-polyfill/ponyfill/es2018': 'unenv/runtime/mock/empty',
|
|
|
|
// Cannot destructure property 'AbortController' of ..
|
|
|
|
'abort-controller': 'unenv/runtime/mock/empty'
|
|
|
|
}
|
|
|
|
},
|
2021-07-21 20:05:22 +00:00
|
|
|
base: nuxt.options.build.publicPath,
|
2021-11-24 15:42:38 +00:00
|
|
|
publicDir: resolve(nuxt.options.srcDir, nuxt.options.dir.public),
|
2021-10-26 12:49:18 +00:00
|
|
|
// TODO: move to kit schema when it exists
|
|
|
|
vue: {
|
|
|
|
isProduction: !nuxt.options.dev,
|
|
|
|
template: { compilerOptions: nuxt.options.vue.compilerOptions }
|
|
|
|
},
|
2021-10-13 20:08:26 +00:00
|
|
|
css: resolveCSSOptions(nuxt),
|
2021-04-29 11:51:54 +00:00
|
|
|
optimizeDeps: {
|
2021-11-15 10:25:50 +00:00
|
|
|
exclude: [
|
|
|
|
...nuxt.options.build.transpile.filter(i => typeof i === 'string'),
|
|
|
|
'vue-demi'
|
|
|
|
],
|
2021-10-07 08:29:10 +00:00
|
|
|
entries: [
|
|
|
|
resolve(nuxt.options.appDir, 'entry.ts')
|
|
|
|
]
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
|
|
|
esbuild: {
|
|
|
|
jsxFactory: 'h',
|
2021-11-04 08:13:29 +00:00
|
|
|
jsxFragment: 'Fragment',
|
|
|
|
tsconfigRaw: '{}'
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
|
|
|
clearScreen: false,
|
|
|
|
build: {
|
2021-07-15 10:18:34 +00:00
|
|
|
emptyOutDir: false,
|
|
|
|
rollupOptions: {
|
2021-11-03 13:04:42 +00:00
|
|
|
input: resolve(nuxt.options.appDir, 'entry'),
|
|
|
|
output: { sanitizeFileName: sanitizeFilePath }
|
2021-07-15 10:18:34 +00:00
|
|
|
}
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2021-07-15 10:18:34 +00:00
|
|
|
plugins: [
|
|
|
|
virtual(nuxt.vfs)
|
|
|
|
],
|
2021-07-14 14:37:07 +00:00
|
|
|
server: {
|
|
|
|
fs: {
|
2021-07-21 20:05:22 +00:00
|
|
|
strict: false,
|
2021-07-14 14:37:07 +00:00
|
|
|
allow: [
|
|
|
|
nuxt.options.buildDir,
|
|
|
|
nuxt.options.appDir,
|
|
|
|
nuxt.options.srcDir,
|
|
|
|
nuxt.options.rootDir,
|
|
|
|
...nuxt.options.modulesDir
|
|
|
|
]
|
|
|
|
}
|
2021-07-15 10:18:34 +00:00
|
|
|
}
|
2021-12-17 09:28:06 +00:00
|
|
|
} as ViteOptions,
|
|
|
|
nuxt.options.vite as any || {}
|
2021-04-29 11:51:54 +00:00
|
|
|
)
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 20:30:06 +00:00
|
|
|
await nuxt.callHook('vite:extend', ctx)
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
nuxt.hook('vite:serverCreated', (server: vite.ViteDevServer) => {
|
|
|
|
const start = Date.now()
|
2021-11-02 12:01:44 +00:00
|
|
|
warmupViteServer(server, ['/entry.mjs']).then(() => {
|
2021-04-29 11:51:54 +00:00
|
|
|
consola.info(`Vite warmed up in ${Date.now() - start}ms`)
|
|
|
|
}).catch(consola.error)
|
|
|
|
})
|
2021-01-22 22:02:33 +00:00
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await buildClient(ctx)
|
2021-07-21 20:05:22 +00:00
|
|
|
if (ctx.nuxt.options.ssr) {
|
|
|
|
await buildServer(ctx)
|
|
|
|
}
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|