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'
|
2023-07-04 07:27:34 +00:00
|
|
|
import { logger, resolvePath, tryResolveModule } 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'
|
|
|
|
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'
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-01-23 11:08:48 +00:00
|
|
|
css: {
|
2023-08-24 12:06:44 +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,
|
|
|
|
'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: {
|
2023-02-06 23:25:24 +00:00
|
|
|
entries: ctx.nuxt.options.ssr ? [ctx.entry] : []
|
2022-08-13 12:43:26 +00:00
|
|
|
},
|
2021-07-23 14:58:38 +00:00
|
|
|
resolve: {
|
|
|
|
alias: {
|
2023-06-28 13:49:50 +00:00
|
|
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server')
|
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: [
|
2023-08-25 13:57:41 +00:00
|
|
|
'#internal/nitro', '#internal/nitro/utils'
|
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(\/|$)/,
|
2023-10-12 14:17:38 +00:00
|
|
|
/(nuxt|nuxt3|nuxt-nightly)\/(dist|src|app)/
|
2021-04-29 11:51:54 +00:00
|
|
|
]
|
|
|
|
},
|
2023-04-14 12:25:33 +00:00
|
|
|
cacheDir: resolve(ctx.nuxt.options.rootDir, '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,
|
2022-09-08 13:52:30 +00:00
|
|
|
sourcemap: ctx.nuxt.options.sourcemap.server ? ctx.config.build?.sourcemap ?? true : 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 },
|
2023-06-28 13:49:50 +00:00
|
|
|
external: ['#internal/nitro'],
|
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: {
|
|
|
|
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)
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-01-05 18:31:24 +00:00
|
|
|
server: {
|
|
|
|
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
2022-07-25 12:29:41 +00:00
|
|
|
preTransformRequests: false,
|
|
|
|
hmr: false
|
2022-01-05 18:31:24 +00:00
|
|
|
},
|
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) {
|
|
|
|
const nitroDependencies = await tryResolveModule('nitropack/package.json', ctx.nuxt.options.modulesDir)
|
|
|
|
.then(r => import(r!)).then(r => Object.keys(r.dependencies || {})).catch(() => [])
|
|
|
|
serverConfig.ssr!.external!.push(
|
|
|
|
// explicit dependencies we use in our ssr renderer - these can be inlined (if necessary) in the nitro build
|
|
|
|
'unhead', '@unhead/ssr', 'unctx', 'h3', 'devalue', '@nuxt/devalue', 'radix3', 'unstorage', 'hookable',
|
|
|
|
// dependencies we might share with nitro - these can be inlined (if necessary) in the nitro build
|
|
|
|
...nitroDependencies
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
viteJsxPlugin(serverConfig.vueJsx)
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:28:07 +00:00
|
|
|
// Write dev client manifest
|
|
|
|
await writeManifest(ctx)
|
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
if (!ctx.nuxt.options.ssr) {
|
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
|
|
|
|
|
2022-03-14 10:19:37 +00:00
|
|
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true })
|
2022-01-17 10:49:53 +00:00
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Close server on exit
|
|
|
|
ctx.nuxt.hook('close', () => viteServer.close())
|
|
|
|
|
|
|
|
// Initialize plugins
|
|
|
|
await viteServer.pluginContainer.buildStart({})
|
|
|
|
|
2022-08-13 12:43:26 +00:00
|
|
|
if (ctx.config.devBundler !== 'legacy') {
|
|
|
|
await initViteNodeServer(ctx)
|
2022-03-11 08:41:27 +00:00
|
|
|
} else {
|
2022-08-13 12:43:26 +00:00
|
|
|
logger.info('Vite server using legacy server bundler...')
|
2022-07-27 09:01:25 +00:00
|
|
|
await import('./dev-bundler').then(r => r.initViteDevBundler(ctx, onBuild))
|
2022-03-11 08:41:27 +00:00
|
|
|
}
|
2021-07-14 14:39:48 +00:00
|
|
|
}
|