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-16 21:34:32 +00:00
|
|
|
import { logger } 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-01-18 16:59:14 +00:00
|
|
|
import { joinURL, withoutLeadingSlash } from 'ufo'
|
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(
|
|
|
|
{
|
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,
|
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'
|
|
|
|
}
|
|
|
|
},
|
2022-01-18 16:59:14 +00:00
|
|
|
base: nuxt.options.dev
|
|
|
|
? joinURL(nuxt.options.app.baseURL, nuxt.options.app.buildAssetsDir)
|
|
|
|
: '/__NUXT_BASE__/',
|
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: {
|
2022-01-24 12:57:24 +00:00
|
|
|
assetsDir: nuxt.options.dev ? withoutLeadingSlash(nuxt.options.app.buildAssetsDir) : '.',
|
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: {
|
2022-02-11 12:09:25 +00:00
|
|
|
hmr: {
|
|
|
|
clientPort: hmrPort,
|
|
|
|
port: hmrPort
|
|
|
|
},
|
2021-07-14 14:37:07 +00:00
|
|
|
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) => {
|
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
|
|
|
}
|