Skip to content

Commit

Permalink
build: load presets from unbundled nitropack/presets (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored May 21, 2024
1 parent 6c3e080 commit 943f11e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 48 deletions.
7 changes: 6 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ export default defineBuildConfig({
},
},
},
externals: ["@nuxt/schema", "firebase-functions", "@scalar/api-reference", "nitropack"],
externals: [
"nitropack",
"nitropack/presets",
"firebase-functions",
"@scalar/api-reference",
],
});
4 changes: 3 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
import { runtimeDir, pkgDir } from "./dirs";
import { nitroImports } from "./imports";
import { PresetName } from "./presets";
import { resolvePreset } from "./preset";

const NitroDefaults: NitroConfig = {
// General
Expand Down Expand Up @@ -165,6 +164,9 @@ export async function loadOptions(
// Compatibility date
const compatibilityDate = process.env.NITRO_COMPATIBILITY_DATE || opts.compatibilityDate;

// Preset resolver
const { resolvePreset } = await import("nitropack/presets");

const c12Config = await (opts.watch ? watchConfig : loadConfig)(<
WatchConfigOptions
>{
Expand Down
41 changes: 0 additions & 41 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { NitroPreset, NitroPresetMeta } from "./types";
import { fileURLToPath } from "node:url";
import { kebabCase } from "scule";
import { provider } from 'std-env'

export function defineNitroPreset<
P extends NitroPreset,
Expand All @@ -17,42 +15,3 @@ export function defineNitroPreset<
}
return { ...preset, _meta: meta } as P & { _meta: M };
}

let _presets: typeof import("./presets")["presets"]

export async function resolvePreset(name: string, opts: { static?: boolean, compatibilityDate?: string }): Promise<(NitroPreset & { _meta?: NitroPresetMeta }) | undefined> {
if (!_presets) {
_presets = (await import("./presets") as typeof import("./presets")).presets;
}

const _name = kebabCase(name) || provider;
const _date = new Date(opts.compatibilityDate || 0);

const matches = _presets.filter((preset) => {
const names = [preset._meta.name, preset._meta.stdName, ...(preset._meta.aliases || [])].filter(Boolean);
if (!names.includes(_name)) {
return false;
}
if (preset._meta.compatibility?.date && new Date(preset._meta.compatibility?.date || 0) > _date) {
return false
}
return true
}).sort((a, b) => {
const aDate = new Date(a._meta.compatibility?.date || 0);
const bDate = new Date(b._meta.compatibility?.date || 0);
return bDate > aDate ? 1 : -1
});

const preset = matches.find(p => (p._meta.static || false) === (opts?.static || false)) || matches[0];

if (typeof preset === 'function') {
return preset();
}

if (!name && !preset) {
return opts?.static ? resolvePreset('static', opts) : resolvePreset('node-server', opts)
}

return preset;
}

37 changes: 37 additions & 0 deletions src/presets/_resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { NitroPreset, NitroPresetMeta } from "nitropack";
import { kebabCase } from "scule";
import { provider } from 'std-env'
import allPresets from "./_all.gen";

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

const matches = allPresets.filter((preset) => {
const names = [preset._meta.name, preset._meta.stdName, ...(preset._meta.aliases || [])].filter(Boolean);
if (!names.includes(_name)) {
return false;
}
if (preset._meta.compatibility?.date && new Date(preset._meta.compatibility?.date || 0) > _date) {
return false
}
return true
}).sort((a, b) => {
const aDate = new Date(a._meta.compatibility?.date || 0);
const bDate = new Date(b._meta.compatibility?.date || 0);
return bDate > aDate ? 1 : -1
});

const preset = matches.find(p => (p._meta.static || false) === (opts?.static || false)) || matches[0];

if (typeof preset === 'function') {
return preset();
}

if (!name && !preset) {
return opts?.static ? resolvePreset('static', opts) : resolvePreset('node-server', opts)
}

return preset;
}

3 changes: 1 addition & 2 deletions src/presets/index.ts → src/presets/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as presets } from "./_all.gen"

export type { PresetOptions, PresetName, PresetNameInput } from "./_types.gen";
export { resolvePreset } from './_resolve'
1 change: 1 addition & 0 deletions src/presets/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { resolvePreset } from './_resolve'
3 changes: 1 addition & 2 deletions src/presets/netlify/legacy/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { existsSync, promises as fsp } from "node:fs";
import { join, dirname } from "pathe";
import { defineNitroPreset } from "nitropack";
import type { Nitro } from "nitropack";
import nitroPkg from "../../../../package.json";
import { deprecateSWR, writeHeaders, writeRedirects } from "./utils";

// Netlify functions
Expand Down Expand Up @@ -111,7 +110,7 @@ const netlifyEdge = defineNitroPreset(
path: "/*",
name: "nitro server handler",
function: "server",
generator: `${nitroPkg.name}@${nitroPkg.version}`,
generator: `${nitro.options.framework.name}@${nitro.options.framework.version}`,
},
],
};
Expand Down
3 changes: 2 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pathToFileURL } from "node:url";
import { createRequire, builtinModules } from "node:module";
import { join, normalize, resolve } from "pathe";
import { dirname, join, normalize, resolve } from "pathe";
import type { InputOptions, OutputOptions, Plugin } from "rollup";
import { defu } from "defu";
// import terser from "@rollup/plugin-terser"; // TODO: Investigate jiti issue
Expand Down Expand Up @@ -431,6 +431,7 @@ export const plugins = [
"@@/",
"virtual:",
"nitropack/runtime",
dirname(nitro.options.entry),
...(nitro.options.experimental.wasm
? [(id: string) => id?.endsWith(".wasm")]
: []),
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"jsx": "preserve",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
"noEmit": true,
"paths": {
"nitropack": ["./src/index"],
"nitropack/config": ["./src/config"],
Expand Down

0 comments on commit 943f11e

Please sign in to comment.