Skip to content

Commit f95c4f5

Browse files
committedMay 27, 2024·
chore: format
1 parent ad100f4 commit f95c4f5

File tree

15 files changed

+108
-66
lines changed

15 files changed

+108
-66
lines changed
 

‎package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
"nitro": "./dist/cli/index.mjs",
4747
"nitropack": "./dist/cli/index.mjs"
4848
},
49-
"files": [
50-
"dist",
51-
"*.d.ts"
52-
],
49+
"files": ["dist", "*.d.ts"],
5350
"scripts": {
5451
"build": "pnpm gen-presets && unbuild",
5552
"dev": "pnpm nitro dev playground",

‎scripts/gen-presets.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const jitiRequire = createJITI(presetsDir, {
2424
esmResolve: true,
2525
interopDefault: true,
2626
alias: {
27-
"nitropack": fileURLToPath(new URL("../src/index.ts", import.meta.url))
28-
}
27+
nitropack: fileURLToPath(new URL("../src/index.ts", import.meta.url)),
28+
},
2929
});
3030
const allPresets: (NitroPreset & { _meta?: NitroPresetMeta })[] = [];
3131
for (const preset of presetDirs) {
@@ -76,19 +76,34 @@ const presetsWithType = presetDirs.filter((presetDir) => {
7676
const presetPath = resolve(presetsDir, presetDir, "preset.ts");
7777
const content = readFileSync(presetPath, "utf8");
7878
const typeExports = findTypeExports(content);
79-
return typeExports.some(type => type.name === "PresetOptions")
79+
return typeExports.some((type) => type.name === "PresetOptions");
8080
});
8181
writeFileSync(
8282
resolve(presetsDir, "_types.gen.ts"),
8383
/* ts */ `${autoGenHeader}
84-
${presetsWithType.map((preset) => `import { PresetOptions as ${pascalCase(preset)}Options } from "./${preset}/preset";`).join("\n")}
84+
${presetsWithType
85+
.map(
86+
(preset) =>
87+
`import { PresetOptions as ${pascalCase(
88+
preset
89+
)}Options } from "./${preset}/preset";`
90+
)
91+
.join("\n")}
8592
8693
export interface PresetOptions {
87-
${presetsWithType.map((preset) => ` ${camelCase(preset)}: ${pascalCase(preset)}Options;`).join("\n")}
94+
${presetsWithType
95+
.map((preset) => ` ${camelCase(preset)}: ${pascalCase(preset)}Options;`)
96+
.join("\n")}
8897
}
8998
9099
export type PresetName = ${names.map((name) => `"${name}"`).join(" | ")};
91100
92-
export type PresetNameInput = ${names.flatMap((name) => [...new Set([kebabCase(name), camelCase(name), snakeCase(name)])].map(n => `"${n}"`)).join(" | ")} | (string & {});
101+
export type PresetNameInput = ${names
102+
.flatMap((name) =>
103+
[...new Set([kebabCase(name), camelCase(name), snakeCase(name)])].map(
104+
(n) => `"${n}"`
105+
)
106+
)
107+
.join(" | ")} | (string & {});
93108
`
94109
);

‎src/cli/commands/build.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ export default defineCommand({
3030
},
3131
async run({ args }) {
3232
const rootDir = resolve((args.dir || args._dir || ".") as string);
33-
const nitro = await createNitro({
34-
rootDir,
35-
dev: false,
36-
minify: args.minify,
37-
preset: args.preset,
38-
}, {
39-
compatibilityDate: args.compatibilityDate || "2024-05-17",
40-
});
33+
const nitro = await createNitro(
34+
{
35+
rootDir,
36+
dev: false,
37+
minify: args.minify,
38+
preset: args.preset,
39+
},
40+
{
41+
compatibilityDate: args.compatibilityDate || "2024-05-17",
42+
}
43+
);
4144
await prepare(nitro);
4245
await copyPublicAssets(nitro);
4346
await prerender(nitro);

‎src/options.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { isTest, isDebug, nodeMajorVersion, provider } from "std-env";
99
import { findWorkspaceDir } from "pkg-types";
1010
import consola from "consola";
1111
import { version } from "../package.json";
12-
import {
13-
resolvePath,
14-
resolveFile,
15-
provideFallbackValues,
16-
} from "./utils";
12+
import { resolvePath, resolveFile, provideFallbackValues } from "./utils";
1713
import type {
1814
NitroConfig,
1915
NitroOptions,
@@ -162,7 +158,8 @@ export async function loadOptions(
162158
globalThis.defineNitroConfig = globalThis.defineNitroConfig || ((c) => c);
163159

164160
// Compatibility date
165-
const compatibilityDate = process.env.NITRO_COMPATIBILITY_DATE || opts.compatibilityDate;
161+
const compatibilityDate =
162+
process.env.NITRO_COMPATIBILITY_DATE || opts.compatibilityDate;
166163

167164
// Preset resolver
168165
const { resolvePreset } = await import("nitropack/presets");
@@ -179,10 +176,12 @@ export async function loadOptions(
179176
preset: presetOverride,
180177
},
181178
defaultConfig: {
182-
preset: (await resolvePreset("", {
183-
static: configOverrides.static,
184-
compatibilityDate
185-
}))?._meta?.name
179+
preset: (
180+
await resolvePreset("", {
181+
static: configOverrides.static,
182+
compatibilityDate,
183+
})
184+
)?._meta?.name,
186185
},
187186
defaults: NitroDefaults,
188187
jitiOptions: {
@@ -191,10 +190,10 @@ export async function loadOptions(
191190
"nitropack/config": "nitropack/config",
192191
},
193192
},
194-
async resolve (id: string) {
193+
async resolve(id: string) {
195194
const preset = await resolvePreset(id, {
196195
static: configOverrides.static,
197-
compatibilityDate: compatibilityDate
196+
compatibilityDate: compatibilityDate,
198197
});
199198
if (preset) {
200199
return {
@@ -208,7 +207,9 @@ export async function loadOptions(
208207
options._config = configOverrides;
209208
options._c12 = c12Config;
210209

211-
const _presetName = (c12Config.layers || []).find((l) => l.config?._meta?.name)?.config?._meta?.name || presetOverride
210+
const _presetName =
211+
(c12Config.layers || []).find((l) => l.config?._meta?.name)?.config?._meta
212+
?.name || presetOverride;
212213
options.preset = _presetName as PresetName;
213214

214215
options.rootDir = resolve(options.rootDir || ".");

‎src/presets/_resolve.ts

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
11
import type { NitroPreset, NitroPresetMeta } from "nitropack";
22
import { kebabCase } from "scule";
3-
import { provider } from 'std-env'
3+
import { provider } from "std-env";
44
import allPresets from "./_all.gen";
55

6-
export async function resolvePreset(name: string, opts: { static?: boolean, compatibilityDate?: string }): Promise<(NitroPreset & { _meta?: NitroPresetMeta }) | undefined> {
6+
export async function resolvePreset(
7+
name: string,
8+
opts: { static?: boolean; compatibilityDate?: string }
9+
): Promise<(NitroPreset & { _meta?: NitroPresetMeta }) | undefined> {
710
const _name = kebabCase(name) || provider;
811
const _date = new Date(opts.compatibilityDate || 0);
912

10-
const matches = allPresets.filter((preset) => {
11-
const names = [preset._meta.name, preset._meta.stdName, ...(preset._meta.aliases || [])].filter(Boolean);
12-
if (!names.includes(_name)) {
13-
return false;
14-
}
15-
if (preset._meta.compatibility?.date && new Date(preset._meta.compatibility?.date || 0) > _date) {
16-
return false
17-
}
18-
return true
19-
}).sort((a, b) => {
20-
const aDate = new Date(a._meta.compatibility?.date || 0);
21-
const bDate = new Date(b._meta.compatibility?.date || 0);
22-
return bDate > aDate ? 1 : -1
23-
});
13+
const matches = allPresets
14+
.filter((preset) => {
15+
const names = [
16+
preset._meta.name,
17+
preset._meta.stdName,
18+
...(preset._meta.aliases || []),
19+
].filter(Boolean);
20+
if (!names.includes(_name)) {
21+
return false;
22+
}
23+
if (
24+
preset._meta.compatibility?.date &&
25+
new Date(preset._meta.compatibility?.date || 0) > _date
26+
) {
27+
return false;
28+
}
29+
return true;
30+
})
31+
.sort((a, b) => {
32+
const aDate = new Date(a._meta.compatibility?.date || 0);
33+
const bDate = new Date(b._meta.compatibility?.date || 0);
34+
return bDate > aDate ? 1 : -1;
35+
});
2436

25-
const preset = matches.find(p => (p._meta.static || false) === (opts?.static || false)) || matches[0];
37+
const preset =
38+
matches.find(
39+
(p) => (p._meta.static || false) === (opts?.static || false)
40+
) || matches[0];
2641

27-
if (typeof preset === 'function') {
42+
if (typeof preset === "function") {
2843
return preset();
2944
}
3045

3146
if (!name && !preset) {
32-
return opts?.static ? resolvePreset('static', opts) : resolvePreset('node-server', opts)
47+
return opts?.static
48+
? resolvePreset("static", opts)
49+
: resolvePreset("node-server", opts);
3350
}
3451

3552
return preset;
3653
}
37-

‎src/presets/cloudflare/preset.ts

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const cloudflarePagesStatic = defineNitroPreset(
7070
stdName: "cloudflare_pages",
7171
url: import.meta.url,
7272
static: true,
73-
7473
}
7574
);
7675

‎src/presets/edgio/preset.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = async function entry (port) {
109109
}
110110
);
111111

112-
export default [edgio] as const
112+
export default [edgio] as const;
113113

114114
async function writeFile(path: string, contents: string) {
115115
await fsp.mkdir(dirname(path), { recursive: true });

‎src/presets/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export type { PresetOptions, PresetName, PresetNameInput } from "./_types.gen";
2-
export { resolvePreset } from './_resolve'
2+
export { resolvePreset } from "./_resolve";

‎src/presets/index.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { resolvePreset } from './_resolve'
1+
export { resolvePreset } from "./_resolve";

‎src/presets/netlify/legacy/preset.ts

-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ const netlifyStatic = defineNitroPreset(
152152
name: "netlify-static" as const,
153153
url: import.meta.url,
154154
static: true,
155-
156155
}
157156
);
158157

‎src/presets/zeabur/preset.ts

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const zeaburStatic = defineNitroPreset(
7373
name: "zeabur-static" as const,
7474
url: import.meta.url,
7575
static: true,
76-
7776
}
7877
);
7978

‎src/rollup/config.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ import { sourcemapMininify } from "./plugins/sourcemap-min";
4343
export type RollupConfig = InputOptions & { output: OutputOptions };
4444

4545
export const getRollupConfig = (nitro: Nitro): RollupConfig => {
46-
const extensions: string[] = [".ts", ".mjs", ".js", ".json", ".node", ".tsx", ".jsx"];
46+
const extensions: string[] = [
47+
".ts",
48+
".mjs",
49+
".js",
50+
".json",
51+
".node",
52+
".tsx",
53+
".jsx",
54+
];
4755

4856
const nodePreset = nitro.options.node === false ? unenv.nodeless : unenv.node;
4957

‎src/types/nitro.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import type {
2323
NitroDevEventHandler,
2424
NitroEventHandler,
2525
} from "./handler";
26-
import type { PresetName, PresetNameInput, PresetOptions } from "../presets/_types.gen";
26+
import type {
27+
PresetName,
28+
PresetNameInput,
29+
PresetOptions,
30+
} from "../presets/_types.gen";
2731
import { NitroModule, NitroModuleInput } from "./module";
2832
import { ProviderName } from "std-env";
2933

@@ -149,7 +153,9 @@ export interface NitroPresetMeta {
149153
}
150154

151155
export interface NitroConfig
152-
extends DeepPartial<Omit<NitroOptions, "routeRules" | "rollupConfig" | "preset">>,
156+
extends DeepPartial<
157+
Omit<NitroOptions, "routeRules" | "rollupConfig" | "preset">
158+
>,
153159
C12InputConfig<NitroConfig> {
154160
preset?: PresetNameInput;
155161
extends?: string | string[] | NitroPreset;

‎src/utils/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export function replaceAll(input: string, from: string, to: string) {
100100
return input.replace(new RegExp(from, "g"), to);
101101
}
102102

103-
104103
export async function isDirectory(path: string) {
105104
try {
106105
return (await fsp.stat(path)).isDirectory();

‎test/tests.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const getPresetTmpDir = (preset: string) => {
6464

6565
export async function setupTest(
6666
preset: string,
67-
opts: { config?: _nitro.NitroConfig, compatibilityDate?: string } = {}
67+
opts: { config?: _nitro.NitroConfig; compatibilityDate?: string } = {}
6868
) {
6969
const presetTmpDir = getPresetTmpDir(preset);
7070

@@ -121,10 +121,10 @@ export async function setupTest(
121121
dir: ctx.outDir,
122122
},
123123
timing: !ctx.isWorker,
124-
})
125-
const nitro = (ctx.nitro = await createNitro(
126-
config, { compatibilityDate: opts.compatibilityDate || "2024-05-17" }
127-
));
124+
});
125+
const nitro = (ctx.nitro = await createNitro(config, {
126+
compatibilityDate: opts.compatibilityDate || "2024-05-17",
127+
}));
128128

129129
if (ctx.isDev) {
130130
// Setup development server

0 commit comments

Comments
 (0)
Please sign in to comment.