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'
|
2022-09-09 09:54:20 +00:00
|
|
|
import { logger, resolveModule } from '@nuxt/kit'
|
2022-07-21 10:44:33 +00:00
|
|
|
import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { ViteBuildContext, ViteOptions } from './vite'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
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'
|
2022-09-07 08:41:08 +00:00
|
|
|
import { writeManifest } from './manifest'
|
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 })
|
2021-04-29 11:51:54 +00:00
|
|
|
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
2022-08-13 12:43:26 +00:00
|
|
|
entry: ctx.entry,
|
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') {
|
|
|
|
return { runtime: `globalThis.__publicAssetsURL(${JSON.stringify(filename)})` }
|
|
|
|
}
|
|
|
|
if (type === 'asset') {
|
|
|
|
const relativeFilename = filename.replace(withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)), '')
|
|
|
|
return { runtime: `globalThis.__buildAssetsURL(${JSON.stringify(relativeFilename)})` }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
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: {
|
|
|
|
entries: [ctx.entry]
|
|
|
|
},
|
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: [
|
2021-10-20 09:50:29 +00:00
|
|
|
...ctx.nuxt.options.build.transpile,
|
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
|
|
|
]
|
|
|
|
},
|
|
|
|
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'),
|
2022-01-13 12:39:23 +00:00
|
|
|
ssr: ctx.nuxt.options.ssr ?? true,
|
2021-04-29 11:51:54 +00:00
|
|
|
rollupOptions: {
|
2022-08-13 12:43:26 +00:00
|
|
|
input: ctx.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: {
|
|
|
|
entryFileNames: 'server.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: [
|
|
|
|
cacheDirPlugin(ctx.nuxt.options.rootDir, 'server'),
|
2021-10-26 12:49:18 +00:00
|
|
|
vuePlugin(ctx.config.vue),
|
2021-10-13 10:34:51 +00:00
|
|
|
viteJsxPlugin()
|
2021-04-29 11:51:54 +00:00
|
|
|
]
|
|
|
|
} as ViteOptions)
|
|
|
|
|
2022-09-03 13:03:30 +00:00
|
|
|
if (ctx.nuxt.options.experimental.inlineSSRStyles) {
|
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,
|
2022-09-03 13:03:30 +00:00
|
|
|
shouldInline: typeof ctx.nuxt.options.experimental.inlineSSRStyles === 'function'
|
|
|
|
? ctx.nuxt.options.experimental.inlineSSRStyles
|
|
|
|
: undefined
|
|
|
|
}))
|
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 })
|
|
|
|
|
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...')
|
2021-10-05 14:18:03 +00:00
|
|
|
await vite.build(serverConfig)
|
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
|
|
|
}
|