Nuxt/packages/nitro/src/context.ts

169 lines
5.1 KiB
TypeScript
Raw Normal View History

import { resolve, dirname } from 'upath'
2020-11-20 00:16:31 +00:00
import defu from 'defu'
import Hookable, { configHooksT } from 'hookable'
import type { Preset } from 'unenv'
import type { NuxtOptions } from '@nuxt/kit'
import { tryImport, resolvePath, detectTarget, extendPreset } from './utils'
import * as PRESETS from './presets'
import type { NodeExternalsOptions } from './rollup/plugins/externals'
2021-04-11 21:22:02 +00:00
import type { StorageOptions } from './rollup/plugins/storage'
2021-04-12 21:28:48 +00:00
import type { AssetOptions } from './rollup/plugins/assets'
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
vfs: Record<string, string>
2020-11-20 00:16:31 +00:00
output: {
dir: string
serverDir: string
publicDir: string
2020-11-20 00:16:31 +00:00
}
2021-04-11 21:22:02 +00:00
storage: StorageOptions,
2021-04-12 21:28:48 +00:00
assets: AssetOptions,
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
ssr: boolean
2020-11-20 00:16:31 +00:00
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
publicDir: 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: {},
vfs: {},
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
},
2021-04-11 21:22:02 +00:00
storage: { mounts: { } },
2021-04-12 21:28:48 +00:00
assets: {
inline: !nuxtOptions.dev,
dirs: {}
},
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,
ssr: nuxtOptions.ssr,
2020-11-20 00:16:31 +00:00
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,
publicDir: resolve(nuxtOptions.srcDir, nuxtOptions.dir.public || 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,
staticAssets: nuxtOptions.generate.staticAssets,
runtimeConfig: {
public: nuxtOptions.publicRuntimeConfig,
private: nuxtOptions.privateRuntimeConfig
}
2020-11-20 00:16:31 +00:00
},
_internal: {
runtimeDir: resolve(dirname(require.resolve('@nuxt/nitro')), '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)
2021-04-20 14:10:49 +00:00
const _preset = (extendPreset(presetDefaults /* base */, input) as Function)(_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-04-11 21:22:02 +00:00
// Dev-only storage
if (nitroContext._nuxt.dev) {
const fsMounts = {
root: resolve(nitroContext._nuxt.rootDir),
src: resolve(nitroContext._nuxt.srcDir),
build: resolve(nitroContext._nuxt.buildDir),
cache: resolve(nitroContext._nuxt.rootDir, '.nuxt/nitro/cache')
}
for (const p in fsMounts) {
nitroContext.storage.mounts[p] = nitroContext.storage.mounts[p] || {
driver: 'fs',
driverOptions: { base: fsMounts[p] }
}
}
}
2021-04-12 21:28:48 +00:00
// Assets
nitroContext.assets.dirs.server = {
dir: resolve(nitroContext._nuxt.srcDir, 'server/assets'), meta: true
2021-04-12 21:28:48 +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
}