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-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'
|
2022-02-28 16:11:46 +00:00
|
|
|
import { logger, isIgnored } from '@nuxt/kit'
|
2021-05-20 10:58:30 +00:00
|
|
|
import type { Options } from '@vitejs/plugin-vue'
|
2022-05-13 10:49:30 +00:00
|
|
|
import replace from '@rollup/plugin-replace'
|
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'
|
2022-07-07 16:26:04 +00:00
|
|
|
import { composableKeysPlugin } from './plugins/composable-keys'
|
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
|
2022-03-11 08:41:27 +00:00
|
|
|
clientServer?: vite.ViteDevServer
|
|
|
|
ssrServer?: vite.ViteDevServer
|
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(
|
|
|
|
{
|
|
|
|
resolve: {
|
|
|
|
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,
|
2022-02-25 12:42:34 +00:00
|
|
|
'/entry.mjs': resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : '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'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
optimizeDeps: {
|
2021-10-07 08:29:10 +00:00
|
|
|
entries: [
|
|
|
|
resolve(nuxt.options.appDir, 'entry.ts')
|
2022-03-31 14:10:06 +00:00
|
|
|
],
|
|
|
|
include: ['vue']
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2022-02-25 19:11:01 +00:00
|
|
|
css: resolveCSSOptions(nuxt),
|
2021-04-29 11:51:54 +00:00
|
|
|
build: {
|
2021-07-15 10:18:34 +00:00
|
|
|
rollupOptions: {
|
2022-02-25 19:11:01 +00:00
|
|
|
output: { sanitizeFileName: sanitizeFilePath },
|
|
|
|
input: resolve(nuxt.options.appDir, 'entry')
|
2022-07-17 15:10:27 +00:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
exclude: nuxt.options.ignore
|
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: [
|
2022-07-07 16:26:04 +00:00
|
|
|
composableKeysPlugin.vite({ sourcemap: nuxt.options.sourcemap, rootDir: nuxt.options.rootDir }),
|
2022-05-13 10:49:30 +00:00
|
|
|
replace({
|
|
|
|
...Object.fromEntries([';', '(', '{', '}', ' ', '\t', '\n'].map(d => [`${d}global.`, `${d}globalThis.`])),
|
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2022-07-21 10:44:33 +00:00
|
|
|
virtual(nuxt.vfs)
|
2021-07-15 10:18:34 +00:00
|
|
|
],
|
2022-03-17 22:17:59 +00:00
|
|
|
vue: {
|
|
|
|
reactivityTransform: nuxt.options.experimental.reactivityTransform
|
|
|
|
},
|
2021-07-14 14:37:07 +00:00
|
|
|
server: {
|
2022-06-22 18:07:54 +00:00
|
|
|
watch: { ignored: isIgnored },
|
2021-07-14 14:37:07 +00:00
|
|
|
fs: {
|
|
|
|
allow: [
|
2022-02-25 19:11:01 +00:00
|
|
|
nuxt.options.appDir
|
2021-07-14 14:37:07 +00:00
|
|
|
]
|
|
|
|
}
|
2021-07-15 10:18:34 +00:00
|
|
|
}
|
2022-03-23 08:10:12 +00:00
|
|
|
} as ViteOptions,
|
2022-02-25 19:11:01 +00:00
|
|
|
nuxt.options.vite
|
2021-04-29 11:51:54 +00:00
|
|
|
)
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 18:07:54 +00:00
|
|
|
// In build mode we explicitly override any vite options that vite is relying on
|
|
|
|
// to detect whether to inject production or development code (such as HMR code)
|
|
|
|
if (!nuxt.options.dev) {
|
|
|
|
ctx.config.server.watch = undefined
|
2022-07-17 15:10:27 +00:00
|
|
|
ctx.config.build.watch = undefined
|
2022-06-22 18:07:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 20:30:06 +00:00
|
|
|
await nuxt.callHook('vite:extend', ctx)
|
|
|
|
|
2022-03-14 10:19:37 +00:00
|
|
|
nuxt.hook('vite:serverCreated', (server: vite.ViteDevServer, env) => {
|
2022-02-08 00:10:42 +00:00
|
|
|
// Invalidate virtual modules when templates are re-generated
|
|
|
|
ctx.nuxt.hook('app:templatesGenerated', () => {
|
|
|
|
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
|
|
if (id.startsWith('\x00virtual:')) {
|
|
|
|
server.moduleGraph.invalidateModule(mod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
const start = Date.now()
|
2022-02-16 21:34:32 +00:00
|
|
|
warmupViteServer(server, ['/entry.mjs'])
|
2022-03-14 10:19:37 +00:00
|
|
|
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
|
2022-02-16 21:34:32 +00:00
|
|
|
.catch(logger.error)
|
2021-04-29 11:51:54 +00:00
|
|
|
})
|
2022-07-17 14:17:07 +00:00
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await buildClient(ctx)
|
2022-01-13 12:39:23 +00:00
|
|
|
await buildServer(ctx)
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|