2023-06-20 18:55:20 +00:00
|
|
|
import { existsSync, promises as fsp, readFileSync } from 'node:fs'
|
2023-06-28 14:17:19 +00:00
|
|
|
import { cpus } from 'node:os'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { join, relative, resolve } from 'pathe'
|
|
|
|
import { build, copyPublicAssets, createDevServer, createNitro, prepare, prerender, scanHandlers, writeTypes } from 'nitropack'
|
|
|
|
import type { Nitro, NitroConfig } from 'nitropack'
|
2023-06-28 13:49:50 +00:00
|
|
|
import { logger } 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-11 21:16:01 +00:00
|
|
|
import type { Nuxt } from 'nuxt/schema'
|
2023-06-20 18:55:20 +00:00
|
|
|
// @ts-expect-error TODO: add legacy type support for subpath imports
|
|
|
|
import { template as defaultSpaLoadingTemplate } from '@nuxt/ui-templates/templates/spa-loading-icon.mjs'
|
2023-03-11 21:16:01 +00:00
|
|
|
|
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/]
|
|
|
|
|
2023-06-25 16:38:31 +00:00
|
|
|
const spaLoadingTemplate = nuxt.options.spaLoadingTemplate ?? resolve(nuxt.options.srcDir, 'app/spa-loading-template.html')
|
|
|
|
if (spaLoadingTemplate && nuxt.options.spaLoadingTemplate && !existsSync(spaLoadingTemplate)) {
|
|
|
|
console.warn(`[nuxt] Could not load custom \`spaLoadingTemplate\` path as it does not exist: \`${spaLoadingTemplate}\`.`)
|
2023-06-20 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:46:40 +00:00
|
|
|
// @ts-expect-error `typescriptBundlerResolution` coming in next nitro version
|
2023-06-22 10:49:14 +00:00
|
|
|
const nitroConfig: NitroConfig = defu(_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-07-14 13:46:40 +00:00
|
|
|
experimental: {
|
|
|
|
// @ts-expect-error `typescriptBundlerResolution` coming in next nitro version
|
2023-07-15 22:48:03 +00:00
|
|
|
typescriptBundlerResolution: nuxt.options.experimental.typescriptBundlerResolution || nuxt.options.typescript?.tsConfig?.compilerOptions?.moduleResolution?.toLowerCase() === 'bundler' || _nitroConfig.typescript?.tsConfig?.compilerOptions?.moduleResolution?.toLowerCase() === 'bundler'
|
2023-07-14 13:46:40 +00:00
|
|
|
},
|
2023-01-14 01:27:06 +00:00
|
|
|
imports: {
|
2023-06-22 10:49:14 +00:00
|
|
|
autoImport: nuxt.options.imports.autoImport as boolean,
|
2023-01-14 01:27:06 +00:00
|
|
|
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,
|
2023-05-02 13:24:11 +00:00
|
|
|
filename: join(nuxt.options.analyzeDir, '{name}.html')
|
2022-08-23 13:54:39 +00:00
|
|
|
},
|
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: {
|
2023-06-20 18:55:20 +00:00
|
|
|
'#internal/nuxt.config.mjs': () => nuxt.vfs['#build/nuxt.config'],
|
|
|
|
'#spa-template': () => {
|
2023-06-25 16:38:31 +00:00
|
|
|
if (!spaLoadingTemplate) { return 'export const template = ""' }
|
2023-06-20 18:55:20 +00:00
|
|
|
try {
|
2023-06-25 16:38:31 +00:00
|
|
|
return `export const template = ${JSON.stringify(readFileSync(spaLoadingTemplate, 'utf-8'))}`
|
2023-06-20 18:55:20 +00:00
|
|
|
} catch {}
|
|
|
|
return `export const template = ${JSON.stringify(defaultSpaLoadingTemplate({}))}`
|
|
|
|
}
|
2022-11-10 11:41:02 +00:00
|
|
|
},
|
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: {
|
2023-05-15 15:36:30 +00:00
|
|
|
strict: true,
|
|
|
|
generateTsConfig: true,
|
2023-06-22 10:49:14 +00:00
|
|
|
tsconfigPath: 'tsconfig.server.json',
|
|
|
|
tsConfig: {
|
|
|
|
include: [
|
|
|
|
join(nuxt.options.buildDir, 'types/nitro-nuxt.d.ts')
|
|
|
|
]
|
|
|
|
}
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
|
|
|
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: {
|
2023-06-28 14:17:19 +00:00
|
|
|
failOnError: true,
|
|
|
|
concurrency: cpus().length * 4 || 4,
|
2023-06-29 09:14:35 +00:00
|
|
|
routes: ([] as string[]).concat(nuxt.options.generate.routes)
|
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-06-22 10:49:14 +00:00
|
|
|
...nuxt.options.build.transpile.filter((i): i is string => typeof i === 'string'),
|
2022-04-07 12:57:57 +00:00
|
|
|
'nuxt/dist',
|
2022-09-15 11:24:43 +00:00
|
|
|
'nuxt3/dist',
|
|
|
|
distDir
|
2023-04-06 11:51:32 +00:00
|
|
|
],
|
|
|
|
traceInclude: [
|
|
|
|
// force include files used in generated code from the runtime-compiler
|
2023-05-01 16:39:07 +00:00
|
|
|
...(nuxt.options.vue.runtimeCompiler && !nuxt.options.experimental.externalVue)
|
2023-04-06 11:51:32 +00:00
|
|
|
? [
|
|
|
|
...nuxt.options.modulesDir.reduce<string[]>((targets, path) => {
|
|
|
|
const serverRendererPath = resolve(path, 'vue/server-renderer/index.js')
|
|
|
|
if (existsSync(serverRendererPath)) { targets.push(serverRendererPath) }
|
|
|
|
return targets
|
|
|
|
}, [])
|
|
|
|
]
|
|
|
|
: []
|
2022-04-07 12:57:57 +00:00
|
|
|
]
|
2022-04-07 11:28:04 +00:00
|
|
|
},
|
|
|
|
alias: {
|
|
|
|
// Vue 3 mocks
|
2023-05-01 16:39:07 +00:00
|
|
|
...nuxt.options.vue.runtimeCompiler || nuxt.options.experimental.externalVue
|
2023-04-06 11:51:32 +00:00
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
'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,
|
2023-04-07 10:34:35 +00:00
|
|
|
'process.env.NUXT_JSON_PAYLOADS': !!nuxt.options.experimental.renderJsonPayloads,
|
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
|
|
|
}
|
2023-06-22 10:49:14 +00:00
|
|
|
} satisfies NitroConfig)
|
2022-04-07 11:28:04 +00:00
|
|
|
|
2023-04-07 13:19:53 +00:00
|
|
|
// Resolve user-provided paths
|
|
|
|
nitroConfig.srcDir = resolve(nuxt.options.rootDir, nuxt.options.srcDir, nitroConfig.srcDir!)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-11 22:33:17 +00:00
|
|
|
if (nuxt.options.builder === '@nuxt/webpack-builder' || nuxt.options.dev) {
|
2022-09-03 13:03:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-03 10:39:01 +00:00
|
|
|
// Add backward-compatible middleware to respect `x-nuxt-no-ssr` header
|
|
|
|
if (nuxt.options.experimental.respectNoSSRHeader) {
|
|
|
|
nitroConfig.handlers = nitroConfig.handlers || []
|
|
|
|
nitroConfig.handlers.push({
|
|
|
|
handler: resolve(distDir, 'core/runtime/nitro/no-ssr'),
|
|
|
|
middleware: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2023-06-22 10:49:14 +00:00
|
|
|
// TODO: extract to shared utility?
|
|
|
|
const excludedAlias = [/^@vue\/.*$/, '#imports', '#vue-router', 'vue-demi', /^#app/]
|
|
|
|
const basePath = nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl ? resolve(nuxt.options.buildDir, nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl) : nuxt.options.buildDir
|
|
|
|
const aliases = nitroConfig.alias!
|
|
|
|
const tsConfig = nitroConfig.typescript!.tsConfig!
|
|
|
|
tsConfig.compilerOptions = tsConfig.compilerOptions || {}
|
|
|
|
tsConfig.compilerOptions.paths = tsConfig.compilerOptions.paths || {}
|
|
|
|
for (const _alias in aliases) {
|
|
|
|
const alias = _alias as keyof typeof aliases
|
|
|
|
if (excludedAlias.some(pattern => typeof pattern === 'string' ? alias === pattern : pattern.test(alias))) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (alias in tsConfig.compilerOptions.paths) { continue }
|
|
|
|
|
|
|
|
const absolutePath = resolve(basePath, aliases[alias]!)
|
|
|
|
const stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */)
|
|
|
|
if (stats?.isDirectory()) {
|
|
|
|
tsConfig.compilerOptions.paths[alias] = [absolutePath]
|
|
|
|
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${absolutePath}/*`]
|
|
|
|
} else {
|
|
|
|
tsConfig.compilerOptions.paths[alias] = [absolutePath.replace(/(?<=\w)\.\w+$/g, '')] /* remove extension */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
// Init nitro
|
|
|
|
const nitro = await createNitro(nitroConfig)
|
|
|
|
|
2023-08-04 07:47:42 +00:00
|
|
|
// Set prerender-only options
|
|
|
|
nitro.options._config.storage ||= {}
|
|
|
|
nitro.options._config.storage['internal:nitro:prerender'] = { driver: 'memory' }
|
|
|
|
nitro.options._config.storage['internal:nitro:prerender:island'] = { driver: 'lruCache', max: 1000 }
|
|
|
|
nitro.options._config.storage['internal:nitro:prerender:payload'] = { driver: 'lruCache', max: 1000 }
|
|
|
|
|
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) => {
|
2023-07-20 13:22:10 +00:00
|
|
|
return nuxt.callHook('prerender:routes', { routes })
|
2022-11-03 21:03:12 +00:00
|
|
|
})
|
2022-04-07 11:28:04 +00:00
|
|
|
|
2023-04-06 11:51:32 +00:00
|
|
|
// Enable runtime compiler client side
|
2023-05-01 16:39:07 +00:00
|
|
|
if (nuxt.options.vue.runtimeCompiler) {
|
2023-04-06 11:51:32 +00:00
|
|
|
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
|
|
|
|
if (isClient) {
|
|
|
|
if (Array.isArray(config.resolve!.alias)) {
|
|
|
|
config.resolve!.alias.push({
|
|
|
|
find: 'vue',
|
|
|
|
replacement: 'vue/dist/vue.esm-bundler'
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
config.resolve!.alias = {
|
|
|
|
...config.resolve!.alias,
|
|
|
|
vue: 'vue/dist/vue.esm-bundler'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
nuxt.hook('webpack:config', (configuration) => {
|
|
|
|
const clientConfig = configuration.find(config => config.name === 'client')
|
|
|
|
if (!clientConfig!.resolve) { clientConfig!.resolve!.alias = {} }
|
|
|
|
if (Array.isArray(clientConfig!.resolve!.alias)) {
|
|
|
|
clientConfig!.resolve!.alias.push({
|
|
|
|
name: 'vue',
|
|
|
|
alias: 'vue/dist/vue.esm-bundler'
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
clientConfig!.resolve!.alias!.vue = 'vue/dist/vue.esm-bundler'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2023-06-11 20:41:22 +00:00
|
|
|
if (!nuxt.options.dev && nuxt.options.experimental.noVueServer) {
|
2023-03-23 09:04:40 +00:00
|
|
|
nitro.hooks.hook('rollup:before', (nitro) => {
|
|
|
|
if (nitro.options.preset === 'nitro-prerender') { return }
|
|
|
|
const nuxtErrorHandler = nitro.options.handlers.findIndex(h => h.route === '/__nuxt_error')
|
|
|
|
if (nuxtErrorHandler >= 0) {
|
|
|
|
nitro.options.handlers.splice(nuxtErrorHandler, 1)
|
|
|
|
}
|
2023-03-23 13:03:21 +00:00
|
|
|
|
2023-03-23 09:04:40 +00:00
|
|
|
nitro.options.renderer = undefined
|
|
|
|
nitro.options.errorHandler = '#internal/nitro/error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
// 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') })
|
|
|
|
})
|
|
|
|
|
2023-06-29 09:14:35 +00:00
|
|
|
if (nitro.options.static) {
|
|
|
|
nitro.hooks.hook('prerender:routes', (routes) => {
|
|
|
|
for (const route of [nuxt.options.ssr ? '/' : '/index.html', '/200.html', '/404.html']) {
|
|
|
|
routes.add(route)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
// 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)
|
2023-06-22 10:00:50 +00:00
|
|
|
|
|
|
|
logger.restoreAll()
|
|
|
|
await build(nitro)
|
|
|
|
logger.wrapAll()
|
|
|
|
|
2023-06-29 09:14:35 +00:00
|
|
|
if (nitro.options.static) {
|
2022-04-20 19:12:04 +00:00
|
|
|
const distDir = resolve(nuxt.options.rootDir, 'dist')
|
|
|
|
if (!existsSync(distDir)) {
|
2023-03-23 09:04:40 +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)
|
|
|
|
}
|
|
|
|
}
|