Nuxt/packages/nitro/src/context.ts

137 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-11-20 00:16:31 +00:00
import { resolve } from 'upath'
import defu from 'defu'
import type { NuxtOptions } from '@nuxt/types'
import Hookable, { configHooksT } from 'hookable'
2020-11-21 11:42:02 +00:00
import type { Preset } from '@nuxt/un'
2020-11-20 00:16:31 +00:00
import { tryImport, resolvePath, detectTarget, extendPreset } from './utils'
import * as PRESETS from './presets'
import type { NodeExternalsOptions } from './rollup/plugins/externals'
import type { ServerMiddleware } from './server/middleware'
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
export interface NitroContext {
2020-11-20 00:16:31 +00:00
timing: boolean
inlineDynamicImports: boolean
2020-11-20 00:16:31 +00:00
minify: boolean
2020-12-07 12:36:43 +00:00
sourceMap: boolean
externals: boolean | NodeExternalsOptions
2020-11-20 00:16:31 +00:00
analyze: boolean
entry: string
node: boolean
preset: string
rollupConfig?: any
renderer: string
2020-11-28 22:49:39 +00:00
serveStatic: boolean
2020-11-20 00:16:31 +00:00
middleware: ServerMiddleware[]
scannedMiddleware: ServerMiddleware[]
2020-11-20 00:16:31 +00:00
hooks: configHooksT
2020-11-20 01:38:06 +00:00
nuxtHooks: configHooksT
2020-11-20 00:16:31 +00:00
ignore: string[]
2020-11-21 11:42:02 +00:00
env: Preset
2020-11-20 00:16:31 +00:00
output: {
dir: string
serverDir: string
publicDir: string
2020-11-20 00:16:31 +00:00
}
_nuxt: {
2021-01-18 11:42:00 +00:00
majorVersion: number
2020-11-20 00:16:31 +00:00
dev: boolean
rootDir: string
2020-11-20 00:38:35 +00:00
srcDir: string
2020-11-20 00:16:31 +00:00
buildDir: string
2020-11-20 01:38:06 +00:00
generateDir: string
2020-11-20 00:16:31 +00:00
staticDir: string
serverDir: string
2020-11-20 00:16:31 +00:00
routerBase: string
publicPath: string
2020-11-20 02:22:22 +00:00
isStatic: boolean
2020-11-20 00:16:31 +00:00
fullStatic: boolean
staticAssets: any
runtimeConfig: { public: any, private: any }
2020-11-20 00:16:31 +00:00
}
_internal: {
runtimeDir: string
hooks: Hookable
}
}
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> }
2021-01-22 19:55:59 +00:00
export interface NitroInput extends DeepPartial<NitroContext> {}
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
export type NitroPreset = NitroInput | ((input: NitroInput) => NitroInput)
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): NitroContext {
const defaults: NitroContext = {
timing: undefined,
inlineDynamicImports: undefined,
minify: undefined,
sourceMap: undefined,
externals: undefined,
analyze: undefined,
2020-11-20 00:16:31 +00:00
entry: undefined,
node: undefined,
preset: undefined,
rollupConfig: undefined,
renderer: undefined,
serveStatic: undefined,
2020-11-20 00:16:31 +00:00
middleware: [],
scannedMiddleware: [],
2020-11-20 00:16:31 +00:00
ignore: [],
2020-11-21 11:42:02 +00:00
env: {},
2020-11-20 00:16:31 +00:00
hooks: {},
2020-11-20 01:38:06 +00:00
nuxtHooks: {},
2020-11-20 00:16:31 +00:00
output: {
dir: '{{ _nuxt.rootDir }}/.output',
serverDir: '{{ output.dir }}/server',
publicDir: '{{ output.dir }}/public'
2020-11-20 00:16:31 +00:00
},
_nuxt: {
2021-01-18 11:42:00 +00:00
majorVersion: nuxtOptions._majorVersion || 2,
2020-11-20 00:16:31 +00:00
dev: nuxtOptions.dev,
rootDir: nuxtOptions.rootDir,
2020-11-20 00:38:35 +00:00
srcDir: nuxtOptions.srcDir,
2020-11-20 00:16:31 +00:00
buildDir: nuxtOptions.buildDir,
2020-11-20 01:38:06 +00:00
generateDir: nuxtOptions.generate.dir,
2020-11-20 00:16:31 +00:00
staticDir: nuxtOptions.dir.static,
serverDir: resolve(nuxtOptions.srcDir, (nuxtOptions.dir as any).server || 'server'),
2020-11-20 00:16:31 +00:00
routerBase: nuxtOptions.router.base,
publicPath: nuxtOptions.build.publicPath,
2020-11-20 02:22:22 +00:00
isStatic: nuxtOptions.target === 'static' && !nuxtOptions.dev,
fullStatic: nuxtOptions.target === 'static' && !nuxtOptions._legacyGenerate,
2020-11-20 00:16:31 +00:00
// @ts-ignore
staticAssets: nuxtOptions.generate.staticAssets,
runtimeConfig: {
public: nuxtOptions.publicRuntimeConfig,
private: nuxtOptions.privateRuntimeConfig
}
2020-11-20 00:16:31 +00:00
},
_internal: {
runtimeDir: resolve(__dirname, './runtime'),
2020-11-20 01:38:06 +00:00
hooks: new Hookable()
2020-11-20 00:16:31 +00:00
}
}
2021-01-22 19:55:59 +00:00
defaults.preset = input.preset || process.env.NITRO_PRESET || detectTarget() || 'server'
2020-11-20 00:16:31 +00:00
let presetDefaults = PRESETS[defaults.preset] || tryImport(nuxtOptions.rootDir, defaults.preset)
if (!presetDefaults) {
throw new Error('Cannot resolve preset: ' + defaults.preset)
}
presetDefaults = presetDefaults.default || presetDefaults
const _presetInput = defu(input, defaults)
// @ts-ignore
const _preset = extendPreset(input, presetDefaults)(_presetInput)
const nitroContext: NitroContext = defu(_preset, defaults) as any
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
nitroContext.output.dir = resolvePath(nitroContext, nitroContext.output.dir)
nitroContext.output.publicDir = resolvePath(nitroContext, nitroContext.output.publicDir)
nitroContext.output.serverDir = resolvePath(nitroContext, nitroContext.output.serverDir)
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
nitroContext._internal.hooks.addHooks(nitroContext.hooks)
2020-11-20 01:38:06 +00:00
2021-01-22 19:55:59 +00:00
// console.log(nitroContext)
2020-11-20 00:16:31 +00:00
// process.exit(1)
2021-01-22 19:55:59 +00:00
return nitroContext
2020-11-20 00:16:31 +00:00
}