2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-10-02 16:01:17 +00:00
|
|
|
import * as vite from 'vite'
|
2021-10-26 12:49:18 +00:00
|
|
|
import vuePlugin from '@vitejs/plugin-vue'
|
2021-10-13 10:34:51 +00:00
|
|
|
import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
|
2021-10-18 07:54:20 +00:00
|
|
|
import type { Connect } from 'vite'
|
2022-02-16 21:34:32 +00:00
|
|
|
import { logger } from '@nuxt/kit'
|
2022-01-24 12:57:24 +00:00
|
|
|
import { joinURL } from 'ufo'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
2021-11-05 08:58:03 +00:00
|
|
|
import { analyzePlugin } from './plugins/analyze'
|
2021-09-07 09:35:55 +00:00
|
|
|
import { wpfs } from './utils/wpfs'
|
|
|
|
import type { ViteBuildContext, ViteOptions } from './vite'
|
2021-10-12 14:05:20 +00:00
|
|
|
import { writeManifest } from './manifest'
|
2021-11-05 08:55:53 +00:00
|
|
|
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
|
2022-04-01 13:22:22 +00:00
|
|
|
import { RelativeAssetPlugin } from './plugins/dynamic-base'
|
2022-03-11 08:41:27 +00:00
|
|
|
import { viteNodePlugin } from './vite-node'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildClient (ctx: ViteBuildContext) {
|
|
|
|
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
|
|
|
define: {
|
|
|
|
'process.server': false,
|
|
|
|
'process.client': true,
|
2021-11-10 20:15:26 +00:00
|
|
|
'module.hot': false
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2021-07-23 14:58:38 +00:00
|
|
|
resolve: {
|
|
|
|
alias: {
|
2022-03-22 15:51:26 +00:00
|
|
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
|
2022-04-07 11:28:04 +00:00
|
|
|
'#nitro': resolve(ctx.nuxt.options.buildDir, 'nitro.client.mjs')
|
2021-07-23 14:58:38 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
build: {
|
2021-09-07 09:35:55 +00:00
|
|
|
rollupOptions: {
|
|
|
|
output: {
|
|
|
|
chunkFileNames: ctx.nuxt.options.dev ? undefined : '[name]-[hash].mjs',
|
|
|
|
entryFileNames: ctx.nuxt.options.dev ? 'entry.mjs' : '[name]-[hash].mjs'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
manifest: true,
|
2021-10-05 14:18:03 +00:00
|
|
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client')
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
|
2021-10-26 12:49:18 +00:00
|
|
|
vuePlugin(ctx.config.vue),
|
2021-11-05 08:55:53 +00:00
|
|
|
viteJsxPlugin(),
|
2022-01-24 12:57:24 +00:00
|
|
|
RelativeAssetPlugin(),
|
2022-01-18 16:59:14 +00:00
|
|
|
devStyleSSRPlugin({
|
|
|
|
rootDir: ctx.nuxt.options.rootDir,
|
|
|
|
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
|
2022-03-11 08:41:27 +00:00
|
|
|
}),
|
|
|
|
viteNodePlugin(ctx)
|
2021-04-29 11:51:54 +00:00
|
|
|
],
|
|
|
|
server: {
|
2022-02-11 12:09:25 +00:00
|
|
|
middlewareMode: true
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|
|
|
|
} as ViteOptions)
|
|
|
|
|
2021-10-21 19:51:44 +00:00
|
|
|
// Add analyze plugin if needed
|
|
|
|
if (ctx.nuxt.options.build.analyze) {
|
2021-11-05 08:58:03 +00:00
|
|
|
clientConfig.plugins.push(...analyzePlugin(ctx))
|
2021-10-21 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })
|
|
|
|
|
|
|
|
const viteServer = await vite.createServer(clientConfig)
|
2022-03-11 08:41:27 +00:00
|
|
|
ctx.clientServer = viteServer
|
2022-03-14 10:19:37 +00:00
|
|
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false })
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2021-10-18 07:54:20 +00:00
|
|
|
const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
|
2021-04-29 11:51:54 +00:00
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
|
|
|
const originalURL = req.url
|
|
|
|
viteServer.middlewares.handle(req, res, (err) => {
|
|
|
|
req.url = originalURL
|
|
|
|
next(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await ctx.nuxt.callHook('server:devMiddleware', viteMiddleware)
|
|
|
|
|
|
|
|
ctx.nuxt.hook('close', async () => {
|
|
|
|
await viteServer.close()
|
|
|
|
})
|
2021-09-07 09:35:55 +00:00
|
|
|
|
|
|
|
if (!ctx.nuxt.options.dev) {
|
|
|
|
const start = Date.now()
|
|
|
|
await vite.build(clientConfig)
|
|
|
|
await ctx.nuxt.callHook('build:resources', wpfs)
|
2022-02-16 21:34:32 +00:00
|
|
|
logger.info(`Client built in ${Date.now() - start}ms`)
|
2021-09-07 09:35:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:05:20 +00:00
|
|
|
await writeManifest(ctx)
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|