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'
|
2021-11-03 13:04:42 +00:00
|
|
|
import { sanitizeFilePath } from 'mlly'
|
2022-02-11 12:09:25 +00:00
|
|
|
import { getPort } from 'get-port-please'
|
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) {
|
2022-02-11 12:09:25 +00:00
|
|
|
// TODO: After nitropack refactor, try if we can resuse the same server port as Nuxt
|
|
|
|
const hmrPortDefault = 24678 // Vite's default HMR port
|
|
|
|
const hmrPort = await getPort({
|
|
|
|
port: hmrPortDefault,
|
|
|
|
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
|
|
|
|
})
|
|
|
|
|
2021-01-22 22:02:33 +00:00
|
|
|
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')
|
|
|
|
]
|
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')
|
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: {
|
2022-02-28 16:11:46 +00:00
|
|
|
watch: {
|
|
|
|
ignored: isIgnored
|
|
|
|
},
|
2022-02-11 12:09:25 +00:00
|
|
|
hmr: {
|
|
|
|
clientPort: hmrPort,
|
|
|
|
port: hmrPort
|
|
|
|
},
|
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-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
|
|
|
}
|
|
|
|
|
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) => {
|
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'])
|
|
|
|
.then(() => logger.info(`Vite warmed up in ${Date.now() - start}ms`))
|
|
|
|
.catch(logger.error)
|
2021-04-29 11:51:54 +00:00
|
|
|
})
|
2021-01-22 22:02:33 +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
|
|
|
}
|