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

refactor!: remove app config support #2526

Merged
merged 4 commits into from
Jun 13, 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
4 changes: 2 additions & 2 deletions scripts/release-nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ fi
# Release packages

echo "Publishing main package..."
npm publish --access public --tolerate-republish --tag v3
npm publish --access public --tolerate-republish --tag 3x

echo "Publishing mirror package..."
cd .mirror
npm publish --access public --tolerate-republish --tag v3
npm publish --access public --tolerate-republish --tag 3x
17 changes: 1 addition & 16 deletions src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,8 @@ export async function writeTypes(nitro: Nitro) {
const config = [
"// Generated by nitro",
`
// App Config
import type { Defu } from 'defu'

${nitro.options.appConfigFiles
.map((file, index) =>
genTypeImport(relative(typesDir, file).replace(/\.\w+$/, ""), [
{ name: "default", as: `appConfig${index}` },
])
)
.join("\n")}

type UserAppConfig = Defu<{}, [${nitro.options.appConfigFiles
.map((_, index: number) => `typeof appConfig${index}`)
.join(", ")}]>

declare module "nitro/types" {
interface AppConfig extends UserAppConfig {}`,
`,
nitro.options.typescript.generateRuntimeConfigTypes
? generateTypes(
await resolveSchema(
Expand Down
2 changes: 0 additions & 2 deletions src/core/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const NitroDefaults: NitroConfig = {
timing: isDebug,
logLevel: isTest ? 1 : 3,
runtimeConfig: { app: {}, nitro: {} },
appConfig: {},
appConfigFiles: [],

// Dirs
scanDirs: [],
Expand Down
1 change: 0 additions & 1 deletion src/core/config/resolvers/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function getNitroImportsPreset(): Preset[] {
"defineRenderHandler",
"defineRouteMeta",
"getRouteRules",
"useAppConfig",
"useEvent",
"defineTask",
"runTask",
Expand Down
14 changes: 0 additions & 14 deletions src/core/config/resolvers/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,6 @@ export async function resolvePathOptions(options: NitroOptions) {
resolve(options.srcDir, dir)
);
options.scanDirs = [...new Set(options.scanDirs)];

// Normalize app.config file paths
options.appConfigFiles ??= [];
options.appConfigFiles = options.appConfigFiles
.map((file) => _tryResolve(resolveNitroPath(file, options)))
.filter(Boolean) as string[];

// Detect app.config from scanDirs
for (const dir of options.scanDirs) {
const configFile = _tryResolve("app.config", dir);
if (configFile && !options.appConfigFiles.includes(configFile)) {
options.appConfigFiles.push(configFile);
}
}
}

function _tryResolve(
Expand Down
5 changes: 3 additions & 2 deletions src/presets/firebase/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const firebase = defineNitroPreset(
// Using the gen 1 makes this preset backwards compatible for people already using it
nitro.options.firebase = { gen: 1 };
}
nitro.options.appConfig.nitro = nitro.options.appConfig.nitro || {};
nitro.options.appConfig.nitro.firebase = nitro.options.firebase;

// Expose firebase config via runtimeConfig
nitro.options.runtimeConfig._firebase = nitro.options.firebase;

// Replace __firebaseServerFunctionName__ to actual name in entries
(rollupConfig.plugins as Plugin[]).unshift({
Expand Down
6 changes: 2 additions & 4 deletions src/presets/firebase/runtime/firebase-gen-1.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitro/runtime";
import { useAppConfig } from "nitro/runtime";

import functions from "firebase-functions";
import { toNodeListener } from "h3";
import { useNitroApp, useRuntimeConfig } from "nitro/runtime";

const nitroApp = useNitroApp();

const firebaseConfig = useAppConfig().nitro.firebase;
const firebaseConfig = useRuntimeConfig()._firebase;

export const __firebaseServerFunctionName__ = functions
.region(firebaseConfig.region ?? functions.RESET_VALUE)
Expand Down
5 changes: 2 additions & 3 deletions src/presets/firebase/runtime/firebase-gen-2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitro/runtime";
import { useAppConfig } from "nitro/runtime";
import { useNitroApp, useRuntimeConfig } from "nitro/runtime";

import { onRequest } from "firebase-functions/v2/https";
import { toNodeListener } from "h3";

const nitroApp = useNitroApp();

const firebaseConfig = useAppConfig().nitro.firebase;
const firebaseConfig = useRuntimeConfig()._firebase;

export const __firebaseServerFunctionName__ = onRequest(
{
Expand Down
4 changes: 0 additions & 4 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as unenv from "unenv";
import type { Preset } from "unenv";
import unimportPlugin from "unimport/unplugin";
import { rollup as unwasm } from "unwasm/plugin";
import { appConfig } from "./plugins/app-config";
import { database } from "./plugins/database";
import { esbuild } from "./plugins/esbuild";
import { externals } from "./plugins/externals";
Expand Down Expand Up @@ -299,9 +298,6 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {
// Database
rollupConfig.plugins.push(database(nitro));

// App.config
rollupConfig.plugins.push(appConfig(nitro));

// Handlers
rollupConfig.plugins.push(handlers(nitro));

Expand Down
25 changes: 0 additions & 25 deletions src/rollup/plugins/app-config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export { useNitroApp } from "./internal/app";

// Config
export { useRuntimeConfig, useAppConfig } from "./internal/config";
export { useRuntimeConfig } from "./internal/config";

// Storage
export { useStorage } from "./internal/storage";
Expand Down
18 changes: 0 additions & 18 deletions src/runtime/internal/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from "h3";
import { klona } from "klona";
import type { NitroRuntimeConfig } from "nitro/types";
import { appConfig as _inlineAppConfig } from "#nitro-internal-virtual/app-config";
import { type EnvOptions, applyEnv } from "./utils.env";

// Static runtime config inlined by nitro build
Expand Down Expand Up @@ -38,23 +37,6 @@ export function useRuntimeConfig<
return runtimeConfig;
}

// App config
const _sharedAppConfig = _deepFreeze(klona(_inlineAppConfig));
export function useAppConfig(event?: H3Event) {
// Backwards compatibility with ambient context
if (!event) {
return _sharedAppConfig;
}
// Reuse cached app config from event context
if (event.context.nitro.appConfig) {
return event.context.nitro.appConfig;
}
// Prepare app config for event context
const appConfig = klona(_inlineAppConfig);
event.context.nitro.appConfig = appConfig;
return appConfig;
}

// --- Utils ---

function _deepFreeze(object: Record<string, any>) {
Expand Down
7 changes: 0 additions & 7 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export interface NitroOptions extends PresetOptions {
static: boolean;
logLevel: LogLevel;
runtimeConfig: NitroRuntimeConfig;
appConfig: AppConfig;
appConfigFiles: string[];

// Dirs
workspaceDir: string;
Expand Down Expand Up @@ -286,11 +284,6 @@ export interface LoadConfigOptions {
// Partial types
// ------------------------------------------------------------

// App config
export interface AppConfig {
[key: string]: any;
}

// Public assets
export interface PublicAssetDir {
baseURL?: string;
Expand Down
3 changes: 0 additions & 3 deletions src/types/virtual/app-config.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixture/app.config.ts

This file was deleted.

3 changes: 1 addition & 2 deletions test/fixture/middleware/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
process.env.NITRO_DYNAMIC = "from-env";

export default eventHandler((event) => {
const appConfig = useAppConfig(event);
appConfig.dynamic = "from-middleware";
// ...
});
4 changes: 0 additions & 4 deletions test/fixture/routes/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
const sharedAppConfig = useAppConfig();
const sharedRuntimeConfig = useRuntimeConfig();

export default eventHandler((event) => {
const appConfig = useAppConfig(event);
const runtimeConfig = useRuntimeConfig(event);

return {
sharedAppConfig,
appConfig,
runtimeConfig,
sharedRuntimeConfig,
};
Expand Down
12 changes: 0 additions & 12 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,13 @@ export function testNitro(
url: "/config",
});
expect(data).toMatchObject({
appConfig: {
dynamic: "from-middleware",
"app-config": true,
"nitro-config": true,
"server-config": true,
},
runtimeConfig: {
dynamic: "from-env",
url: "https://test.com",
app: {
baseURL: "/",
},
},
sharedAppConfig: {
dynamic: "initial",
"app-config": true,
"nitro-config": true,
"server-config": true,
},
sharedRuntimeConfig: {
dynamic: "from-env",
// url: "https://test.com",
Expand Down