From 8f5b0c76608dd504dc88401f429d4c34daf7ca98 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:01:57 +0200 Subject: [PATCH 1/8] feat: provide static `process` and `import.meta` flags --- src/rollup/config.ts | 24 ++++++++++++++++++++++ src/types/nitro.ts | 15 ++++++++++++++ test/fixture/routes/env-flags.ts | 12 +++++++++++ test/tests.ts | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 test/fixture/routes/env-flags.ts diff --git a/src/rollup/config.ts b/src/rollup/config.ts index acdde87472..6a799cb79d 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -21,6 +21,7 @@ import { hash } from "ohash"; import type { Nitro } 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"; @@ -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", @@ -172,6 +174,16 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { DEBUG: nitro.options.dev, }; + const envFlags = { + dev: nitro.options.dev, + preset: nitro.options.preset, + prerender: nitro.options.preset === "nitro-prerender", + server: true, + client: false, + nitro: true, + "versions.nitro": version, + }; + // Universal import.meta rollupConfig.plugins.push(importMeta(nitro)); @@ -208,6 +220,18 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { JSON.stringify(val), ]) ), + ...Object.fromEntries( + Object.entries(envFlags).map(([key, val]) => [ + `process.${key}`, + JSON.stringify(val), + ]) + ), + ...Object.fromEntries( + Object.entries(envFlags).map(([key, val]) => [ + `import.meta.${key}`, + JSON.stringify(val), + ]) + ), ...nitro.options.replace, }, }) diff --git a/src/types/nitro.ts b/src/types/nitro.ts index f370755a8c..46dea5522a 100644 --- a/src/types/nitro.ts +++ b/src/types/nitro.ts @@ -278,4 +278,19 @@ export interface NitroOptions extends PresetOptions { declare global { const defineNitroConfig: (config: NitroConfig) => NitroConfig; + + // eslint-disable-next-line @typescript-eslint/no-namespace + namespace NodeJS { + interface Global { + dev?: boolean; + client?: boolean; + nitro?: boolean; + prerender?: boolean; + preset?: NitroOptions["preset"]; + server?: boolean; + versions?: { + nitro?: string; + }; + } + } } diff --git a/test/fixture/routes/env-flags.ts b/test/fixture/routes/env-flags.ts new file mode 100644 index 0000000000..f041497a8b --- /dev/null +++ b/test/fixture/routes/env-flags.ts @@ -0,0 +1,12 @@ +// 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], + }; +}); diff --git a/test/tests.ts b/test/tests.ts index 591f613692..ef5210b7f9 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -371,4 +371,39 @@ export function testNitro( expect(headers["server-timing"]).toMatch(/-;dur=\d+;desc="Generate"/); }); } + + it("env-flags", async () => { + const { data } = await callHandler({ url: "/env-flags" }); + expect(data).toMatchInlineSnapshot(` + { + "client": [ + false, + false, + ], + "dev": [ + true, + true, + ], + "nitro": [ + true, + true, + ], + "prerender": [ + false, + false, + ], + "preset": [ + "nitro-dev", + "nitro-dev", + ], + "server": [ + true, + true, + ], + "versions.nitro": [ + "2.4.0", + ], + } + `); + }); } From 53471f1cee4f06b21d8df6ea717ff95ccbba4421 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:07:02 +0200 Subject: [PATCH 2/8] rename --- src/rollup/config.ts | 6 ++-- .../routes/{env-flags.ts => static-flags.ts} | 2 +- test/tests.ts | 36 +++++++++++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) rename test/fixture/routes/{env-flags.ts => static-flags.ts} (84%) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index 6a799cb79d..3d1088b6d6 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -174,7 +174,7 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { DEBUG: nitro.options.dev, }; - const envFlags = { + const staticFlags = { dev: nitro.options.dev, preset: nitro.options.preset, prerender: nitro.options.preset === "nitro-prerender", @@ -221,13 +221,13 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { ]) ), ...Object.fromEntries( - Object.entries(envFlags).map(([key, val]) => [ + Object.entries(staticFlags).map(([key, val]) => [ `process.${key}`, JSON.stringify(val), ]) ), ...Object.fromEntries( - Object.entries(envFlags).map(([key, val]) => [ + Object.entries(staticFlags).map(([key, val]) => [ `import.meta.${key}`, JSON.stringify(val), ]) diff --git a/test/fixture/routes/env-flags.ts b/test/fixture/routes/static-flags.ts similarity index 84% rename from test/fixture/routes/env-flags.ts rename to test/fixture/routes/static-flags.ts index f041497a8b..65387d623e 100644 --- a/test/fixture/routes/env-flags.ts +++ b/test/fixture/routes/static-flags.ts @@ -7,6 +7,6 @@ export default eventHandler(async (event) => { server: [process.server, import.meta.server], client: [process.client, import.meta.client], nitro: [process.nitro, import.meta.nitro], - "versions.nitro": [process.versions.nitro], + "versions.nitro": [process.versions.nitro, import.meta.versions.nitro], }; }); diff --git a/test/tests.ts b/test/tests.ts index ef5210b7f9..2903f18d7f 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -372,8 +372,8 @@ export function testNitro( }); } - it("env-flags", async () => { - const { data } = await callHandler({ url: "/env-flags" }); + it("static build flags", async () => { + const { data } = await callHandler({ url: "/static-flags" }); expect(data).toMatchInlineSnapshot(` { "client": [ @@ -402,6 +402,38 @@ export function testNitro( ], "versions.nitro": [ "2.4.0", + "2.4.0", + ], + } + `` + { + "client": [ + false, + false, + ], + "dev": [ + false, + false, + ], + "nitro": [ + true, + true, + ], + "prerender": [ + false, + false, + ], + "preset": [ + "aws-lambda", + "aws-lambda", + ], + "server": [ + true, + true, + ], + "versions.nitro": [ + "2.4.0", + "2.4.0", ], } `); From 2bb24f6f7d6844933a7e3a28cbfb26d93297f180 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:12:50 +0200 Subject: [PATCH 3/8] split global types --- src/types/global.ts | 24 ++++++++++++++++++++++++ src/types/index.ts | 1 + src/types/nitro.ts | 15 --------------- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 src/types/global.ts diff --git a/src/types/global.ts b/src/types/global.ts new file mode 100644 index 0000000000..d4f69f64e5 --- /dev/null +++ b/src/types/global.ts @@ -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 {}; diff --git a/src/types/index.ts b/src/types/index.ts index b0fedf12fb..4e8cf44199 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,5 @@ export * from "./fetch"; +export * from "./global"; export * from "./serialize"; export * from "./nitro"; export * from "./handler"; diff --git a/src/types/nitro.ts b/src/types/nitro.ts index 46dea5522a..f370755a8c 100644 --- a/src/types/nitro.ts +++ b/src/types/nitro.ts @@ -278,19 +278,4 @@ export interface NitroOptions extends PresetOptions { declare global { const defineNitroConfig: (config: NitroConfig) => NitroConfig; - - // eslint-disable-next-line @typescript-eslint/no-namespace - namespace NodeJS { - interface Global { - dev?: boolean; - client?: boolean; - nitro?: boolean; - prerender?: boolean; - preset?: NitroOptions["preset"]; - server?: boolean; - versions?: { - nitro?: string; - }; - } - } } From cdc6f3e53d0416119e0bb417c65e64d5b9ab028d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:14:22 +0200 Subject: [PATCH 4/8] support optional replacement too --- src/rollup/config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index 3d1088b6d6..dc8f5eefdd 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -182,6 +182,7 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { client: false, nitro: true, "versions.nitro": version, + "versions?.nitro": version, }; // Universal import.meta From 9968ca1086815a0821edb9e938468d6a2be2ebce Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:17:41 +0200 Subject: [PATCH 5/8] update test --- test/fixture/routes/static-flags.ts | 1 + test/tests.ts | 73 ++++------------------------- 2 files changed, 11 insertions(+), 63 deletions(-) diff --git a/test/fixture/routes/static-flags.ts b/test/fixture/routes/static-flags.ts index 65387d623e..3a502d450f 100644 --- a/test/fixture/routes/static-flags.ts +++ b/test/fixture/routes/static-flags.ts @@ -8,5 +8,6 @@ export default eventHandler(async (event) => { 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], }; }); diff --git a/test/tests.ts b/test/tests.ts index 2903f18d7f..5941f06fe0 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -374,68 +374,15 @@ export function testNitro( it("static build flags", async () => { const { data } = await callHandler({ url: "/static-flags" }); - expect(data).toMatchInlineSnapshot(` - { - "client": [ - false, - false, - ], - "dev": [ - true, - true, - ], - "nitro": [ - true, - true, - ], - "prerender": [ - false, - false, - ], - "preset": [ - "nitro-dev", - "nitro-dev", - ], - "server": [ - true, - true, - ], - "versions.nitro": [ - "2.4.0", - "2.4.0", - ], - } - `` - { - "client": [ - false, - false, - ], - "dev": [ - false, - false, - ], - "nitro": [ - true, - true, - ], - "prerender": [ - false, - false, - ], - "preset": [ - "aws-lambda", - "aws-lambda", - ], - "server": [ - true, - true, - ], - "versions.nitro": [ - "2.4.0", - "2.4.0", - ], - } - `); + expect(data).toMatchObject({ + client: [false, false], + dev: [false, false], + nitro: [true, true], + preset: [expect.any(String), expect.any(String)], + prerender: [expect.any(Boolean), expect.any(Boolean)], + server: [true, true], + "versions.nitro": [expect.any(String), expect.any(String)], + "versions?.nitro": [expect.any(String), expect.any(String)], + }); }); } From 3ba81725bd8d342d651852290c2ddf8c95a3d7c1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:22:48 +0200 Subject: [PATCH 6/8] add type --- src/rollup/config.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index dc8f5eefdd..01a65a3685 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -18,7 +18,7 @@ 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"; @@ -174,13 +174,14 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { DEBUG: nitro.options.dev, }; - const staticFlags = { + 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, }; From 2b271cb32f0c56c42a06719ab39ffff747ff0f8a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:23:44 +0200 Subject: [PATCH 7/8] update test --- test/tests.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/tests.ts b/test/tests.ts index 5941f06fe0..f03c0bcd40 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -375,11 +375,11 @@ export function testNitro( 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-dev", ctx.preset === "nitro-dev"], client: [false, false], - dev: [false, false], nitro: [true, true], - preset: [expect.any(String), expect.any(String)], - prerender: [expect.any(Boolean), expect.any(Boolean)], server: [true, true], "versions.nitro": [expect.any(String), expect.any(String)], "versions?.nitro": [expect.any(String), expect.any(String)], From 193c3850b60b9d911c4d5cd3bd455ddb86471e71 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Jun 2023 17:27:49 +0200 Subject: [PATCH 8/8] update test --- test/tests.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/tests.ts b/test/tests.ts index f03c0bcd40..db7f382a2b 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -377,7 +377,10 @@ export function testNitro( expect(data).toMatchObject({ dev: [ctx.isDev, ctx.isDev], preset: [ctx.preset, ctx.preset], - prerender: [ctx.preset === "nitro-dev", ctx.preset === "nitro-dev"], + prerender: [ + ctx.preset === "nitro-prerenderer", + ctx.preset === "nitro-prerenderer", + ], client: [false, false], nitro: [true, true], server: [true, true],