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-07-25 12:29:41 +00:00
|
|
|
import type { Connect, HmrOptions } 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'
|
|
|
|
import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } from 'ufo'
|
|
|
|
import escapeRE from 'escape-string-regexp'
|
2022-07-25 12:29:41 +00:00
|
|
|
import defu from 'defu'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
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-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) {
|
2022-08-13 12:43:26 +00:00
|
|
|
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry
|
|
|
|
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
2022-08-13 12:43:26 +00:00
|
|
|
entry: ctx.entry,
|
2022-08-15 13:10:53 +00:00
|
|
|
base: './',
|
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')
|
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
|
|
|
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-15 13:40:06 +00:00
|
|
|
ctx.config.build!.rollupOptions = defu(ctx.config.build!.rollupOptions!, {
|
2022-08-12 14:16:08 +00:00
|
|
|
output: {
|
|
|
|
// https://github.com/vitejs/vite/tree/main/packages/vite/src/node/build.ts#L464-L478
|
|
|
|
assetFileNames: ctx.nuxt.options.dev ? undefined : withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[name].[hash].[ext]')),
|
|
|
|
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-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)
|
|
|
|
})
|
|
|
|
clientConfig.server.hmr = defu(clientConfig.server.hmr as HmrOptions, {
|
|
|
|
// https://github.com/nuxt/framework/issues/4191
|
|
|
|
protocol: 'ws',
|
|
|
|
port: hmrPort
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-07-25 09:52:21 +00:00
|
|
|
const baseURL = joinURL(ctx.nuxt.options.app.baseURL.replace(/^\./, '') || '/', ctx.nuxt.options.app.buildAssetsDir)
|
|
|
|
const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(baseURL)))}`)
|
2022-07-17 14:17:07 +00:00
|
|
|
const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
|
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
2022-08-15 13:40:06 +00:00
|
|
|
const originalURL = req.url!
|
|
|
|
req.url = originalURL.replace(BASE_RE, '/')
|
|
|
|
viteServer.middlewares.handle(req, res, (err: unknown) => {
|
2022-07-17 14:17:07 +00:00
|
|
|
req.url = originalURL
|
|
|
|
next(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await ctx.nuxt.callHook('server:devMiddleware', 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)
|
|
|
|
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
|
|
|
}
|