Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add compatibility date support #2511

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@
"@types/http-proxy": "^1.17.14",
"@vercel/nft": "^0.27.1",
"archiver": "^7.0.1",
"c12": "^1.10.0",
"c12": "^1.11.1",
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"citty": "^0.1.6",
"compatx": "^0.1.8",
"confbox": "^0.1.7",
"consola": "^3.2.3",
"cookie-es": "^1.1.0",
Expand Down
4 changes: 3 additions & 1 deletion playground/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({});
export default defineNitroConfig({
compatibilityDate: "2024-06-12",
});
60 changes: 47 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
copyPublicAssets,
} from "nitropack/core";
import { commonArgs } from "../common";
import type { DateString } from "compatx";

export default defineCommand({
meta: {
Expand Down Expand Up @@ -42,7 +43,7 @@ export default defineCommand({
preset: args.preset,
},
{
compatibilityDate: args.compatibilityDate || "2024-05-17",
compatibilityDate: args.compatibilityDate as DateString,
}
);
await prepare(nitro);
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default defineCommand({
rootDir,
dev: true,
preset: "nitro-dev",
_cli: { command: "dev" },
},
{
watch: true,
Expand Down
3 changes: 2 additions & 1 deletion src/core/build/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { snapshotStorage } from "../utils/storage";
import { formatRollupError } from "./error";
import { writeTypes } from "./types";
import { nitroServerName } from "../utils/nitro";
import { formatCompatibilityDate } from "compatx";

export async function buildProduction(
nitro: Nitro,
Expand All @@ -21,7 +22,7 @@ export async function buildProduction(

if (!nitro.options.static) {
nitro.logger.info(
`Building ${nitroServerName(nitro)} (preset: \`${nitro.options.preset}\`)`
`Building ${nitroServerName(nitro)} (preset: \`${nitro.options.preset}\`, compatibility date: \`${formatCompatibilityDate(nitro.options.compatibilityDate)}\` )`
);
const build = await rollup.rollup(rollupConfig).catch((error) => {
nitro.logger.error(formatRollupError(error));
Expand Down
66 changes: 46 additions & 20 deletions src/core/config/loader.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { loadConfig, watchConfig, type WatchConfigOptions } from "c12";
import { loadConfig, watchConfig } from "c12";
import { klona } from "klona/full";
import { CompatibilityDateSpec, resolveCompatibilityDates } from "compatx";
import type {
LoadConfigOptions,
NitroConfig,
NitroOptions,
NitroPresetMeta,
} from "nitropack/types";
import type { PresetName } from "nitropack/presets";

import { NitroDefaults } from "./defaults";

// Resolvers
import {
fallbackCompatibilityDate,
resolveCompatibilityOptions,
} from "./resolvers/compatibility";
import { resolvePathOptions } from "./resolvers/paths";
import { resolveImportsOptions } from "./resolvers/imports";
import { resolveRouteRulesOptions } from "./resolvers/route-rules";
Expand All @@ -21,6 +28,7 @@ import { resolveAssetsOptions } from "./resolvers/assets";
import { resolveURLOptions } from "./resolvers/url";

const configResolvers = [
resolveCompatibilityOptions,
resolvePathOptions,
resolveImportsOptions,
resolveRouteRulesOptions,
Expand Down Expand Up @@ -64,15 +72,21 @@ async function _loadUserConfig(
globalThis.defineNitroConfig = globalThis.defineNitroConfig || ((c) => c);

// Compatibility date
const compatibilityDate =
process.env.NITRO_COMPATIBILITY_DATE || opts.compatibilityDate;
let compatibilityDate: CompatibilityDateSpec | undefined =
configOverrides.compatibilityDate ||
opts.compatibilityDate ||
((process.env.NITRO_COMPATIBILITY_DATE ||
process.env.SERVER_COMPATIBILITY_DATE ||
process.env.COMPATIBILITY_DATE) as CompatibilityDateSpec);

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

const c12Config = await (opts.watch ? watchConfig : loadConfig)(<
WatchConfigOptions
>{
const loadedConfig = await (opts.watch
? watchConfig<NitroConfig & { _meta?: NitroPresetMeta }>
: loadConfig<NitroConfig & { _meta?: NitroPresetMeta }>)({
name: "nitro",
cwd: configOverrides.rootDir,
dotenv: configOverrides.dev,
Expand All @@ -81,13 +95,21 @@ async function _loadUserConfig(
...configOverrides,
preset: presetOverride,
},
defaultConfig: {
preset: (
await resolvePreset("", {
static: configOverrides.static,
compatibilityDate,
})
)?._meta?.name,
async defaultConfig({ configs }) {
if (!compatibilityDate) {
compatibilityDate =
configs.main?.compatibilityDate ||
configs.rc?.compatibilityDate ||
configs.packageJson?.compatibilityDate;
}
return {
preset: (
await resolvePreset("" /* auto detect */, {
static: configOverrides.static,
compatibilityDate: compatibilityDate || fallbackCompatibilityDate,
})
)?._meta?.name,
};
},
defaults: NitroDefaults,
jitiOptions: {
Expand All @@ -99,7 +121,7 @@ async function _loadUserConfig(
async resolve(id: string) {
const preset = await resolvePreset(id, {
static: configOverrides.static,
compatibilityDate: compatibilityDate,
compatibilityDate: compatibilityDate || fallbackCompatibilityDate,
});
if (preset) {
return {
Expand All @@ -110,16 +132,20 @@ async function _loadUserConfig(
...opts.c12,
});

const options = klona(c12Config.config) as NitroOptions;
const options = klona(loadedConfig.config) as NitroOptions;

options._config = configOverrides;
options._c12 = c12Config;
options._c12 = loadedConfig;

const _presetName =
(c12Config.layers || []).find((l) => l.config?._meta?.name)?.config?._meta
?.name || presetOverride;

(loadedConfig.layers || []).find((l) => l.config?._meta?.name)?.config
?._meta?.name || presetOverride;
options.preset = _presetName as PresetName;

options.compatibilityDate = resolveCompatibilityDates(
compatibilityDate,
options.compatibilityDate
);

return options;
}
Loading