-
Notifications
You must be signed in to change notification settings - Fork 539
/
nitro.ts
296 lines (263 loc) · 7.75 KB
/
nitro.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* eslint-disable no-use-before-define */
import type { Preset as UnenvPreset } from "unenv";
import type { Unimport } from "unimport";
import type { UnimportPluginOptions } from "unimport/unplugin";
import type { PluginVisualizerOptions } from "rollup-plugin-visualizer";
import type { NestedHooks, Hookable } from "hookable";
import type { ConsolaInstance, LogLevel } from "consola";
import type { WatchOptions } from "chokidar";
import type { RollupCommonJSOptions } from "@rollup/plugin-commonjs";
import type { RollupWasmOptions } from "@rollup/plugin-wasm";
import type { Storage, BuiltinDriverName } from "unstorage";
import type { ServerOptions as HTTPProxyOptions } from "http-proxy";
import type { ProxyOptions } from "h3";
import type { ResolvedConfig, ConfigWatcher } from "c12";
import type { TSConfig } from "pkg-types";
import type { NodeExternalsOptions } from "../rollup/plugins/externals";
import type { RollupConfig } from "../rollup/config";
import type { Options as EsbuildOptions } from "../rollup/plugins/esbuild";
import { CachedEventHandlerOptions } from "../runtime/types";
import type * as _PRESETS from "../presets";
import type {
NitroErrorHandler,
NitroDevEventHandler,
NitroEventHandler,
} from "./handler";
import type { PresetOptions } from "./presets";
import type { KebabCase } from "./utils";
export type NitroDynamicConfig = Pick<
NitroConfig,
"runtimeConfig" | "routeRules"
>;
export interface NitroRuntimeConfigApp {
baseURL: string;
[key: string]: any;
}
export interface NitroRuntimeConfig {
app: NitroRuntimeConfigApp;
nitro: {
envPrefix?: string;
routeRules?: {
[path: string]: NitroRouteConfig;
};
};
[key: string]: any;
}
export interface Nitro {
options: NitroOptions;
scannedHandlers: NitroEventHandler[];
vfs: Record<string, string>;
hooks: Hookable<NitroHooks>;
unimport?: Unimport;
logger: ConsolaInstance;
storage: Storage;
close: () => Promise<void>;
updateConfig: (config: NitroDynamicConfig) => void | Promise<void>;
/* @internal */
_prerenderedRoutes?: PrerenderGenerateRoute[];
}
export interface PrerenderRoute {
route: string;
contents?: string;
data?: ArrayBuffer;
fileName?: string;
error?: Error & { statusCode: number; statusMessage: string };
generateTimeMS?: number;
}
export interface PrerenderGenerateRoute extends PrerenderRoute {
skip?: boolean;
}
type HookResult = void | Promise<void>;
export interface NitroHooks {
"rollup:before": (nitro: Nitro, config: RollupConfig) => HookResult;
compiled: (nitro: Nitro) => HookResult;
"dev:reload": () => HookResult;
"rollup:reload": () => HookResult;
restart: () => HookResult;
close: () => HookResult;
"prerender:routes": (routes: Set<string>) => HookResult;
"prerender:route": (route: PrerenderRoute) => HookResult;
"prerender:generate": (
route: PrerenderGenerateRoute,
nitro: Nitro
) => HookResult;
}
type CustomDriverName = string & { _custom?: any };
export interface StorageMounts {
[path: string]: {
driver: BuiltinDriverName | CustomDriverName;
[option: string]: any;
};
}
type DeepPartial<T> = T extends Record<string, any>
? { [P in keyof T]?: DeepPartial<T[P]> | T[P] }
: T;
export type NitroPreset = NitroConfig | (() => NitroConfig);
export interface NitroConfig
extends DeepPartial<Omit<NitroOptions, "routeRules" | "rollupConfig">> {
extends?: string | string[] | NitroPreset;
routeRules?: { [path: string]: NitroRouteConfig };
rollupConfig?: Partial<RollupConfig>;
}
export interface AppConfig {
[key: string]: any;
}
export interface PublicAssetDir {
baseURL?: string;
fallthrough?: boolean;
maxAge: number;
dir: string;
}
export interface ServerAssetDir {
baseName: string;
dir: string;
}
export interface DevServerOptions {
watch: string[];
}
export interface CompressOptions {
gzip?: boolean;
brotli?: boolean;
}
type Enumerate<
N extends number,
Acc extends number[] = []
> = Acc["length"] extends N
? Acc[number]
: Enumerate<N, [...Acc, Acc["length"]]>;
type IntRange<F extends number, T extends number> = Exclude<
Enumerate<T>,
Enumerate<F>
>;
type HTTPStatusCode = IntRange<100, 600>;
type ExcludeFunctions<G extends Record<string, any>> = Pick<
G,
// eslint-disable-next-line @typescript-eslint/ban-types
{ [P in keyof G]: NonNullable<G[P]> extends Function ? never : P }[keyof G]
>;
export interface NitroRouteConfig {
cache?: ExcludeFunctions<CachedEventHandlerOptions> | false;
headers?: Record<string, string>;
redirect?: string | { to: string; statusCode?: HTTPStatusCode };
prerender?: boolean;
proxy?: string | ({ to: string } & ProxyOptions);
isr?: number | boolean;
// Shortcuts
cors?: boolean;
swr?: boolean | number;
static?: boolean | number;
}
export interface NitroRouteRules
extends Omit<NitroRouteConfig, "redirect" | "cors" | "swr" | "static"> {
redirect?: { to: string; statusCode: HTTPStatusCode };
proxy?: { to: string } & ProxyOptions;
}
export interface NitroOptions extends PresetOptions {
// Internal
_config: NitroConfig;
_c12: ResolvedConfig<NitroConfig> | ConfigWatcher<NitroConfig>;
// General
debug: boolean;
// eslint-disable-next-line @typescript-eslint/ban-types
preset: KebabCase<keyof typeof _PRESETS> | (string & {});
static: boolean;
logLevel: LogLevel;
runtimeConfig: NitroRuntimeConfig;
appConfig: AppConfig;
appConfigFiles: string[];
// Dirs
workspaceDir: string;
rootDir: string;
srcDir: string;
scanDirs: string[];
buildDir: string;
output: {
dir: string;
serverDir: string;
publicDir: string;
};
// Features
storage: StorageMounts;
devStorage: StorageMounts;
bundledStorage: string[];
timing: boolean;
renderer?: string;
serveStatic: boolean | "node" | "deno";
noPublicDir: boolean;
experimental?: {
wasm?: boolean | RollupWasmOptions;
legacyExternals?: boolean;
openAPI?: boolean;
/**
* See https://github.com/microsoft/TypeScript/pull/51669
*/
typescriptBundlerResolution?: boolean;
};
future: {
nativeSWR: boolean;
};
serverAssets: ServerAssetDir[];
publicAssets: PublicAssetDir[];
imports: UnimportPluginOptions | false;
plugins: string[];
virtual: Record<string, string | (() => string | Promise<string>)>;
compressPublicAssets: boolean | CompressOptions;
ignore: string[];
// Dev
dev: boolean;
devServer: DevServerOptions;
watchOptions: WatchOptions;
devProxy: Record<string, string | HTTPProxyOptions>;
// Routing
baseURL: string;
handlers: NitroEventHandler[];
routeRules: { [path: string]: NitroRouteRules };
devHandlers: NitroDevEventHandler[];
errorHandler: string;
devErrorHandler: NitroErrorHandler;
prerender: {
concurrency: number;
interval: number;
crawlLinks: boolean;
failOnError: boolean;
ignore: string[];
routes: string[];
};
// Rollup
rollupConfig?: RollupConfig;
entry: string;
unenv: UnenvPreset;
alias: Record<string, string>;
minify: boolean;
inlineDynamicImports: boolean;
sourceMap: boolean | "inline" | "hidden";
node: boolean;
moduleSideEffects: string[];
esbuild?: {
options?: Partial<EsbuildOptions>;
};
noExternals: boolean;
externals: NodeExternalsOptions;
analyze: false | PluginVisualizerOptions;
replace: Record<string, string | ((id: string) => string)>;
commonJS?: RollupCommonJSOptions;
exportConditions?: string[];
// Advanced
typescript: {
strict?: boolean;
internalPaths?: boolean;
generateTsConfig?: boolean;
/** the path of the generated `tsconfig.json`, relative to buildDir */
tsconfigPath: string;
tsConfig?: Partial<TSConfig>;
};
hooks: NestedHooks<NitroHooks>;
nodeModulesDirs: string[];
commands: {
preview: string;
deploy: string;
};
}
declare global {
const defineNitroConfig: (config: NitroConfig) => NitroConfig;
}