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'
|
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-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'
|
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-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) {
|
2022-07-21 10:44:33 +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)
|
|
|
|
})
|
2021-04-29 11:51:54 +00:00
|
|
|
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
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
|
|
|
},
|
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
|
|
|
rollupOptions: {
|
|
|
|
output: {
|
2022-07-21 10:44:33 +00:00
|
|
|
// 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].mjs')),
|
|
|
|
entryFileNames: ctx.nuxt.options.dev ? 'entry.mjs' : withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[name].[hash].mjs'))
|
2021-09-07 09:35:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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-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
|
|
|
],
|
2022-07-21 10:44:33 +00:00
|
|
|
appType: 'custom',
|
2021-04-29 11:51:54 +00:00
|
|
|
server: {
|
2022-07-21 10:44:33 +00:00
|
|
|
hmr: {
|
|
|
|
// https://github.com/nuxt/framework/issues/4191
|
|
|
|
protocol: 'ws',
|
|
|
|
clientPort: hmrPort,
|
|
|
|
port: hmrPort
|
|
|
|
},
|
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) {
|
|
|
|
clientConfig.server.hmr = false
|
|
|
|
}
|
|
|
|
|
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 })
|
|
|
|
|
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-21 10:44:33 +00:00
|
|
|
const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir))))}`)
|
2022-07-17 14:17:07 +00:00
|
|
|
const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
|
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
|
|
|
const originalURL = req.url
|
2022-07-21 10:44:33 +00:00
|
|
|
req.url = req.url.replace(BASE_RE, '/')
|
2022-07-17 14:17:07 +00:00
|
|
|
viteServer.middlewares.handle(req, res, (err) => {
|
|
|
|
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
|
|
|
}
|