2022-07-27 09:01:25 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-04-29 11:51:54 +00:00
|
|
|
import * as vite from 'vite'
|
|
|
|
import vuePlugin from '@vitejs/plugin-vue'
|
2021-10-13 10:34:51 +00:00
|
|
|
import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
|
2024-07-03 22:19:54 +00:00
|
|
|
import { logger, resolvePath, tryImportModule } from '@nuxt/kit'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { joinURL, withTrailingSlash, withoutLeadingSlash } from 'ufo'
|
2023-03-29 10:59:57 +00:00
|
|
|
import type { ViteConfig } from '@nuxt/schema'
|
2024-08-13 16:01:13 +00:00
|
|
|
import defu from 'defu'
|
2024-09-05 20:36:15 +00:00
|
|
|
import type { Nitro } from 'nitro/types'
|
2023-03-29 10:59:57 +00:00
|
|
|
import type { ViteBuildContext } from './vite'
|
2023-03-08 11:56:41 +00:00
|
|
|
import { createViteLogger } from './utils/logger'
|
2022-08-13 12:43:26 +00:00
|
|
|
import { initViteNodeServer } from './vite-node'
|
2022-09-07 08:41:08 +00:00
|
|
|
import { writeManifest } from './manifest'
|
2023-01-19 10:56:34 +00:00
|
|
|
import { transpile } from './utils/transpile'
|
2024-08-13 16:01:13 +00:00
|
|
|
import { createSourcemapPreserver } from './plugins/nitro-sourcemap'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildServer (ctx: ViteBuildContext) {
|
2023-01-14 01:27:06 +00:00
|
|
|
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.'
|
2023-02-06 23:25:24 +00:00
|
|
|
const entry = ctx.nuxt.options.ssr ? ctx.entry : await resolvePath(resolve(ctx.nuxt.options.appDir, 'entry-spa'))
|
2023-07-24 17:32:12 +00:00
|
|
|
const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, vite.mergeConfig({
|
2023-06-16 14:19:53 +00:00
|
|
|
configFile: false,
|
2022-07-21 10:44:33 +00:00
|
|
|
base: ctx.nuxt.options.dev
|
2022-07-25 09:52:21 +00:00
|
|
|
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
|
2022-07-21 10:44:33 +00:00
|
|
|
: undefined,
|
|
|
|
experimental: {
|
|
|
|
renderBuiltUrl: (filename, { type, hostType }) => {
|
|
|
|
if (hostType !== 'js') {
|
|
|
|
// In CSS we only use relative paths until we craft a clever runtime CSS hack
|
|
|
|
return { relative: true }
|
|
|
|
}
|
|
|
|
if (type === 'public') {
|
2023-01-14 01:27:06 +00:00
|
|
|
return { runtime: `${helper}__publicAssetsURL(${JSON.stringify(filename)})` }
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
|
|
|
if (type === 'asset') {
|
|
|
|
const relativeFilename = filename.replace(withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)), '')
|
2023-01-14 01:27:06 +00:00
|
|
|
return { runtime: `${helper}__buildAssetsURL(${JSON.stringify(relativeFilename)})` }
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
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.server,
|
2023-01-23 11:08:48 +00:00
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
define: {
|
|
|
|
'process.server': true,
|
|
|
|
'process.client': false,
|
2023-08-07 22:03:40 +00:00
|
|
|
'process.browser': false,
|
2023-08-10 08:51:58 +00:00
|
|
|
'import.meta.server': true,
|
|
|
|
'import.meta.client': false,
|
2023-12-14 13:17:50 +00:00
|
|
|
'import.meta.browser': false,
|
2024-04-05 18:08:32 +00:00
|
|
|
'window': 'undefined',
|
|
|
|
'document': 'undefined',
|
|
|
|
'navigator': 'undefined',
|
|
|
|
'location': 'undefined',
|
|
|
|
'XMLHttpRequest': 'undefined',
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2022-08-13 12:43:26 +00:00
|
|
|
optimizeDeps: {
|
2024-05-26 14:46:42 +00:00
|
|
|
noDiscovery: true,
|
2022-08-13 12:43:26 +00:00
|
|
|
},
|
2021-07-23 14:58:38 +00:00
|
|
|
resolve: {
|
2024-09-05 20:36:15 +00:00
|
|
|
conditions: ((ctx.nuxt as any)._nitro as Nitro)?.options.exportConditions,
|
2021-07-23 14:58:38 +00:00
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
ssr: {
|
2023-08-07 22:05:29 +00:00
|
|
|
external: [
|
2024-06-26 13:18:05 +00:00
|
|
|
'nitro/runtime',
|
2023-08-07 22:05:29 +00:00
|
|
|
],
|
2021-06-14 18:52:40 +00:00
|
|
|
noExternal: [
|
2023-01-19 10:56:34 +00:00
|
|
|
...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }),
|
2021-10-13 10:34:51 +00:00
|
|
|
'/__vue-jsx',
|
2021-10-12 01:38:30 +00:00
|
|
|
'#app',
|
2022-09-15 11:24:43 +00:00
|
|
|
/^nuxt(\/|$)/,
|
2024-04-05 18:08:32 +00:00
|
|
|
/(nuxt|nuxt3|nuxt-nightly)\/(dist|src|app)/,
|
|
|
|
],
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2024-06-15 21:02:57 +00:00
|
|
|
cacheDir: resolve(ctx.nuxt.options.rootDir, ctx.config.cacheDir ?? 'node_modules/.cache/vite', 'server'),
|
2021-04-29 11:51:54 +00:00
|
|
|
build: {
|
2023-09-28 13:13:10 +00:00
|
|
|
// we'll display this in nitro build output
|
|
|
|
reportCompressedSize: false,
|
2024-01-19 18:08:17 +00:00
|
|
|
sourcemap: ctx.nuxt.options.sourcemap.server ? ctx.config.build?.sourcemap ?? ctx.nuxt.options.sourcemap.server : false,
|
2021-07-14 14:40:40 +00:00
|
|
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/server'),
|
2023-02-06 23:25:24 +00:00
|
|
|
ssr: true,
|
2021-04-29 11:51:54 +00:00
|
|
|
rollupOptions: {
|
2023-03-21 21:35:51 +00:00
|
|
|
input: { server: entry },
|
2024-06-26 13:18:05 +00:00
|
|
|
external: ['nitro/runtime', '#internal/nuxt/paths', '#internal/nuxt/app-config'],
|
2021-07-15 09:38:06 +00:00
|
|
|
output: {
|
2023-03-21 21:35:51 +00:00
|
|
|
entryFileNames: '[name].mjs',
|
2022-12-12 15:22:04 +00:00
|
|
|
format: 'module',
|
|
|
|
generatedCode: {
|
2024-08-12 21:15:05 +00:00
|
|
|
symbols: true, // temporary fix for https://github.com/vuejs/core/issues/8351,
|
2024-04-05 18:08:32 +00:00
|
|
|
constBindings: true,
|
|
|
|
},
|
2021-07-15 09:38:06 +00:00
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
onwarn (warning, rollupWarn) {
|
2022-08-15 13:40:06 +00:00
|
|
|
if (warning.code && ['UNUSED_EXTERNAL_IMPORT'].includes(warning.code)) {
|
|
|
|
return
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|
2022-08-15 13:40:06 +00:00
|
|
|
rollupWarn(warning)
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
},
|
2022-01-05 18:31:24 +00:00
|
|
|
server: {
|
2024-07-02 17:01:52 +00:00
|
|
|
warmup: {
|
|
|
|
ssrFiles: [ctx.entry],
|
|
|
|
},
|
2022-01-05 18:31:24 +00:00
|
|
|
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
2022-07-25 12:29:41 +00:00
|
|
|
preTransformRequests: false,
|
2024-04-05 18:08:32 +00:00
|
|
|
hmr: false,
|
|
|
|
},
|
2023-07-24 17:32:12 +00:00
|
|
|
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$server || {}))
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2023-08-25 13:57:41 +00:00
|
|
|
if (!ctx.nuxt.options.dev) {
|
2024-07-03 22:19:54 +00:00
|
|
|
const { runtimeDependencies = [] } = await tryImportModule<typeof import('nitro/runtime/meta')>('nitro/runtime/meta', {
|
|
|
|
paths: ctx.nuxt.options.modulesDir,
|
|
|
|
}) || {}
|
2024-03-21 11:57:11 +00:00
|
|
|
if (Array.isArray(serverConfig.ssr!.external)) {
|
|
|
|
serverConfig.ssr!.external.push(
|
|
|
|
// explicit dependencies we use in our ssr renderer - these can be inlined (if necessary) in the nitro build
|
2024-08-28 12:28:14 +00:00
|
|
|
'unhead', '@unhead/ssr', 'unctx', 'h3', 'devalue', '@nuxt/devalue', 'radix3', 'rou3', 'unstorage', 'hookable',
|
2024-09-05 20:36:15 +00:00
|
|
|
// ensure we only have one version of vue if nitro is going to inline anyway
|
|
|
|
...((ctx.nuxt as any)._nitro as Nitro).options.inlineDynamicImports ? ['vue', '@vue/server-renderer', '@unhead/vue'] : [],
|
2024-03-21 11:57:11 +00:00
|
|
|
// dependencies we might share with nitro - these can be inlined (if necessary) in the nitro build
|
2024-07-03 22:19:54 +00:00
|
|
|
...runtimeDependencies,
|
2024-03-21 11:57:11 +00:00
|
|
|
)
|
|
|
|
}
|
2023-08-25 13:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 16:01:13 +00:00
|
|
|
// tell rollup's nitro build about the original sources of the generated vite server build
|
|
|
|
if (ctx.nuxt.options.sourcemap.server && !ctx.nuxt.options.dev) {
|
|
|
|
const { vitePlugin, nitroPlugin } = createSourcemapPreserver()
|
|
|
|
serverConfig.plugins!.push(vitePlugin)
|
|
|
|
ctx.nuxt.hook('nitro:build:before', (nitro) => {
|
|
|
|
nitro.options.rollupConfig = defu(nitro.options.rollupConfig, {
|
|
|
|
plugins: [nitroPlugin],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:56:41 +00:00
|
|
|
serverConfig.customLogger = createViteLogger(serverConfig)
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await ctx.nuxt.callHook('vite:extendConfig', serverConfig, { isClient: false, isServer: true })
|
|
|
|
|
2023-03-29 10:59:57 +00:00
|
|
|
serverConfig.plugins!.unshift(
|
|
|
|
vuePlugin(serverConfig.vue),
|
2024-04-05 18:08:32 +00:00
|
|
|
viteJsxPlugin(serverConfig.vueJsx),
|
2023-03-29 10:59:57 +00:00
|
|
|
)
|
|
|
|
|
2024-09-05 20:36:15 +00:00
|
|
|
if (!ctx.nuxt.options.dev) {
|
|
|
|
serverConfig.plugins!.push({
|
|
|
|
name: 'nuxt:nitro:vue-feature-flags',
|
|
|
|
configResolved (config) {
|
|
|
|
for (const key in config.define) {
|
|
|
|
if (key.startsWith('__VUE')) {
|
|
|
|
// tree-shake vue feature flags for non-node targets
|
|
|
|
((ctx.nuxt as any)._nitro as Nitro).options.replace[key] = config.define[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:37:06 +00:00
|
|
|
await ctx.nuxt.callHook('vite:configResolved', serverConfig, { isClient: false, isServer: true })
|
|
|
|
|
2022-10-27 10:36:37 +00:00
|
|
|
const onBuild = () => ctx.nuxt.callHook('vite:compiled')
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Production build
|
|
|
|
if (!ctx.nuxt.options.dev) {
|
|
|
|
const start = Date.now()
|
2022-02-16 21:34:32 +00:00
|
|
|
logger.info('Building server...')
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.restoreAll()
|
2021-10-05 14:18:03 +00:00
|
|
|
await vite.build(serverConfig)
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.wrapAll()
|
2022-09-07 08:41:08 +00:00
|
|
|
// Write production client manifest
|
|
|
|
await writeManifest(ctx)
|
2021-04-29 11:51:54 +00:00
|
|
|
await onBuild()
|
2022-02-16 21:34:32 +00:00
|
|
|
logger.success(`Server built in ${Date.now() - start}ms`)
|
2021-04-29 11:51:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
if (!ctx.nuxt.options.ssr) {
|
2024-08-09 16:43:42 +00:00
|
|
|
await writeManifest(ctx)
|
2021-04-29 11:51:54 +00:00
|
|
|
await onBuild()
|
2021-10-05 14:18:03 +00:00
|
|
|
return
|
2021-07-14 14:39:48 +00:00
|
|
|
}
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Start development server
|
|
|
|
const viteServer = await vite.createServer(serverConfig)
|
2022-03-11 08:41:27 +00:00
|
|
|
ctx.ssrServer = viteServer
|
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Close server on exit
|
|
|
|
ctx.nuxt.hook('close', () => viteServer.close())
|
|
|
|
|
2024-07-02 17:01:52 +00:00
|
|
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true })
|
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Initialize plugins
|
|
|
|
await viteServer.pluginContainer.buildStart({})
|
|
|
|
|
2024-06-19 12:57:08 +00:00
|
|
|
await initViteNodeServer(ctx)
|
2021-07-14 14:39:48 +00:00
|
|
|
}
|