2021-08-27 12:51:40 +00:00
|
|
|
/* eslint-disable no-use-before-define */
|
2021-10-02 16:01:17 +00:00
|
|
|
import { resolve } from 'pathe'
|
2020-11-20 00:16:31 +00:00
|
|
|
import defu from 'defu'
|
2021-08-27 12:51:40 +00:00
|
|
|
import { createHooks, Hookable, NestedHooks } from 'hookable'
|
2021-05-20 10:58:30 +00:00
|
|
|
import type { Preset } from 'unenv'
|
2021-08-27 12:51:40 +00:00
|
|
|
import type { NuxtHooks, NuxtOptions } from '@nuxt/kit'
|
2021-05-24 11:14:10 +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-04-12 21:28:48 +00:00
|
|
|
import type { AssetOptions } from './rollup/plugins/assets'
|
2021-02-18 16:06:58 +00:00
|
|
|
import type { ServerMiddleware } from './server/middleware'
|
2021-07-26 10:37:39 +00:00
|
|
|
import type { RollupConfig } from './rollup/config'
|
2021-09-22 14:39:54 +00:00
|
|
|
import type { Options as EsbuildOptions } from './rollup/plugins/esbuild'
|
2021-10-02 16:01:17 +00:00
|
|
|
import { runtimeDir } from './dirs'
|
2020-11-20 00:16:31 +00:00
|
|
|
|
2021-08-27 12:51:40 +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
|
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
|
2021-07-26 10:37:39 +00:00
|
|
|
rollupConfig?: RollupConfig
|
2021-09-22 14:39:54 +00:00
|
|
|
esbuild?: {
|
|
|
|
options?: EsbuildOptions
|
|
|
|
}
|
2021-07-26 11:32:35 +00:00
|
|
|
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[]
|
2021-02-18 16:06:58 +00:00
|
|
|
scannedMiddleware: ServerMiddleware[]
|
2021-08-27 12:51:40 +00:00
|
|
|
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
|
2021-07-15 10:18:34 +00:00
|
|
|
vfs: Record<string, string>
|
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,
|
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
|
2021-07-21 20:05:22 +00:00
|
|
|
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
|
2021-06-30 10:29:48 +00:00
|
|
|
publicDir: 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
|
2021-08-11 20:28:38 +00:00
|
|
|
modulesDir: string[]
|
2020-11-28 20:50:02 +00:00
|
|
|
runtimeConfig: { public: any, private: any }
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
|
|
|
_internal: {
|
|
|
|
runtimeDir: string
|
2021-08-27 12:51:40 +00:00
|
|
|
hooks: Hookable<NitroHooks>
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-07-26 11:32:35 +00:00
|
|
|
moduleSideEffects: ['unenv/runtime/polyfill/'],
|
2020-11-20 00:16:31 +00:00
|
|
|
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: {},
|
2021-07-15 10:18:34 +00:00
|
|
|
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',
|
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: { } },
|
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,
|
2021-07-21 20:05:22 +00:00
|
|
|
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,
|
2021-06-30 10:29:48 +00:00
|
|
|
publicDir: resolve(nuxtOptions.srcDir, nuxtOptions.dir.public || 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-28 20:50:02 +00:00
|
|
|
staticAssets: nuxtOptions.generate.staticAssets,
|
2021-08-11 20:28:38 +00:00
|
|
|
modulesDir: nuxtOptions.modulesDir,
|
2020-11-28 20:50:02 +00:00
|
|
|
runtimeConfig: {
|
|
|
|
public: nuxtOptions.publicRuntimeConfig,
|
|
|
|
private: nuxtOptions.privateRuntimeConfig
|
|
|
|
}
|
2020-11-20 00:16:31 +00:00
|
|
|
},
|
|
|
|
_internal: {
|
2021-10-02 16:01:17 +00:00
|
|
|
runtimeDir,
|
2021-08-27 12:51:40 +00:00
|
|
|
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'
|
2021-10-02 18:40:10 +00:00
|
|
|
// 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)
|
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-04-12 21:28:48 +00:00
|
|
|
// Assets
|
|
|
|
nitroContext.assets.dirs.server = {
|
2021-06-30 10:29:48 +00:00
|
|
|
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
|
|
|
}
|