-
Notifications
You must be signed in to change notification settings - Fork 486
/
nitro.ts
85 lines (74 loc) · 2.35 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
import { consola } from "consola";
import { createDebugger, createHooks } from "hookable";
import { runtimeDir } from "nitropack/runtime/meta";
import type {
LoadConfigOptions,
Nitro,
NitroConfig,
NitroDynamicConfig,
} from "nitropack/types";
import { join } from "pathe";
import { createUnimport } from "unimport";
import { loadOptions } from "./config/loader";
import { updateNitroConfig } from "./config/update";
import { installModules } from "./module";
import { scanAndSyncOptions } from "./scan";
import { addNitroTasksVirtualFile } from "./task";
import { createStorage } from "./utils/storage";
export async function createNitro(
config: NitroConfig = {},
opts: LoadConfigOptions = {}
): Promise<Nitro> {
// Resolve options
const options = await loadOptions(config, opts);
// Create nitro context
const nitro: Nitro = {
options,
hooks: createHooks(),
vfs: {},
logger: consola.withTag("nitro"),
scannedHandlers: [],
close: () => nitro.hooks.callHook("close"),
storage: undefined as any,
async updateConfig(config: NitroDynamicConfig) {
updateNitroConfig(nitro, config);
},
};
// Scan dirs and sync options
// TODO: Make it side-effect free to allow proper watching
await scanAndSyncOptions(nitro);
// Storage
nitro.storage = await createStorage(nitro);
nitro.hooks.hook("close", async () => {
await nitro.storage.dispose();
});
// Debug and timing
if (nitro.options.debug) {
createDebugger(nitro.hooks, { tag: "nitro" });
nitro.options.plugins.push(join(runtimeDir, "internal/debug"));
}
if (nitro.options.timing) {
nitro.options.plugins.push(join(runtimeDir, "internal/timing"));
}
// Logger
if (nitro.options.logLevel !== undefined) {
nitro.logger.level = nitro.options.logLevel;
}
// Hooks
nitro.hooks.addHooks(nitro.options.hooks);
// Tasks
addNitroTasksVirtualFile(nitro);
// Auto imports
if (nitro.options.imports) {
// Create unimport instance
nitro.unimport = createUnimport(nitro.options.imports);
await nitro.unimport.init();
// Support for importing from '#imports'
nitro.options.virtual["#imports"] = () => nitro.unimport?.toExports() || "";
// Backward compatibility
nitro.options.virtual["#nitro"] = 'export * from "#imports"';
}
// Scan and install modules as last step
await installModules(nitro);
return nitro;
}