Nuxt/packages/nitro/src/context.ts

134 lines
3.7 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'
export interface ServerMiddleware {
route: string
handle: string
2020-11-20 20:06:59 +00:00
lazy?: boolean // Default is true
promisify?: boolean // Default is true
2020-11-20 00:16:31 +00:00
}
export interface SigmaContext {
timing: boolean
inlineChunks: boolean
minify: boolean
externals: boolean
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[]
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: {
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
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]> }
export interface SigmaInput extends DeepPartial<SigmaContext> {}
2020-11-20 00:16:31 +00:00
export type SigmaPreset = SigmaInput | ((input: SigmaInput) => SigmaInput)
export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): SigmaContext {
const defaults: SigmaContext = {
timing: true,
inlineChunks: true,
minify: true,
externals: false,
analyze: false,
entry: undefined,
node: undefined,
preset: undefined,
rollupConfig: undefined,
renderer: undefined,
2020-11-28 22:49:39 +00:00
serveStatic: false,
2020-11-20 00:16:31 +00:00
middleware: [],
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: {
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,
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
}
}
defaults.preset = input.preset || process.env.SIGMA_PRESET || detectTarget() || 'server'
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 sigmaContext: SigmaContext = defu(input, _preset, defaults) as any
sigmaContext.output.dir = resolvePath(sigmaContext, sigmaContext.output.dir)
sigmaContext.output.publicDir = resolvePath(sigmaContext, sigmaContext.output.publicDir)
2020-11-20 00:16:31 +00:00
sigmaContext.output.serverDir = resolvePath(sigmaContext, sigmaContext.output.serverDir)
2020-11-20 01:38:06 +00:00
sigmaContext._internal.hooks.addHooks(sigmaContext.hooks)
2020-11-20 00:16:31 +00:00
// console.log(sigmaContext)
// process.exit(1)
return sigmaContext
}