2022-12-12 12:28:49 +00:00
|
|
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
2023-01-21 23:15:48 +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'
|
2023-04-07 16:02:47 +00:00
|
|
|
import type { BuildOptions, 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'
|
2023-01-21 23:15:48 +00:00
|
|
|
import { joinURL, withoutLeadingSlash } from 'ufo'
|
2023-01-30 12:09:48 +00:00
|
|
|
import { defu } from 'defu'
|
2024-01-18 16:09:27 +00:00
|
|
|
import { env, nodeless } from 'unenv'
|
2023-10-28 17:14:39 +00:00
|
|
|
import { appendCorsHeaders, appendCorsPreflightHeaders, defineEventHandler } from 'h3'
|
2023-03-29 10:59:57 +00:00
|
|
|
import type { ViteConfig } from '@nuxt/schema'
|
2023-02-16 12:43:58 +00:00
|
|
|
import { chunkErrorPlugin } from './plugins/chunk-error'
|
2023-03-29 10:59:57 +00:00
|
|
|
import type { ViteBuildContext } from './vite'
|
2021-11-05 08:55:53 +00:00
|
|
|
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
|
2023-01-31 14:04:55 +00:00
|
|
|
import { runtimePathsPlugin } from './plugins/paths'
|
2023-06-05 14:58:18 +00:00
|
|
|
import { typeCheckPlugin } from './plugins/type-check'
|
2022-08-13 12:43:26 +00:00
|
|
|
import { viteNodePlugin } from './vite-node'
|
2023-03-08 11:56:41 +00:00
|
|
|
import { createViteLogger } from './utils/logger'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildClient (ctx: ViteBuildContext) {
|
2024-03-09 06:48:15 +00:00
|
|
|
const nodeCompat = ctx.nuxt.options.experimental.clientNodeCompat
|
|
|
|
? {
|
|
|
|
alias: env(nodeless).alias,
|
|
|
|
define: {
|
2024-04-05 18:08:32 +00:00
|
|
|
global: 'globalThis',
|
|
|
|
},
|
2024-03-09 06:48:15 +00:00
|
|
|
}
|
|
|
|
: { alias: {}, define: {} }
|
2024-01-18 16:09:27 +00:00
|
|
|
|
2023-07-24 17:32:12 +00:00
|
|
|
const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, vite.mergeConfig({
|
2023-06-16 14:19:53 +00:00
|
|
|
configFile: false,
|
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: {
|
2023-01-21 23:15:48 +00:00
|
|
|
renderBuiltUrl: (filename, { type, hostType }) => {
|
2022-07-21 10:44:33 +00:00
|
|
|
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)})` }
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2022-07-21 10:44:33 +00:00
|
|
|
},
|
2023-01-23 11:08:48 +00:00
|
|
|
css: {
|
2024-04-05 18:08:32 +00:00
|
|
|
devSourcemap: !!ctx.nuxt.options.sourcemap.client,
|
2023-01-23 11:08:48 +00:00
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
define: {
|
2023-06-05 18:23:38 +00:00
|
|
|
'process.env.NODE_ENV': JSON.stringify(ctx.config.mode),
|
2021-04-29 11:51:54 +00:00
|
|
|
'process.server': false,
|
|
|
|
'process.client': true,
|
2023-08-07 22:03:40 +00:00
|
|
|
'process.browser': true,
|
|
|
|
'process.nitro': false,
|
|
|
|
'process.prerender': false,
|
2023-08-10 08:51:58 +00:00
|
|
|
'import.meta.server': false,
|
|
|
|
'import.meta.client': true,
|
|
|
|
'import.meta.browser': true,
|
|
|
|
'import.meta.nitro': false,
|
|
|
|
'import.meta.prerender': false,
|
2024-01-18 16:09:27 +00:00
|
|
|
'module.hot': false,
|
2024-04-05 18:08:32 +00:00
|
|
|
...nodeCompat.define,
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2022-08-13 12:43:26 +00:00
|
|
|
optimizeDeps: {
|
2024-04-05 18:08:32 +00:00
|
|
|
entries: [ctx.entry],
|
2024-05-27 13:26:03 +00:00
|
|
|
include: [],
|
|
|
|
// We exclude Vue and Nuxt common dependencies from optimization
|
|
|
|
// as they already ship ESM.
|
|
|
|
//
|
|
|
|
// This will help to reduce the chance for users to encounter
|
|
|
|
// common chunk conflicts that causing browser reloads.
|
|
|
|
// We should also encourage module authors to add their deps to
|
|
|
|
// `exclude` if they ships bundled ESM.
|
|
|
|
//
|
|
|
|
// Also since `exclude` is inert, it's safe to always include
|
|
|
|
// all possible deps even if they are not used yet.
|
|
|
|
//
|
|
|
|
// @see https://github.com/antfu/nuxt-better-optimize-deps#how-it-works
|
|
|
|
exclude: [
|
|
|
|
// Vue
|
|
|
|
'vue',
|
|
|
|
'@vue/runtime-core',
|
|
|
|
'@vue/runtime-dom',
|
|
|
|
'@vue/reactivity',
|
|
|
|
'@vue/shared',
|
|
|
|
'@vue/devtools-api',
|
|
|
|
'vue-router',
|
|
|
|
'vue-demi',
|
|
|
|
|
|
|
|
// Nuxt
|
|
|
|
'nuxt',
|
|
|
|
'nuxt/app',
|
|
|
|
|
|
|
|
// Nuxt Deps
|
|
|
|
'@unhead/vue',
|
|
|
|
'consola',
|
|
|
|
'defu',
|
|
|
|
'devalue',
|
|
|
|
'h3',
|
|
|
|
'hookable',
|
|
|
|
'klona',
|
|
|
|
'ofetch',
|
|
|
|
'pathe',
|
|
|
|
'ufo',
|
|
|
|
'unctx',
|
|
|
|
'unenv',
|
|
|
|
],
|
2022-08-13 12:43:26 +00:00
|
|
|
},
|
2021-07-23 14:58:38 +00:00
|
|
|
resolve: {
|
|
|
|
alias: {
|
2024-01-18 16:09:27 +00:00
|
|
|
...nodeCompat.alias,
|
|
|
|
...ctx.config.resolve?.alias,
|
2024-03-21 11:57:11 +00:00
|
|
|
'#internal/nuxt/paths': resolve(ctx.nuxt.options.buildDir, 'paths.mjs'),
|
2022-03-22 15:51:26 +00:00
|
|
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
|
2024-04-05 18:08:32 +00:00
|
|
|
'#internal/nitro': resolve(ctx.nuxt.options.buildDir, 'nitro.client.mjs'),
|
2022-08-18 08:06:37 +00:00
|
|
|
},
|
2023-05-14 11:56:15 +00:00
|
|
|
dedupe: [
|
2024-04-05 18:08:32 +00:00
|
|
|
'vue',
|
|
|
|
],
|
2021-07-23 14:58:38 +00:00
|
|
|
},
|
2024-06-15 21:02:57 +00:00
|
|
|
cacheDir: resolve(ctx.nuxt.options.rootDir, ctx.config.cacheDir ?? 'node_modules/.cache/vite', 'client'),
|
2021-04-29 11:51:54 +00:00
|
|
|
build: {
|
2024-01-19 18:08:17 +00:00
|
|
|
sourcemap: ctx.nuxt.options.sourcemap.client ? ctx.config.build?.sourcemap ?? ctx.nuxt.options.sourcemap.client : false,
|
2023-09-18 12:09:59 +00:00
|
|
|
manifest: 'manifest.json',
|
2022-08-13 12:43:26 +00:00
|
|
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client'),
|
|
|
|
rollupOptions: {
|
2024-04-05 18:08:32 +00:00
|
|
|
input: { entry: ctx.entry },
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
|
|
|
plugins: [
|
2022-01-18 16:59:14 +00:00
|
|
|
devStyleSSRPlugin({
|
2022-08-12 09:11:09 +00:00
|
|
|
srcDir: ctx.nuxt.options.srcDir,
|
2024-04-05 18:08:32 +00:00
|
|
|
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir),
|
2022-03-11 08:41:27 +00:00
|
|
|
}),
|
2023-01-31 14:04:55 +00:00
|
|
|
runtimePathsPlugin({
|
2024-04-05 18:08:32 +00:00
|
|
|
sourcemap: !!ctx.nuxt.options.sourcemap.client,
|
2023-01-31 14:04:55 +00:00
|
|
|
}),
|
2024-04-05 18:08:32 +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: {
|
2024-04-05 18:08:32 +00:00
|
|
|
middlewareMode: true,
|
|
|
|
},
|
2023-07-24 17:32:12 +00:00
|
|
|
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$client || {}))
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2023-03-08 11:56:41 +00:00
|
|
|
clientConfig.customLogger = createViteLogger(clientConfig)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-16 12:43:58 +00:00
|
|
|
// Emit chunk errors if the user has opted in to `experimental.emitRouteChunkError`
|
|
|
|
if (ctx.nuxt.options.experimental.emitRouteChunkError) {
|
2023-08-24 12:06:44 +00:00
|
|
|
clientConfig.plugins!.push(chunkErrorPlugin({ sourcemap: !!ctx.nuxt.options.sourcemap.client }))
|
2023-02-16 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 17:14:39 +00:00
|
|
|
// Inject an h3-based CORS handler in preference to vite's
|
|
|
|
const useViteCors = clientConfig.server?.cors !== undefined
|
|
|
|
if (!useViteCors) {
|
|
|
|
clientConfig.server!.cors = false
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:16:08 +00:00
|
|
|
// We want to respect users' own rollup output options
|
2024-06-13 22:35:00 +00:00
|
|
|
const fileNames = withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[hash].js'))
|
2022-08-30 13:13:23 +00:00
|
|
|
clientConfig.build!.rollupOptions = defu(clientConfig.build!.rollupOptions!, {
|
2022-08-12 14:16:08 +00:00
|
|
|
output: {
|
2024-06-13 22:35:00 +00:00
|
|
|
chunkFileNames: ctx.nuxt.options.dev ? undefined : fileNames,
|
|
|
|
entryFileNames: ctx.nuxt.options.dev ? 'entry.js' : fileNames,
|
2024-04-05 18:08:32 +00:00
|
|
|
} satisfies NonNullable<BuildOptions['rollupOptions']>['output'],
|
2022-08-30 13:13:23 +00:00
|
|
|
}) 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) {
|
2024-05-28 12:48:33 +00:00
|
|
|
const serverDefaults: Omit<ServerOptions, 'hmr'> & { hmr: Exclude<ServerOptions['hmr'], boolean> } = {
|
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',
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2024-05-28 12:48:33 +00:00
|
|
|
}
|
|
|
|
if (typeof clientConfig.server.hmr !== 'object' || !clientConfig.server.hmr.server) {
|
|
|
|
const hmrPortDefault = 24678 // Vite's default HMR port
|
|
|
|
serverDefaults.hmr!.port = await getPort({
|
|
|
|
port: hmrPortDefault,
|
|
|
|
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (ctx.nuxt.options.devServer.https) {
|
|
|
|
serverDefaults.https = ctx.nuxt.options.devServer.https === true ? {} : ctx.nuxt.options.devServer.https
|
|
|
|
}
|
|
|
|
clientConfig.server = defu(clientConfig.server, serverDefaults as ViteConfig['server'])
|
2022-07-25 12:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 19:51:44 +00:00
|
|
|
// Add analyze plugin if needed
|
2023-12-05 17:09:46 +00:00
|
|
|
if (!ctx.nuxt.options.test && ctx.nuxt.options.build.analyze && (ctx.nuxt.options.build.analyze === true || ctx.nuxt.options.build.analyze.enabled)) {
|
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
|
|
|
}
|
|
|
|
|
2023-06-05 14:58:18 +00:00
|
|
|
// Add type checking client panel
|
2023-12-05 17:09:46 +00:00
|
|
|
if (!ctx.nuxt.options.test && ctx.nuxt.options.typescript.typeCheck === true && ctx.nuxt.options.dev) {
|
2023-08-24 12:06:44 +00:00
|
|
|
clientConfig.plugins!.push(typeCheckPlugin({ sourcemap: !!ctx.nuxt.options.sourcemap.client }))
|
2023-06-05 14:58:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })
|
|
|
|
|
2023-03-29 10:59:57 +00:00
|
|
|
clientConfig.plugins!.unshift(
|
|
|
|
vuePlugin(clientConfig.vue),
|
2024-04-05 18:08:32 +00:00
|
|
|
viteJsxPlugin(clientConfig.vueJsx),
|
2023-03-29 10:59:57 +00:00
|
|
|
)
|
|
|
|
|
2023-04-29 22:37:06 +00:00
|
|
|
await ctx.nuxt.callHook('vite:configResolved', clientConfig, { isClient: true, isServer: false })
|
|
|
|
|
2024-05-27 13:26:03 +00:00
|
|
|
// Prioritize `optimizeDeps.exclude`. If same dep is in `include` and `exclude`, remove it from `include`
|
|
|
|
clientConfig.optimizeDeps!.include = clientConfig.optimizeDeps!.include!
|
|
|
|
.filter(dep => !clientConfig.optimizeDeps!.exclude!.includes(dep))
|
|
|
|
|
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-12-12 11:45:16 +00:00
|
|
|
const transformHandler = viteServer.middlewares.stack.findIndex(m => m.handle instanceof Function && m.handle.name === 'viteTransformMiddleware')
|
|
|
|
viteServer.middlewares.stack.splice(transformHandler, 0, {
|
|
|
|
route: '',
|
|
|
|
handle: (req: IncomingMessage & { _skip_transform?: boolean }, res: ServerResponse, next: (err?: any) => void) => {
|
|
|
|
// 'Skip' the transform middleware
|
|
|
|
if (req._skip_transform) { req.url = joinURL('/__skip_vite', req.url!) }
|
|
|
|
next()
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2022-12-12 11:45:16 +00:00
|
|
|
})
|
2023-01-16 16:04:16 +00:00
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
const viteMiddleware = defineEventHandler(async (event) => {
|
2024-06-13 22:35:00 +00:00
|
|
|
const viteRoutes: string[] = []
|
|
|
|
for (const viteRoute of viteServer.middlewares.stack) {
|
|
|
|
const m = viteRoute.route
|
|
|
|
if (m.length > 1) {
|
|
|
|
viteRoutes.push(m)
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 07:30:53 +00:00
|
|
|
if (!event.path.startsWith(clientConfig.base!) && !viteRoutes.some(route => event.path.startsWith(route))) {
|
2023-01-16 16:04:16 +00:00
|
|
|
// @ts-expect-error _skip_transform is a private property
|
|
|
|
event.node.req._skip_transform = true
|
2023-10-28 17:14:39 +00:00
|
|
|
} else if (!useViteCors) {
|
|
|
|
if (event.method === 'OPTIONS') {
|
|
|
|
appendCorsPreflightHeaders(event, {})
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
appendCorsHeaders(event, {})
|
2023-01-16 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 07:30:53 +00:00
|
|
|
// Workaround: vite devmiddleware modifies req.url
|
|
|
|
const _originalPath = event.node.req.url
|
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) => {
|
2023-08-23 07:30:53 +00:00
|
|
|
event.node.req.url = _originalPath
|
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
|
2022-12-16 11:47:12 +00:00
|
|
|
logger.info('Building client...')
|
2021-09-07 09:35:55 +00:00
|
|
|
const start = Date.now()
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.restoreAll()
|
2021-09-07 09:35:55 +00:00
|
|
|
await vite.build(clientConfig)
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.wrapAll()
|
2022-10-27 10:36:37 +00:00
|
|
|
await ctx.nuxt.callHook('vite:compiled')
|
2022-12-16 11:47:12 +00:00
|
|
|
logger.success(`Client built in ${Date.now() - start}ms`)
|
2021-09-07 09:35:55 +00:00
|
|
|
}
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|