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'
|
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'
|
2022-04-01 13:22:22 +00:00
|
|
|
import { DynamicBasePlugin } from './plugins/dynamic-base'
|
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) {
|
2022-02-11 12:09:25 +00:00
|
|
|
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')
|
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')
|
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-04-01 13:22:22 +00:00
|
|
|
virtual(nuxt.vfs),
|
2022-04-22 15:35:42 +00:00
|
|
|
DynamicBasePlugin.vite({ sourcemap: nuxt.options.sourcemap })
|
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 },
|
2022-02-11 12:09:25 +00:00
|
|
|
hmr: {
|
2022-04-09 10:09:43 +00:00
|
|
|
// https://github.com/nuxt/framework/issues/4191
|
|
|
|
protocol: 'ws',
|
2022-02-11 12:09:25 +00:00
|
|
|
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-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.hmr = false
|
|
|
|
ctx.config.server.watch = undefined
|
2022-07-13 12:44:07 +00:00
|
|
|
|
|
|
|
// TODO: Workaround for vite watching tsconfig changes
|
|
|
|
// https://github.com/nuxt/framework/pull/5875
|
|
|
|
ctx.config.plugins.push({
|
|
|
|
name: 'nuxt:close-vite-watcher',
|
|
|
|
configureServer (server) {
|
|
|
|
return server?.watcher?.close()
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
})
|
|
|
|
await buildClient(ctx)
|
2022-01-13 12:39:23 +00:00
|
|
|
await buildServer(ctx)
|
2021-01-22 22:02:33 +00:00
|
|
|
}
|