2022-07-25 11:02:42 +00:00
|
|
|
import { resolveTSConfig } from 'pkg-types'
|
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-07-27 09:01:25 +00:00
|
|
|
import { logger, resolveModule } from '@nuxt/kit'
|
2022-07-21 10:44:33 +00:00
|
|
|
import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { ViteBuildContext, ViteOptions } from './vite'
|
|
|
|
import { wpfs } from './utils/wpfs'
|
|
|
|
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'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildServer (ctx: ViteBuildContext) {
|
2022-08-13 12:43:26 +00:00
|
|
|
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry ||
|
2022-09-03 13:03:30 +00:00
|
|
|
(ctx.nuxt.options.vite.devBundler === 'vite-node' && ctx.nuxt.options.dev)
|
2022-08-13 12:43:26 +00:00
|
|
|
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
|
|
|
|
|
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-04-20 08:52:39 +00:00
|
|
|
/(nuxt|nuxt3)\/(dist|src|app)/,
|
2022-03-02 17:54:53 +00:00
|
|
|
/@nuxt\/nitro\/(dist|src)/
|
2021-04-29 11:51:54 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
build: {
|
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',
|
|
|
|
preferConst: true,
|
2022-07-06 19:19:16 +00:00
|
|
|
// TODO: https://github.com/vitejs/vite/pull/8641
|
2022-08-08 13:25:58 +00:00
|
|
|
inlineDynamicImports: !ctx.nuxt.options.experimental.viteServerDynamicImports,
|
2021-07-15 09:38:06 +00:00
|
|
|
format: 'module'
|
|
|
|
},
|
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) {
|
|
|
|
serverConfig.plugins!.push(ssrStylesPlugin({
|
|
|
|
srcDir: ctx.nuxt.options.srcDir,
|
|
|
|
shouldInline: typeof ctx.nuxt.options.experimental.inlineSSRStyles === 'function'
|
|
|
|
? ctx.nuxt.options.experimental.inlineSSRStyles
|
|
|
|
: undefined
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:28:42 +00:00
|
|
|
// Add type-checking
|
|
|
|
if (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev)) {
|
|
|
|
const checker = await import('vite-plugin-checker').then(r => r.default)
|
2022-08-15 13:40:06 +00:00
|
|
|
serverConfig.plugins!.push(checker({
|
2022-07-25 11:02:42 +00:00
|
|
|
vueTsc: {
|
|
|
|
tsconfigPath: await resolveTSConfig(ctx.nuxt.options.rootDir)
|
|
|
|
}
|
|
|
|
}))
|
2022-04-15 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:51:54 +00:00
|
|
|
await ctx.nuxt.callHook('vite:extendConfig', serverConfig, { isClient: false, isServer: true })
|
|
|
|
|
|
|
|
const onBuild = () => ctx.nuxt.callHook('build:resources', wpfs)
|
|
|
|
|
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)
|
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) {
|
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
|
|
|
}
|