2021-04-29 11:51:54 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import * as vite from 'vite'
|
|
|
|
import vitePlugin from '@vitejs/plugin-vue'
|
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
|
|
|
import { replace } from './plugins/replace'
|
|
|
|
import { ViteBuildContext, ViteOptions } from './vite'
|
2021-06-16 12:42:58 +00:00
|
|
|
import { transformNuxtSetup } from './plugins/transformSetup'
|
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,
|
|
|
|
'module.hot': false,
|
|
|
|
global: 'globalThis'
|
|
|
|
},
|
|
|
|
build: {
|
|
|
|
outDir: 'dist/client',
|
|
|
|
assetsDir: '.',
|
|
|
|
rollupOptions: {
|
|
|
|
input: resolve(ctx.nuxt.options.buildDir, 'client.js')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
replace({ 'process.env': 'import.meta.env' }),
|
|
|
|
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
|
2021-06-16 12:42:58 +00:00
|
|
|
vitePlugin(ctx.config.vue),
|
|
|
|
transformNuxtSetup()
|
2021-04-29 11:51:54 +00:00
|
|
|
],
|
|
|
|
server: {
|
|
|
|
middlewareMode: true
|
|
|
|
}
|
|
|
|
} as ViteOptions)
|
|
|
|
|
|
|
|
await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })
|
|
|
|
|
|
|
|
const viteServer = await vite.createServer(clientConfig)
|
|
|
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer)
|
|
|
|
|
|
|
|
const viteMiddleware = (req, res, next) => {
|
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
|
|
|
const originalURL = req.url
|
|
|
|
if (req.url === '/_nuxt/client.js') {
|
|
|
|
return res.end('')
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|