2021-10-12 14:42:44 +00:00
|
|
|
import { resolve, normalize } 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'
|
2021-10-05 14:18:03 +00:00
|
|
|
import fse from 'fs-extra'
|
|
|
|
import pDebounce from 'p-debounce'
|
2021-04-29 11:51:54 +00:00
|
|
|
import consola from 'consola'
|
2021-10-12 08:39:06 +00:00
|
|
|
import { resolveModule } from '@nuxt/kit'
|
2021-04-29 11:51:54 +00:00
|
|
|
import { ViteBuildContext, ViteOptions } from './vite'
|
|
|
|
import { wpfs } from './utils/wpfs'
|
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
2021-10-05 14:18:03 +00:00
|
|
|
import { bundleRequest } from './dev-bundler'
|
2021-10-12 14:05:20 +00:00
|
|
|
import { writeManifest } from './manifest'
|
|
|
|
import { isCSS } from './utils'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildServer (ctx: ViteBuildContext) {
|
2021-10-12 08:39:06 +00:00
|
|
|
const _resolve = id => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
|
2021-04-29 11:51:54 +00:00
|
|
|
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
|
|
|
define: {
|
|
|
|
'process.server': true,
|
|
|
|
'process.client': false,
|
|
|
|
'typeof window': '"undefined"',
|
|
|
|
'typeof document': '"undefined"',
|
|
|
|
'typeof navigator': '"undefined"',
|
|
|
|
'typeof location': '"undefined"',
|
|
|
|
'typeof XMLHttpRequest': '"undefined"'
|
|
|
|
},
|
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'),
|
|
|
|
// Alias vue
|
2021-10-12 08:39:06 +00:00
|
|
|
'vue/server-renderer': _resolve('vue/server-renderer'),
|
|
|
|
'vue/compiler-sfc': _resolve('vue/compiler-sfc'),
|
|
|
|
'@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`),
|
|
|
|
'vue-router': _resolve(`vue-router/dist/vue-router.cjs${ctx.nuxt.options.dev ? '' : '.prod'}.js`),
|
|
|
|
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: {
|
2021-10-12 08:39:06 +00:00
|
|
|
external: [],
|
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',
|
2021-10-20 09:50:29 +00:00
|
|
|
/nuxt3\/dist/,
|
|
|
|
/nuxt3\/src/,
|
|
|
|
/@nuxt\/nitro\/dist/,
|
|
|
|
/@nuxt\/nitro\/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: {
|
2021-07-15 09:38:06 +00:00
|
|
|
output: {
|
|
|
|
entryFileNames: 'server.mjs',
|
|
|
|
preferConst: true,
|
|
|
|
format: 'module'
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
onwarn (warning, rollupWarn) {
|
|
|
|
if (!['UNUSED_EXTERNAL_IMPORT'].includes(warning.code)) {
|
|
|
|
rollupWarn(warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-01-05 18:31:24 +00:00
|
|
|
server: {
|
|
|
|
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
|
|
|
preTransformRequests: false
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
consola.info('Building server...')
|
|
|
|
await vite.build(serverConfig)
|
2021-04-29 11:51:54 +00:00
|
|
|
await onBuild()
|
2021-10-05 14:18:03 +00:00
|
|
|
consola.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)
|
2021-04-29 11:51:54 +00:00
|
|
|
|
2022-01-17 10:49:53 +00:00
|
|
|
// Invalidate virtual modules when templates are re-generated
|
|
|
|
ctx.nuxt.hook('app:templatesGenerated', () => {
|
|
|
|
for (const [id, mod] of viteServer.moduleGraph.idToModuleMap) {
|
|
|
|
if (id.startsWith('\x00virtual:')) {
|
|
|
|
viteServer.moduleGraph.invalidateModule(mod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Close server on exit
|
|
|
|
ctx.nuxt.hook('close', () => viteServer.close())
|
|
|
|
|
|
|
|
// Initialize plugins
|
|
|
|
await viteServer.pluginContainer.buildStart({})
|
|
|
|
|
|
|
|
// Build and watch
|
|
|
|
const _doBuild = async () => {
|
|
|
|
const start = Date.now()
|
2021-11-30 14:37:01 +00:00
|
|
|
const { code, ids } = await bundleRequest({ viteServer }, resolve(ctx.nuxt.options.appDir, 'entry'))
|
2021-10-05 14:18:03 +00:00
|
|
|
await fse.writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'), code, 'utf-8')
|
2021-10-12 14:05:20 +00:00
|
|
|
// Have CSS in the manifest to prevent FOUC on dev SSR
|
2021-11-30 14:37:01 +00:00
|
|
|
await writeManifest(ctx, ids.filter(isCSS).map(i => i.slice(1)))
|
2021-10-05 14:18:03 +00:00
|
|
|
const time = (Date.now() - start)
|
|
|
|
consola.success(`Vite server built in ${time}ms`)
|
|
|
|
await onBuild()
|
|
|
|
}
|
|
|
|
const doBuild = pDebounce(_doBuild, 100)
|
|
|
|
|
|
|
|
// Initial build
|
|
|
|
await _doBuild()
|
2021-07-14 14:39:48 +00:00
|
|
|
|
2021-10-05 14:18:03 +00:00
|
|
|
// Watch
|
|
|
|
viteServer.watcher.on('all', (_event, file) => {
|
2021-10-12 14:42:44 +00:00
|
|
|
file = normalize(file) // Fix windows paths
|
2021-10-05 14:18:03 +00:00
|
|
|
if (file.indexOf(ctx.nuxt.options.buildDir) === 0) { return }
|
|
|
|
doBuild()
|
2021-07-14 14:39:48 +00:00
|
|
|
})
|
2021-10-05 14:18:03 +00:00
|
|
|
// ctx.nuxt.hook('builder:watch', () => doBuild())
|
|
|
|
ctx.nuxt.hook('app:templatesGenerated', () => doBuild())
|
2021-07-14 14:39:48 +00:00
|
|
|
}
|