2021-07-12 10:54:24 +00:00
|
|
|
import { resolve } from 'upath'
|
2021-04-29 11:51:54 +00:00
|
|
|
import * as vite from 'vite'
|
|
|
|
import vuePlugin from '@vitejs/plugin-vue'
|
|
|
|
import consola from 'consola'
|
|
|
|
import { ViteBuildContext, ViteOptions } from './vite'
|
|
|
|
import { wpfs } from './utils/wpfs'
|
|
|
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
2021-06-16 12:42:58 +00:00
|
|
|
import { transformNuxtSetup } from './plugins/transformSetup'
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
export async function buildServer (ctx: ViteBuildContext) {
|
|
|
|
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: {
|
|
|
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server')
|
|
|
|
}
|
|
|
|
},
|
2021-04-29 11:51:54 +00:00
|
|
|
ssr: {
|
|
|
|
external: [
|
|
|
|
'axios'
|
2021-06-14 18:52:40 +00:00
|
|
|
],
|
|
|
|
noExternal: [
|
2021-07-23 14:43:06 +00:00
|
|
|
...ctx.nuxt.options.build.transpile.filter(i => typeof i === 'string'),
|
2021-06-14 18:52:40 +00:00
|
|
|
'@nuxt/app'
|
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'),
|
2021-04-29 11:51:54 +00:00
|
|
|
ssr: true,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
cacheDirPlugin(ctx.nuxt.options.rootDir, 'server'),
|
2021-06-16 12:42:58 +00:00
|
|
|
vuePlugin(),
|
|
|
|
transformNuxtSetup()
|
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)
|
|
|
|
|
|
|
|
if (!ctx.nuxt.options.ssr) {
|
|
|
|
await onBuild()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:39:48 +00:00
|
|
|
let lastBuild = 0
|
|
|
|
const build = async () => {
|
|
|
|
let start = Date.now()
|
|
|
|
// debounce
|
|
|
|
if (start - lastBuild < 300) {
|
|
|
|
await sleep(300 - (start - lastBuild) + 1)
|
|
|
|
start = Date.now()
|
|
|
|
if (start - lastBuild < 300) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastBuild = start
|
2021-04-29 11:51:54 +00:00
|
|
|
await vite.build(serverConfig)
|
|
|
|
await onBuild()
|
|
|
|
consola.info(`Server built in ${Date.now() - start}ms`)
|
2021-07-14 14:39:48 +00:00
|
|
|
}
|
2021-04-29 11:51:54 +00:00
|
|
|
|
|
|
|
await build()
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
ctx.nuxt.hook('builder:watch', () => build())
|
|
|
|
ctx.nuxt.hook('app:templatesGenerated', () => build())
|
2021-04-29 11:51:54 +00:00
|
|
|
}
|
2021-07-14 14:39:48 +00:00
|
|
|
|
|
|
|
function sleep (ms:number) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms)
|
|
|
|
})
|
|
|
|
}
|