2022-04-20 19:12:04 +00:00
|
|
|
import { existsSync, promises as fsp } from 'node:fs'
|
2022-04-07 11:28:04 +00:00
|
|
|
import { resolve, join } from 'pathe'
|
|
|
|
import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack'
|
|
|
|
import type { NitroEventHandler, NitroDevEventHandler, NitroConfig } from 'nitropack'
|
|
|
|
import type { Nuxt } from '@nuxt/schema'
|
2022-04-19 19:10:32 +00:00
|
|
|
import { resolvePath } from '@nuxt/kit'
|
2022-04-07 11:28:04 +00:00
|
|
|
import defu from 'defu'
|
|
|
|
import fsExtra from 'fs-extra'
|
|
|
|
import { toEventHandler, dynamicEventHandler } from 'h3'
|
|
|
|
import { distDir } from '../dirs'
|
|
|
|
import { ImportProtectionPlugin } from './plugins/import-protection'
|
|
|
|
|
|
|
|
export async function initNitro (nuxt: Nuxt) {
|
2022-04-11 18:38:42 +00:00
|
|
|
// Resolve handlers
|
|
|
|
const { handlers, devHandlers } = await resolveHandlers(nuxt)
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
// Resolve config
|
|
|
|
const _nitroConfig = ((nuxt.options as any).nitro || {}) as NitroConfig
|
|
|
|
const nitroConfig: NitroConfig = defu(_nitroConfig, <NitroConfig>{
|
|
|
|
rootDir: nuxt.options.rootDir,
|
|
|
|
srcDir: join(nuxt.options.srcDir, 'server'),
|
|
|
|
dev: nuxt.options.dev,
|
|
|
|
preset: nuxt.options.dev ? 'nitro-dev' : undefined,
|
|
|
|
buildDir: nuxt.options.buildDir,
|
|
|
|
scanDirs: nuxt.options._layers.map(layer => join(layer.config.srcDir, 'server')),
|
|
|
|
renderer: resolve(distDir, 'core/runtime/nitro/renderer'),
|
2022-04-12 20:37:32 +00:00
|
|
|
errorHandler: resolve(distDir, 'core/runtime/nitro/error'),
|
2022-04-07 11:28:04 +00:00
|
|
|
nodeModulesDirs: nuxt.options.modulesDir,
|
2022-04-11 18:38:42 +00:00
|
|
|
handlers,
|
2022-04-07 11:28:04 +00:00
|
|
|
devHandlers: [],
|
|
|
|
baseURL: nuxt.options.app.baseURL,
|
|
|
|
runtimeConfig: {
|
2022-04-11 14:34:23 +00:00
|
|
|
...nuxt.options.runtimeConfig,
|
2022-04-07 11:28:04 +00:00
|
|
|
nitro: {
|
2022-04-11 14:34:23 +00:00
|
|
|
envPrefix: 'NUXT_',
|
|
|
|
...nuxt.options.runtimeConfig.nitro
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
typescript: {
|
|
|
|
generateTsConfig: false
|
|
|
|
},
|
|
|
|
publicAssets: [
|
|
|
|
{
|
|
|
|
baseURL: nuxt.options.app.buildAssetsDir,
|
|
|
|
dir: resolve(nuxt.options.buildDir, 'dist/client')
|
|
|
|
},
|
|
|
|
...nuxt.options._layers
|
2022-04-20 20:07:01 +00:00
|
|
|
.map(layer => join(layer.config.srcDir, layer.config.dir?.public || 'public'))
|
2022-04-07 11:28:04 +00:00
|
|
|
.filter(dir => existsSync(dir))
|
|
|
|
.map(dir => ({ dir }))
|
|
|
|
],
|
|
|
|
prerender: {
|
2022-04-15 16:31:19 +00:00
|
|
|
crawlLinks: nuxt.options._generate ? nuxt.options.generate.crawler : false,
|
|
|
|
routes: []
|
2022-04-19 08:17:41 +00:00
|
|
|
.concat(nuxt.options._generate ? ['/', ...nuxt.options.generate.routes] : [])
|
2022-04-15 16:31:19 +00:00
|
|
|
.concat(nuxt.options.ssr === false ? ['/', '/200', '/404'] : [])
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
2022-04-22 15:35:42 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap,
|
2022-04-07 11:28:04 +00:00
|
|
|
externals: {
|
2022-04-07 12:57:57 +00:00
|
|
|
inline: [
|
2022-04-08 00:05:27 +00:00
|
|
|
...(nuxt.options.dev ? [] : ['vue', '@vue/', '@nuxt/', nuxt.options.buildDir]),
|
2022-04-07 12:57:57 +00:00
|
|
|
'nuxt/dist',
|
|
|
|
'nuxt3/dist'
|
|
|
|
]
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
|
|
|
alias: {
|
|
|
|
'vue/compiler-sfc': 'vue/compiler-sfc',
|
2022-05-06 15:34:21 +00:00
|
|
|
'vue/server-renderer': 'vue/server-renderer',
|
2022-04-07 11:28:04 +00:00
|
|
|
vue: await resolvePath(`vue/dist/vue.cjs${nuxt.options.dev ? '' : '.prod'}.js`),
|
|
|
|
|
|
|
|
// Vue 3 mocks
|
|
|
|
'estree-walker': 'unenv/runtime/mock/proxy',
|
|
|
|
'@babel/parser': 'unenv/runtime/mock/proxy',
|
|
|
|
'@vue/compiler-core': 'unenv/runtime/mock/proxy',
|
|
|
|
'@vue/compiler-dom': 'unenv/runtime/mock/proxy',
|
|
|
|
'@vue/compiler-ssr': 'unenv/runtime/mock/proxy',
|
|
|
|
'@vue/devtools-api': 'unenv/runtime/mock/proxy',
|
|
|
|
|
|
|
|
// Paths
|
2022-04-08 00:05:27 +00:00
|
|
|
'#paths': resolve(distDir, 'core/runtime/nitro/paths'),
|
|
|
|
|
|
|
|
// Nuxt aliases
|
|
|
|
...nuxt.options.alias
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
|
|
|
replace: {
|
2022-05-06 15:34:21 +00:00
|
|
|
'process.env.NUXT_NO_SSR': nuxt.options.ssr === false
|
2022-05-06 08:33:56 +00:00
|
|
|
},
|
|
|
|
rollupConfig: {
|
|
|
|
plugins: []
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Extend nitro config with hook
|
|
|
|
await nuxt.callHook('nitro:config', nitroConfig)
|
|
|
|
|
|
|
|
// Init nitro
|
|
|
|
const nitro = await createNitro(nitroConfig)
|
|
|
|
|
|
|
|
// Expose nitro to modules
|
|
|
|
await nuxt.callHook('nitro:init', nitro)
|
|
|
|
|
|
|
|
// Connect vfs storages
|
|
|
|
nitro.vfs = nuxt.vfs = nitro.vfs || nuxt.vfs || {}
|
|
|
|
|
|
|
|
// Connect hooks
|
|
|
|
nuxt.hook('close', () => nitro.hooks.callHook('close'))
|
|
|
|
|
2022-04-20 08:52:39 +00:00
|
|
|
// Register nuxt protection patterns
|
2022-04-19 19:10:32 +00:00
|
|
|
nitro.hooks.hook('rollup:before', (nitro) => {
|
2022-04-15 07:57:23 +00:00
|
|
|
const plugin = ImportProtectionPlugin.rollup({
|
2022-04-07 11:28:04 +00:00
|
|
|
rootDir: nuxt.options.rootDir,
|
|
|
|
patterns: [
|
|
|
|
...['#app', /^#build(\/|$)/]
|
|
|
|
.map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][]
|
|
|
|
]
|
2022-04-15 07:57:23 +00:00
|
|
|
})
|
|
|
|
nitro.options.rollupConfig.plugins.push(plugin)
|
2022-04-07 11:28:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Setup handlers
|
|
|
|
const devMidlewareHandler = dynamicEventHandler()
|
|
|
|
nitro.options.devHandlers.unshift({ handler: devMidlewareHandler })
|
|
|
|
nitro.options.devHandlers.push(...devHandlers)
|
|
|
|
nitro.options.handlers.unshift({
|
2022-04-09 09:52:42 +00:00
|
|
|
route: '/__nuxt_error',
|
2022-04-07 11:28:04 +00:00
|
|
|
lazy: true,
|
|
|
|
handler: resolve(distDir, 'core/runtime/nitro/renderer')
|
|
|
|
})
|
|
|
|
|
|
|
|
// Add typed route responses
|
|
|
|
nuxt.hook('prepare:types', async (opts) => {
|
|
|
|
if (nuxt.options._prepare) {
|
|
|
|
await scanHandlers(nitro)
|
|
|
|
await writeTypes(nitro)
|
|
|
|
}
|
|
|
|
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/nitro.d.ts') })
|
|
|
|
})
|
|
|
|
|
|
|
|
// nuxt build/dev
|
|
|
|
nuxt.hook('build:done', async () => {
|
2022-05-02 20:15:47 +00:00
|
|
|
await nuxt.callHook('nitro:build:before', nitro)
|
2022-04-07 11:28:04 +00:00
|
|
|
if (nuxt.options.dev) {
|
|
|
|
await build(nitro)
|
|
|
|
} else {
|
|
|
|
await prepare(nitro)
|
|
|
|
await copyPublicAssets(nitro)
|
2022-04-15 16:31:19 +00:00
|
|
|
await prerender(nitro)
|
2022-04-20 19:12:04 +00:00
|
|
|
if (!nuxt.options._generate) {
|
|
|
|
await build(nitro)
|
|
|
|
} else {
|
|
|
|
const distDir = resolve(nuxt.options.rootDir, 'dist')
|
|
|
|
if (!existsSync(distDir)) {
|
|
|
|
await fsp.symlink(nitro.options.output.publicDir, distDir, 'junction').catch(() => {})
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// nuxt dev
|
|
|
|
if (nuxt.options.dev) {
|
|
|
|
nuxt.hook('build:compile', ({ compiler }) => {
|
|
|
|
compiler.outputFileSystem = { ...fsExtra, join } as any
|
|
|
|
})
|
|
|
|
nuxt.hook('server:devMiddleware', (m) => { devMidlewareHandler.set(toEventHandler(m)) })
|
|
|
|
nuxt.server = createDevServer(nitro)
|
|
|
|
nuxt.hook('build:resources', () => {
|
|
|
|
nuxt.server.reload()
|
|
|
|
})
|
2022-04-19 19:10:32 +00:00
|
|
|
const waitUntilCompile = new Promise<void>(resolve => nitro.hooks.hook('compiled', () => resolve()))
|
2022-04-07 11:28:04 +00:00
|
|
|
nuxt.hook('build:done', () => waitUntilCompile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resolveHandlers (nuxt: Nuxt) {
|
2022-05-06 13:31:52 +00:00
|
|
|
const handlers: NitroEventHandler[] = [...nuxt.options.serverHandlers]
|
|
|
|
const devHandlers: NitroDevEventHandler[] = [...nuxt.options.devServerHandlers]
|
2022-04-07 11:28:04 +00:00
|
|
|
|
2022-05-06 13:31:52 +00:00
|
|
|
// Map legacy serverMiddleware to handlers
|
2022-04-07 11:28:04 +00:00
|
|
|
for (let m of nuxt.options.serverMiddleware) {
|
|
|
|
if (typeof m === 'string' || typeof m === 'function' /* legacy middleware */) { m = { handler: m } }
|
|
|
|
const route = m.path || m.route || '/'
|
|
|
|
const handler = m.handler || m.handle
|
|
|
|
if (typeof handler !== 'string' || typeof route !== 'string') {
|
|
|
|
devHandlers.push({ route, handler })
|
|
|
|
} else {
|
|
|
|
delete m.handler
|
|
|
|
delete m.path
|
|
|
|
handlers.push({
|
|
|
|
...m,
|
|
|
|
route,
|
2022-05-06 13:03:38 +00:00
|
|
|
middleware: true,
|
2022-04-07 11:28:04 +00:00
|
|
|
handler: await resolvePath(handler)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
handlers,
|
|
|
|
devHandlers
|
|
|
|
}
|
|
|
|
}
|