Nuxt/packages/nitro/src/context.ts

188 lines
5.8 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-use-before-define */
import { resolve } from 'pathe'
2020-11-20 00:16:31 +00:00
import defu from 'defu'
import { createHooks, Hookable, NestedHooks } from 'hookable'
import type { Preset } from 'unenv'
import type { NuxtHooks, 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'
import type { RollupConfig } from './rollup/config'
import type { Options as EsbuildOptions } from './rollup/plugins/esbuild'
import { runtimeDir } from './dirs'
2020-11-20 00:16:31 +00:00
export interface NitroHooks {
'nitro:document': (htmlTemplate: { src: string, contents: string, dst: string, compiled: string }) => void
'nitro:rollup:before': (context: NitroContext) => void | Promise<void>
'nitro:compiled': (context: NitroContext) => void
'close': () => void
}
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?: RollupConfig
esbuild?: {
options?: EsbuildOptions
}
moduleSideEffects: string[]
2020-11-20 00:16:31 +00:00
renderer: string
2020-11-28 22:49:39 +00:00
serveStatic: boolean
2020-11-20 00:16:31 +00:00
middleware: ServerMiddleware[]
scannedMiddleware: ServerMiddleware[]
hooks: NestedHooks<NitroHooks>
nuxtHooks: NestedHooks<NuxtHooks>
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
modulesDir: string[]
runtimeConfig: { public: any, private: any }
2020-11-20 00:16:31 +00:00
}
_internal: {
runtimeDir: string
hooks: Hookable<NitroHooks>
2020-11-20 00:16:31 +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 = {
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,
moduleSideEffects: ['unenv/runtime/polyfill/'],
2020-11-20 00:16:31 +00:00
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,
modulesDir: nuxtOptions.modulesDir,
runtimeConfig: {
public: nuxtOptions.publicRuntimeConfig,
private: nuxtOptions.privateRuntimeConfig
}
2020-11-20 00:16:31 +00:00
},
_internal: {
runtimeDir,
hooks: createHooks<NitroHooks>()
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'
// eslint-disable-next-line import/namespace
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
}