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'
|
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) {
|
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)})` }
|
|
|
|
}
|
|
|
|
},
|
2023-01-23 11:08:48 +00:00
|
|
|
css: {
|
2023-08-24 12:06:44 +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,
|
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
|
|
|
},
|
2023-05-14 11:56:15 +00:00
|
|
|
dedupe: [
|
2023-07-04 07:27:34 +00:00
|
|
|
'vue'
|
2023-05-14 11:56:15 +00:00
|
|
|
]
|
2021-07-23 14:58:38 +00:00
|
|
|
},
|
2023-04-14 12:25:33 +00:00
|
|
|
cacheDir: resolve(ctx.nuxt.options.rootDir, 'node_modules/.cache/vite', 'client'),
|
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,
|
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: {
|
2023-03-21 21:35:51 +00:00
|
|
|
input: { entry: ctx.entry }
|
2022-08-13 12:43:26 +00:00
|
|
|
}
|
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,
|
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
|
|
|
}),
|
2023-01-31 14:04:55 +00:00
|
|
|
runtimePathsPlugin({
|
2023-08-24 12:06:44 +00:00
|
|
|
sourcemap: !!ctx.nuxt.options.sourcemap.client
|
2023-01-31 14:04:55 +00:00
|
|
|
}),
|
2023-10-30 21:04:45 +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
|
|
|
}
|
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
|
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'))
|
2023-03-31 09:56:10 +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) {
|
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)
|
|
|
|
})
|
2023-03-21 21:35:51 +00:00
|
|
|
clientConfig.server = defu(clientConfig.server, <ServerOptions> {
|
2022-10-27 10:36:37 +00:00
|
|
|
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
|
2023-10-23 11:19:16 +00:00
|
|
|
if (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-11-03 22:35:13 +00:00
|
|
|
if (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),
|
|
|
|
viteJsxPlugin(clientConfig.vueJsx)
|
|
|
|
)
|
|
|
|
|
2023-04-29 22:37:06 +00:00
|
|
|
await ctx.nuxt.callHook('vite:configResolved', 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-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()
|
|
|
|
}
|
|
|
|
})
|
2023-01-16 16:04:16 +00:00
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
const viteMiddleware = defineEventHandler(async (event) => {
|
2023-01-16 16:04:16 +00:00
|
|
|
const viteRoutes = viteServer.middlewares.stack.map(m => m.route).filter(r => r.length > 1)
|
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
|
|
|
}
|