2022-07-21 10:44:33 +00:00
|
|
|
import { join, 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'
|
2022-10-15 18:42:57 +00:00
|
|
|
import type { ServerOptions } from 'vite'
|
2022-02-16 21:34:32 +00:00
|
|
|
import { logger } from '@nuxt/kit'
|
2022-07-21 10:44:33 +00:00
|
|
|
import { getPort } from 'get-port-please'
|
2022-10-11 05:26:38 +00:00
|
|
|
import { joinURL, withoutLeadingSlash } from 'ufo'
|
2022-07-25 12:29:41 +00:00
|
|
|
import defu from 'defu'
|
2022-08-30 13:13:23 +00:00
|
|
|
import type { OutputOptions } from 'rollup'
|
2022-10-15 18:42:57 +00:00
|
|
|
import { defineEventHandler } from 'h3'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
2021-09-07 09:35:55 +00:00
|
|
|
import type { ViteBuildContext, ViteOptions } from './vite'
|
2021-11-05 08:55:53 +00:00
|
|
|
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
|
2022-08-13 12:43:26 +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, {
|
2022-08-13 12:43:26 +00:00
|
|
|
entry: ctx.entry,
|
2022-09-05 08:48:35 +00:00
|
|
|
base: ctx.nuxt.options.dev
|
|
|
|
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
|
|
|
|
: './',
|
2022-07-21 10:44:33 +00:00
|
|
|
experimental: {
|
|
|
|
renderBuiltUrl: (filename, { type, hostType }) => {
|
|
|
|
if (hostType !== 'js' || type === 'asset') {
|
|
|
|
// In CSS we only use relative paths until we craft a clever runtime CSS hack
|
|
|
|
return { relative: true }
|
|
|
|
}
|
|
|
|
return { runtime: `globalThis.__publicAssetsURL(${JSON.stringify(filename)})` }
|
|
|
|
}
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
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
|
|
|
},
|
2022-08-13 12:43:26 +00:00
|
|
|
optimizeDeps: {
|
|
|
|
entries: [ctx.entry]
|
|
|
|
},
|
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-19 19:10:32 +00:00
|
|
|
'#internal/nitro': resolve(ctx.nuxt.options.buildDir, 'nitro.client.mjs')
|
2022-08-18 08:06:37 +00:00
|
|
|
},
|
|
|
|
dedupe: ['vue']
|
2021-07-23 14:58:38 +00:00
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
build: {
|
2022-09-08 13:52:30 +00:00
|
|
|
sourcemap: ctx.nuxt.options.sourcemap.client ? ctx.config.build?.sourcemap ?? true : false,
|
2021-09-07 09:35:55 +00:00
|
|
|
manifest: true,
|
2022-08-13 12:43:26 +00:00
|
|
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client'),
|
|
|
|
rollupOptions: {
|
|
|
|
input: ctx.entry
|
|
|
|
}
|
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-18 16:59:14 +00:00
|
|
|
devStyleSSRPlugin({
|
2022-08-12 09:11:09 +00:00
|
|
|
srcDir: ctx.nuxt.options.srcDir,
|
2022-01-18 16:59:14 +00:00
|
|
|
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
|
2022-03-11 08:41:27 +00:00
|
|
|
}),
|
2022-08-13 12:43:26 +00:00
|
|
|
viteNodePlugin(ctx)
|
2021-04-29 11:51:54 +00:00
|
|
|
],
|
2022-07-21 10:44:33 +00:00
|
|
|
appType: 'custom',
|
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)
|
|
|
|
|
2022-07-21 10:44:33 +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 (!ctx.nuxt.options.dev) {
|
2022-08-15 13:40:06 +00:00
|
|
|
clientConfig.server!.hmr = false
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 14:16:08 +00:00
|
|
|
// We want to respect users' own rollup output options
|
2022-08-30 13:13:23 +00:00
|
|
|
clientConfig.build!.rollupOptions = defu(clientConfig.build!.rollupOptions!, {
|
2022-08-12 14:16:08 +00:00
|
|
|
output: {
|
|
|
|
chunkFileNames: ctx.nuxt.options.dev ? undefined : withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[name].[hash].js')),
|
|
|
|
entryFileNames: ctx.nuxt.options.dev ? 'entry.js' : withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[name].[hash].js'))
|
2022-08-30 13:13:23 +00:00
|
|
|
} as OutputOptions
|
|
|
|
}) as any
|
2022-08-12 14:16:08 +00:00
|
|
|
|
2022-08-15 13:40:06 +00:00
|
|
|
if (clientConfig.server && clientConfig.server.hmr !== false) {
|
2022-07-25 12:29:41 +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)
|
|
|
|
})
|
2022-10-27 10:36:37 +00:00
|
|
|
clientConfig.server = defu(clientConfig.server, <ServerOptions>{
|
|
|
|
https: ctx.nuxt.options.devServer.https,
|
2022-09-20 10:54:52 +00:00
|
|
|
hmr: {
|
2022-10-27 10:36:37 +00:00
|
|
|
protocol: ctx.nuxt.options.devServer.https ? 'wss' : 'ws',
|
2022-09-20 10:54:52 +00:00
|
|
|
port: hmrPort
|
|
|
|
}
|
2022-07-25 12:29:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-21 19:51:44 +00:00
|
|
|
// Add analyze plugin if needed
|
|
|
|
if (ctx.nuxt.options.build.analyze) {
|
2022-08-15 13:40:06 +00:00
|
|
|
clientConfig.plugins!.push(...await import('./plugins/analyze').then(r => r.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 })
|
|
|
|
|
2022-07-17 14:17:07 +00:00
|
|
|
if (ctx.nuxt.options.dev) {
|
|
|
|
// Dev
|
|
|
|
const viteServer = await vite.createServer(clientConfig)
|
|
|
|
ctx.clientServer = viteServer
|
|
|
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false })
|
2022-11-02 09:05:17 +00:00
|
|
|
const viteRoutes = viteServer.middlewares.stack.map(m => m.route).filter(r => r.length > 1)
|
2022-10-15 18:42:57 +00:00
|
|
|
const viteMiddleware = defineEventHandler(async (event) => {
|
2022-07-17 14:17:07 +00:00
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
2022-11-15 14:33:43 +00:00
|
|
|
const originalURL = event.node.req.url!
|
2022-11-02 09:05:17 +00:00
|
|
|
if (!viteRoutes.some(route => originalURL.startsWith(route)) && !originalURL.startsWith(clientConfig.base!)) {
|
2022-11-15 14:33:43 +00:00
|
|
|
event.node.req.url = joinURL('/__url', originalURL)
|
2022-10-11 05:26:38 +00:00
|
|
|
}
|
2022-10-15 18:42:57 +00:00
|
|
|
await new Promise((resolve, reject) => {
|
2022-11-15 14:33:43 +00:00
|
|
|
viteServer.middlewares.handle(event.node.req, event.node.res, (err: Error) => {
|
|
|
|
event.node.req.url = originalURL
|
2022-10-15 18:42:57 +00:00
|
|
|
return err ? reject(err) : resolve(null)
|
|
|
|
})
|
2022-07-17 14:17:07 +00:00
|
|
|
})
|
2022-10-15 18:42:57 +00:00
|
|
|
})
|
|
|
|
await ctx.nuxt.callHook('server:devHandler', viteMiddleware)
|
2021-09-07 09:35:55 +00:00
|
|
|
|
2022-07-17 14:17:07 +00:00
|
|
|
ctx.nuxt.hook('close', async () => {
|
|
|
|
await viteServer.close()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Build
|
2021-09-07 09:35:55 +00:00
|
|
|
const start = Date.now()
|
|
|
|
await vite.build(clientConfig)
|
2022-10-27 10:36:37 +00:00
|
|
|
await ctx.nuxt.callHook('vite:compiled')
|
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-04-29 11:51:54 +00:00
|
|
|
}
|