2022-04-20 19:12:04 +00:00
|
|
|
import { existsSync, promises as fsp } from 'node:fs'
|
2023-03-08 21:14:06 +00:00
|
|
|
import { resolve, join, relative } from 'pathe'
|
2022-12-11 21:44:52 +00:00
|
|
|
import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack'
|
|
|
|
import type { NitroConfig, Nitro } from 'nitropack'
|
2022-12-19 11:57:08 +00:00
|
|
|
import { logger, resolvePath } from '@nuxt/kit'
|
2022-11-15 13:52:16 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2023-01-30 12:09:48 +00:00
|
|
|
import { defu } from 'defu'
|
2022-04-07 11:28:04 +00:00
|
|
|
import fsExtra from 'fs-extra'
|
2022-10-15 18:42:57 +00:00
|
|
|
import { dynamicEventHandler } from 'h3'
|
2023-03-08 15:32:24 +00:00
|
|
|
import { createHeadCore } from '@unhead/vue'
|
2022-11-15 16:26:38 +00:00
|
|
|
import { renderSSRHead } from '@unhead/ssr'
|
2023-03-11 21:16:01 +00:00
|
|
|
import type { Nuxt } from 'nuxt/schema'
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
import { distDir } from '../dirs'
|
|
|
|
import { ImportProtectionPlugin } from './plugins/import-protection'
|
|
|
|
|
2022-09-15 16:10:50 +00:00
|
|
|
export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
2022-04-07 11:28:04 +00:00
|
|
|
// Resolve config
|
|
|
|
const _nitroConfig = ((nuxt.options as any).nitro || {}) as NitroConfig
|
2023-02-07 17:18:47 +00:00
|
|
|
|
|
|
|
const excludePaths = nuxt.options._layers
|
|
|
|
.flatMap(l => [
|
|
|
|
l.cwd.match(/(?<=\/)node_modules\/(.+)$/)?.[1],
|
|
|
|
l.cwd.match(/\.pnpm\/.+\/node_modules\/(.+)$/)?.[1]
|
|
|
|
])
|
|
|
|
.filter((dir): dir is string => Boolean(dir))
|
|
|
|
.map(dir => escapeRE(dir))
|
|
|
|
const excludePattern = excludePaths.length
|
|
|
|
? [new RegExp(`node_modules\\/(?!${excludePaths.join('|')})`)]
|
|
|
|
: [/node_modules/]
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
const nitroConfig: NitroConfig = defu(_nitroConfig, <NitroConfig>{
|
2022-10-15 10:56:15 +00:00
|
|
|
debug: nuxt.options.debug,
|
2022-04-07 11:28:04 +00:00
|
|
|
rootDir: nuxt.options.rootDir,
|
2022-09-12 20:06:17 +00:00
|
|
|
workspaceDir: nuxt.options.workspaceDir,
|
2022-10-10 10:49:44 +00:00
|
|
|
srcDir: nuxt.options.serverDir,
|
2022-04-07 11:28:04 +00:00
|
|
|
dev: nuxt.options.dev,
|
|
|
|
buildDir: nuxt.options.buildDir,
|
2023-01-14 01:27:06 +00:00
|
|
|
imports: {
|
|
|
|
imports: [
|
|
|
|
{
|
|
|
|
as: '__buildAssetsURL',
|
|
|
|
name: 'buildAssetsURL',
|
|
|
|
from: resolve(distDir, 'core/runtime/nitro/paths')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
as: '__publicAssetsURL',
|
|
|
|
name: 'publicAssetsURL',
|
|
|
|
from: resolve(distDir, 'core/runtime/nitro/paths')
|
2023-03-14 09:54:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// TODO: Remove after https://github.com/unjs/nitro/issues/1049
|
|
|
|
as: 'defineAppConfig',
|
|
|
|
name: 'defineAppConfig',
|
|
|
|
from: resolve(distDir, 'core/runtime/nitro/config'),
|
|
|
|
priority: -1
|
2023-01-14 01:27:06 +00:00
|
|
|
}
|
2023-02-07 17:18:47 +00:00
|
|
|
],
|
|
|
|
exclude: [...excludePattern, /[\\/]\.git[\\/]/]
|
2023-01-14 01:27:06 +00:00
|
|
|
},
|
2022-11-15 13:52:16 +00:00
|
|
|
esbuild: {
|
2023-02-07 17:18:47 +00:00
|
|
|
options: { exclude: excludePattern }
|
2022-11-15 13:52:16 +00:00
|
|
|
},
|
2022-08-23 13:54:39 +00:00
|
|
|
analyze: nuxt.options.build.analyze && {
|
|
|
|
template: 'treemap',
|
|
|
|
projectRoot: nuxt.options.rootDir,
|
|
|
|
filename: join(nuxt.options.rootDir, '.nuxt/stats', '{name}.html')
|
|
|
|
},
|
2022-10-10 10:49:44 +00:00
|
|
|
scanDirs: nuxt.options._layers.map(layer => (layer.config.serverDir || layer.config.srcDir) && resolve(layer.cwd, layer.config.serverDir || resolve(layer.config.srcDir, 'server'))).filter(Boolean),
|
2022-04-07 11:28:04 +00:00
|
|
|
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-10-27 10:36:37 +00:00
|
|
|
handlers: nuxt.options.serverHandlers,
|
2022-04-07 11:28:04 +00:00
|
|
|
devHandlers: [],
|
|
|
|
baseURL: nuxt.options.app.baseURL,
|
2022-11-10 11:41:02 +00:00
|
|
|
virtual: {
|
|
|
|
'#internal/nuxt.config.mjs': () => nuxt.vfs['#build/nuxt.config']
|
|
|
|
},
|
2022-10-18 18:01:23 +00:00
|
|
|
routeRules: {
|
|
|
|
'/__nuxt_error': { cache: false }
|
|
|
|
},
|
2022-04-07 11:28:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2023-03-14 09:54:59 +00:00
|
|
|
appConfig: nuxt.options.appConfig,
|
|
|
|
appConfigFiles: nuxt.options._layers.map(
|
|
|
|
layer => resolve(layer.config.srcDir, 'app.config')
|
|
|
|
),
|
2022-04-07 11:28:04 +00:00
|
|
|
typescript: {
|
|
|
|
generateTsConfig: false
|
|
|
|
},
|
|
|
|
publicAssets: [
|
2023-02-09 06:02:07 +00:00
|
|
|
nuxt.options.dev
|
|
|
|
? { dir: resolve(nuxt.options.buildDir, 'dist/client') }
|
|
|
|
: {
|
|
|
|
dir: join(nuxt.options.buildDir, 'dist/client', nuxt.options.app.buildAssetsDir),
|
2023-02-28 13:06:31 +00:00
|
|
|
maxAge: 31536000 /* 1 year */,
|
2023-02-09 06:02:07 +00:00
|
|
|
baseURL: nuxt.options.app.buildAssetsDir
|
|
|
|
},
|
2022-04-07 11:28:04 +00:00
|
|
|
...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-10-27 10:36:37 +00:00
|
|
|
crawlLinks: nuxt.options._generate ?? undefined,
|
2022-08-12 17:47:58 +00:00
|
|
|
routes: ([] as string[])
|
2022-10-03 14:17:46 +00:00
|
|
|
.concat(nuxt.options.generate.routes)
|
|
|
|
.concat(nuxt.options._generate ? [nuxt.options.ssr ? '/' : '/index.html', '/200.html', '/404.html'] : [])
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
2022-09-07 11:32:10 +00:00
|
|
|
sourceMap: nuxt.options.sourcemap.server,
|
2022-04-07 11:28:04 +00:00
|
|
|
externals: {
|
2022-04-07 12:57:57 +00:00
|
|
|
inline: [
|
2022-06-10 14:31:36 +00:00
|
|
|
...(nuxt.options.dev
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
...nuxt.options.experimental.externalVue ? [] : ['vue', '@vue/'],
|
|
|
|
'@nuxt/',
|
|
|
|
nuxt.options.buildDir
|
|
|
|
]),
|
2023-01-13 23:19:03 +00:00
|
|
|
...nuxt.options.build.transpile.filter(i => typeof i === 'string'),
|
2022-04-07 12:57:57 +00:00
|
|
|
'nuxt/dist',
|
2022-09-15 11:24:43 +00:00
|
|
|
'nuxt3/dist',
|
|
|
|
distDir
|
2022-04-07 12:57:57 +00:00
|
|
|
]
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
|
|
|
alias: {
|
2022-06-10 14:31:36 +00:00
|
|
|
...nuxt.options.experimental.externalVue
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
'vue/compiler-sfc': 'vue/compiler-sfc',
|
|
|
|
'vue/server-renderer': 'vue/server-renderer',
|
|
|
|
vue: await resolvePath(`vue/dist/vue.cjs${nuxt.options.dev ? '' : '.prod'}.js`)
|
|
|
|
},
|
2022-04-07 11:28:04 +00:00
|
|
|
// 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',
|
2022-08-17 14:44:36 +00:00
|
|
|
'@vue/devtools-api': 'vue-devtools-stub',
|
2022-04-07 11:28:04 +00:00
|
|
|
|
|
|
|
// 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-07-12 12:32:07 +00:00
|
|
|
'process.env.NUXT_NO_SSR': nuxt.options.ssr === false,
|
2022-10-17 20:20:13 +00:00
|
|
|
'process.env.NUXT_EARLY_HINTS': nuxt.options.experimental.writeEarlyHints !== false,
|
2022-09-26 09:52:43 +00:00
|
|
|
'process.env.NUXT_NO_SCRIPTS': !!nuxt.options.experimental.noScripts && !nuxt.options.dev,
|
2022-09-03 13:03:30 +00:00
|
|
|
'process.env.NUXT_INLINE_STYLES': !!nuxt.options.experimental.inlineSSRStyles,
|
2022-09-16 10:14:41 +00:00
|
|
|
'process.env.NUXT_PAYLOAD_EXTRACTION': !!nuxt.options.experimental.payloadExtraction,
|
2022-11-24 12:24:14 +00:00
|
|
|
'process.env.NUXT_COMPONENT_ISLANDS': !!nuxt.options.experimental.componentIslands,
|
2022-08-11 22:33:21 +00:00
|
|
|
'process.dev': nuxt.options.dev,
|
|
|
|
__VUE_PROD_DEVTOOLS__: false
|
2022-05-06 08:33:56 +00:00
|
|
|
},
|
|
|
|
rollupConfig: {
|
2022-12-12 15:22:04 +00:00
|
|
|
output: {},
|
2022-05-06 08:33:56 +00:00
|
|
|
plugins: []
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-11-15 16:26:38 +00:00
|
|
|
// Add head chunk for SPA renders
|
|
|
|
const head = createHeadCore()
|
|
|
|
head.push(nuxt.options.app.head)
|
|
|
|
const headChunk = await renderSSRHead(head)
|
|
|
|
nitroConfig.virtual!['#head-static'] = `export default ${JSON.stringify(headChunk)}`
|
|
|
|
|
2022-07-07 16:51:42 +00:00
|
|
|
// Add fallback server for `ssr: false`
|
|
|
|
if (!nuxt.options.ssr) {
|
2022-08-12 17:47:58 +00:00
|
|
|
nitroConfig.virtual!['#build/dist/server/server.mjs'] = 'export default () => {}'
|
2023-02-09 06:02:07 +00:00
|
|
|
// In case a non-normalized absolute path is called for on Windows
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
nitroConfig.virtual!['#build/dist/server/server.mjs'.replace(/\//g, '\\')] = 'export default () => {}'
|
|
|
|
}
|
2022-07-07 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 13:03:30 +00:00
|
|
|
if (!nuxt.options.experimental.inlineSSRStyles) {
|
|
|
|
nitroConfig.virtual!['#build/dist/server/styles.mjs'] = 'export default {}'
|
2023-02-09 06:02:07 +00:00
|
|
|
// In case a non-normalized absolute path is called for on Windows
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
nitroConfig.virtual!['#build/dist/server/styles.mjs'.replace(/\//g, '\\')] = 'export default {}'
|
|
|
|
}
|
2022-09-03 13:03:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 10:55:32 +00:00
|
|
|
// Register nuxt protection patterns
|
2022-12-12 15:22:04 +00:00
|
|
|
nitroConfig.rollupConfig!.plugins = await nitroConfig.rollupConfig!.plugins || []
|
|
|
|
nitroConfig.rollupConfig!.plugins = Array.isArray(nitroConfig.rollupConfig!.plugins) ? nitroConfig.rollupConfig!.plugins : [nitroConfig.rollupConfig!.plugins]
|
2022-10-18 09:36:06 +00:00
|
|
|
nitroConfig.rollupConfig!.plugins!.push(
|
|
|
|
ImportProtectionPlugin.rollup({
|
|
|
|
rootDir: nuxt.options.rootDir,
|
|
|
|
patterns: [
|
|
|
|
...['#app', /^#build(\/|$)/]
|
|
|
|
.map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][]
|
|
|
|
],
|
|
|
|
exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/]
|
2023-02-05 12:31:53 +00:00
|
|
|
})
|
2022-10-18 09:36:06 +00:00
|
|
|
)
|
2022-07-12 10:55:32 +00:00
|
|
|
|
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)
|
|
|
|
|
2022-09-15 16:10:50 +00:00
|
|
|
// Expose nitro to modules and kit
|
|
|
|
nuxt._nitro = nitro
|
2022-04-07 11:28:04 +00:00
|
|
|
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-11-03 21:03:12 +00:00
|
|
|
nitro.hooks.hook('prerender:routes', (routes) => {
|
|
|
|
nuxt.callHook('prerender:routes', { routes })
|
|
|
|
})
|
2022-04-07 11:28:04 +00:00
|
|
|
|
|
|
|
// Setup handlers
|
2022-08-26 10:09:29 +00:00
|
|
|
const devMiddlewareHandler = dynamicEventHandler()
|
|
|
|
nitro.options.devHandlers.unshift({ handler: devMiddlewareHandler })
|
2022-10-27 10:36:37 +00:00
|
|
|
nitro.options.devHandlers.push(...nuxt.options.devServerHandlers)
|
2022-04-07 11:28:04 +00:00
|
|
|
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) => {
|
2022-07-21 13:50:41 +00:00
|
|
|
if (!nuxt.options.dev) {
|
2022-04-07 11:28:04 +00:00
|
|
|
await scanHandlers(nitro)
|
|
|
|
await writeTypes(nitro)
|
|
|
|
}
|
2023-03-08 21:14:06 +00:00
|
|
|
// Exclude nitro output dir from typescript
|
|
|
|
opts.tsConfig.exclude = opts.tsConfig.exclude || []
|
|
|
|
opts.tsConfig.exclude.push(relative(nuxt.options.buildDir, resolve(nuxt.options.rootDir, nitro.options.output.dir)))
|
2022-04-07 11:28:04 +00:00
|
|
|
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)
|
2023-03-14 10:34:55 +00:00
|
|
|
await nuxt.callHook('nitro:build:public-assets', nitro)
|
2022-04-15 16:31:19 +00:00
|
|
|
await prerender(nitro)
|
2022-04-20 19:12:04 +00:00
|
|
|
if (!nuxt.options._generate) {
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.restoreAll()
|
2022-04-20 19:12:04 +00:00
|
|
|
await build(nitro)
|
2022-12-19 11:57:08 +00:00
|
|
|
logger.wrapAll()
|
2022-04-20 19:12:04 +00:00
|
|
|
} else {
|
|
|
|
const distDir = resolve(nuxt.options.rootDir, 'dist')
|
|
|
|
if (!existsSync(distDir)) {
|
2022-10-27 10:36:37 +00:00
|
|
|
await fsp.symlink(nitro.options.output.publicDir, distDir, 'junction').catch(() => { })
|
2022-04-20 19:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// nuxt dev
|
|
|
|
if (nuxt.options.dev) {
|
2022-10-27 10:36:37 +00:00
|
|
|
nuxt.hook('webpack:compile', ({ compiler }) => { compiler.outputFileSystem = { ...fsExtra, join } as any })
|
|
|
|
nuxt.hook('webpack:compiled', () => { nuxt.server.reload() })
|
|
|
|
nuxt.hook('vite:compiled', () => { nuxt.server.reload() })
|
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
nuxt.hook('server:devHandler', (h) => { devMiddlewareHandler.set(h) })
|
2022-04-07 11:28:04 +00:00
|
|
|
nuxt.server = createDevServer(nitro)
|
2022-10-27 10:36:37 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|