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-02-06 23:25:24 +00:00
|
|
|
import { logger, resolveModule, resolvePath } 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-03 13:03:30 +00:00
|
|
|
import { ssrStylesPlugin } from './plugins/ssr-styles'
|
2023-02-16 15:00:40 +00:00
|
|
|
import { pureAnnotationsPlugin } from './plugins/pure-annotations'
|
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) {
|
2022-07-27 09:01:25 +00:00
|
|
|
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
|
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-03-29 10:59:57 +00:00
|
|
|
const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, {
|
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: {
|
|
|
|
devSourcemap: ctx.nuxt.options.sourcemap.server
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
define: {
|
|
|
|
'process.server': true,
|
|
|
|
'process.client': false,
|
|
|
|
'typeof window': '"undefined"',
|
|
|
|
'typeof document': '"undefined"',
|
|
|
|
'typeof navigator': '"undefined"',
|
|
|
|
'typeof location': '"undefined"',
|
|
|
|
'typeof XMLHttpRequest': '"undefined"'
|
|
|
|
},
|
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: {
|
2021-10-05 14:18:03 +00:00
|
|
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server'),
|
2022-07-21 10:44:33 +00:00
|
|
|
...ctx.nuxt.options.experimental.externalVue || ctx.nuxt.options.dev
|
2022-06-10 14:31:36 +00:00
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
'@vue/reactivity': _resolve(`@vue/reactivity/dist/reactivity.cjs${ctx.nuxt.options.dev ? '' : '.prod'}.js`),
|
|
|
|
'@vue/shared': _resolve(`@vue/shared/dist/shared.cjs${ctx.nuxt.options.dev ? '' : '.prod'}.js`),
|
2022-07-21 10:44:33 +00:00
|
|
|
'vue-router': _resolve(`vue-router/dist/vue-router.cjs${ctx.nuxt.options.dev ? '' : '.prod'}.js`),
|
|
|
|
'vue/server-renderer': _resolve('vue/server-renderer'),
|
|
|
|
'vue/compiler-sfc': _resolve('vue/compiler-sfc'),
|
|
|
|
vue: _resolve(`vue/dist/vue.cjs${ctx.nuxt.options.dev ? '' : '.prod'}.js`)
|
|
|
|
}
|
2021-07-23 14:58:38 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
ssr: {
|
2022-07-21 10:44:33 +00:00
|
|
|
external: ctx.nuxt.options.experimental.externalVue
|
|
|
|
? ['#internal/nitro', '#internal/nitro/utils', 'vue', 'vue-router']
|
|
|
|
: ['#internal/nitro', '#internal/nitro/utils'],
|
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-29 08:43:07 +00:00
|
|
|
// TODO: Use externality for production (rollup) build
|
2021-11-16 13:28:29 +00:00
|
|
|
/\/esm\/.*\.js$/,
|
2021-10-21 19:02:10 +00:00
|
|
|
/\.(es|esm|esm-browser|esm-bundler).js$/,
|
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(\/|$)/,
|
|
|
|
/(nuxt|nuxt3)\/(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: {
|
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 },
|
2022-06-10 14:31:36 +00:00
|
|
|
external: ['#internal/nitro', ...ctx.nuxt.options.experimental.externalVue ? ['vue', 'vue-router'] : []],
|
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
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
plugins: [
|
2023-02-16 15:00:40 +00:00
|
|
|
pureAnnotationsPlugin.vite({
|
|
|
|
sourcemap: ctx.nuxt.options.sourcemap.server,
|
2023-03-07 22:51:08 +00:00
|
|
|
functions: ['defineComponent', 'defineAsyncComponent', 'defineNuxtLink', 'createClientOnly', 'defineNuxtPlugin', 'defineNuxtRouteMiddleware', 'defineNuxtComponent', 'useRuntimeConfig']
|
2023-02-16 15:00:40 +00:00
|
|
|
})
|
2021-04-29 11:51:54 +00:00
|
|
|
]
|
2023-03-29 10:59:57 +00:00
|
|
|
} satisfies vite.InlineConfig)
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2023-03-08 11:56:41 +00:00
|
|
|
serverConfig.customLogger = createViteLogger(serverConfig)
|
|
|
|
|
2023-05-11 22:33:17 +00:00
|
|
|
if (!ctx.nuxt.options.dev) {
|
2022-09-07 08:41:08 +00:00
|
|
|
const chunksWithInlinedCSS = new Set<string>()
|
2022-09-03 13:03:30 +00:00
|
|
|
serverConfig.plugins!.push(ssrStylesPlugin({
|
|
|
|
srcDir: ctx.nuxt.options.srcDir,
|
2022-09-07 08:41:08 +00:00
|
|
|
chunksWithInlinedCSS,
|
2023-05-11 22:33:17 +00:00
|
|
|
shouldInline: ctx.nuxt.options.experimental.inlineSSRStyles,
|
|
|
|
components: ctx.nuxt.apps.default.components
|
2022-09-03 13:03:30 +00:00
|
|
|
}))
|
2022-09-07 08:41:08 +00:00
|
|
|
|
|
|
|
// Remove CSS entries for files that will have inlined styles
|
|
|
|
ctx.nuxt.hook('build:manifest', (manifest) => {
|
|
|
|
for (const key in manifest) {
|
|
|
|
const entry = manifest[key]
|
|
|
|
const shouldRemoveCSS = chunksWithInlinedCSS.has(key)
|
|
|
|
if (shouldRemoveCSS) {
|
|
|
|
entry.css = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-09-03 13:03:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|