2021-04-02 11:47:01 +00:00
|
|
|
import { resolve, dirname } from 'upath'
|
2020-11-20 00:16:31 +00:00
|
|
|
import defu from 'defu'
|
2021-04-02 11:47:01 +00:00
|
|
|
import type { NuxtOptions } from '@nuxt/kit'
|
2020-11-20 00:16:31 +00:00
|
|
|
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'
|
2020-12-07 21:59:24 +00:00
|
|
|
import type { NodeExternalsOptions } from './rollup/plugins/externals'
|
2021-04-11 21:22:02 +00:00
|
|
|
import type { StorageOptions } from './rollup/plugins/storage'
|
2021-02-18 16:06:58 +00:00
|
|
|
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
|
2021-02-22 12:12:59 +00:00
|
|
|
inlineDynamicImports: boolean
|
2020-11-20 00:16:31 +00:00
|
|
|
minify: boolean
|
2020-12-07 12:36:43 +00:00
|
|
|
sourceMap: boolean
|
2020-12-07 21:59:24 +00:00
|
|
|
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[]
|
2021-02-18 16:06:58 +00:00
|
|
|
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
|
2020-11-20 11:55:55 +00:00
|
|
|
publicDir: string
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
2021-04-11 21:22:02 +00:00
|
|
|
storage: StorageOptions,
|
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
|
2021-02-18 16:06:58 +00:00
|
|
|
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
|
2020-11-28 20:50:02 +00:00
|
|
|
runtimeConfig: { public: any, private: any }
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
|
|
|
_internal: {
|
|
|
|
runtimeDir: string
|
|
|
|
hooks: Hookable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:55:55 +00:00
|
|
|
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 = {
|
2021-02-22 12:12:59 +00:00
|
|
|
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,
|
2021-02-22 12:12:59 +00:00
|
|
|
serveStatic: undefined,
|
2020-11-20 00:16:31 +00:00
|
|
|
middleware: [],
|
2021-02-18 16:06:58 +00:00
|
|
|
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',
|
2020-11-20 11:55:55 +00:00
|
|
|
publicDir: '{{ output.dir }}/public'
|
2020-11-20 00:16:31 +00:00
|
|
|
},
|
2021-04-11 21:22:02 +00:00
|
|
|
storage: { mounts: { } },
|
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,
|
2021-02-18 16:06:58 +00:00
|
|
|
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
|
2020-11-28 20:50:02 +00:00
|
|
|
staticAssets: nuxtOptions.generate.staticAssets,
|
|
|
|
runtimeConfig: {
|
|
|
|
public: nuxtOptions.publicRuntimeConfig,
|
|
|
|
private: nuxtOptions.privateRuntimeConfig
|
|
|
|
}
|
2020-11-20 00:16:31 +00:00
|
|
|
},
|
|
|
|
_internal: {
|
2021-04-02 11:47:01 +00:00
|
|
|
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)
|
|
|
|
// @ts-ignore
|
|
|
|
const _preset = extendPreset(input, presetDefaults)(_presetInput)
|
2021-02-26 19:15:41 +00:00
|
|
|
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-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
|
|
|
}
|