Skip to content

Commit

Permalink
feat: provide static process.* and import.meta.* build flags (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jun 21, 2023
1 parent 0f1832c commit dbf7a90
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import type { Preset } from "unenv";
import { sanitizeFilePath, resolvePath } from "mlly";
import unimportPlugin from "unimport/unplugin";
import { hash } from "ohash";
import type { Nitro } from "../types";
import type { Nitro, NitroStaticBuildFlags } from "../types";
import { resolveAliases } from "../utils";
import { runtimeDir } from "../dirs";
import { version } from "../../package.json";
import { replace } from "./plugins/replace";
import { virtual } from "./plugins/virtual";
import { dynamicRequire } from "./plugins/dynamic-require";
Expand Down Expand Up @@ -163,6 +164,7 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {
if (nitro.options.preset === "nitro-prerender") {
NODE_ENV = "prerender";
}

const buildEnvVars = {
NODE_ENV,
prerender: nitro.options.preset === "nitro-prerender",
Expand All @@ -172,6 +174,18 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {
DEBUG: nitro.options.dev,
};

const staticFlags: NitroStaticBuildFlags = {
dev: nitro.options.dev,
preset: nitro.options.preset,
prerender: nitro.options.preset === "nitro-prerender",
server: true,
client: false,
nitro: true,
// @ts-expect-error
"versions.nitro": version,
"versions?.nitro": version,
};

// Universal import.meta
rollupConfig.plugins.push(importMeta(nitro));

Expand Down Expand Up @@ -208,6 +222,18 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {
JSON.stringify(val),
])
),
...Object.fromEntries(
Object.entries(staticFlags).map(([key, val]) => [
`process.${key}`,
JSON.stringify(val),
])
),
...Object.fromEntries(
Object.entries(staticFlags).map(([key, val]) => [
`import.meta.${key}`,
JSON.stringify(val),
])
),
...nitro.options.replace,
},
})
Expand Down
24 changes: 24 additions & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { NitroOptions } from "./nitro";

export interface NitroStaticBuildFlags {
dev?: boolean;
client?: boolean;
nitro?: boolean;
prerender?: boolean;
preset?: NitroOptions["preset"];
server?: boolean;
versions?: {
nitro?: string;
};
}

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface Process extends NitroStaticBuildFlags {}
}

interface ImportMeta extends NitroStaticBuildFlags {}
}

export {};
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./fetch";
export * from "./global";
export * from "./serialize";
export * from "./nitro";
export * from "./handler";
Expand Down
13 changes: 13 additions & 0 deletions test/fixture/routes/static-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line require-await
export default eventHandler(async (event) => {
return {
dev: [process.dev, import.meta.dev],
preset: [process.preset, import.meta.preset],
prerender: [process.prerender, import.meta.prerender],
server: [process.server, import.meta.server],
client: [process.client, import.meta.client],
nitro: [process.nitro, import.meta.nitro],
"versions.nitro": [process.versions.nitro, import.meta.versions.nitro],
"versions?.nitro": [process.versions?.nitro, import.meta.versions?.nitro],
};
});
17 changes: 17 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,21 @@ export function testNitro(
expect(headers["server-timing"]).toMatch(/-;dur=\d+;desc="Generate"/);
});
}

it("static build flags", async () => {
const { data } = await callHandler({ url: "/static-flags" });
expect(data).toMatchObject({
dev: [ctx.isDev, ctx.isDev],
preset: [ctx.preset, ctx.preset],
prerender: [
ctx.preset === "nitro-prerenderer",
ctx.preset === "nitro-prerenderer",
],
client: [false, false],
nitro: [true, true],
server: [true, true],
"versions.nitro": [expect.any(String), expect.any(String)],
"versions?.nitro": [expect.any(String), expect.any(String)],
});
});
}

0 comments on commit dbf7a90

Please sign in to comment.