feat: support inlining FRESH_PUBLIC_ env vars (#3214)
Fixes https://github.com/denoland/fresh/issues/3213
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"./internal-dev": "./src/internals_dev.ts"
|
||||
},
|
||||
"imports": {
|
||||
"@std/fmt": "jsr:@std/fmt@^1.0.8"
|
||||
"@std/fmt": "jsr:@std/fmt@^1.0.8",
|
||||
"@types/node": "npm:@types/node@^24.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,11 @@ export async function bundleJs(
|
||||
preactDebugger(PREACT_ENV),
|
||||
buildIdPlugin(options.buildId),
|
||||
windowsPathFixer(),
|
||||
denoPlugin({ preserveJsx: true, debug: false }),
|
||||
denoPlugin({
|
||||
preserveJsx: true,
|
||||
debug: false,
|
||||
publicEnvVarPrefix: "FRESH_PUBLIC_",
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// deno-lint-ignore-file no-process-global
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { useEffect } from "preact/hooks";
|
||||
|
||||
export interface EnvIslandProps {
|
||||
id?: string;
|
||||
}
|
||||
|
||||
export function EnvIsland(props: EnvIslandProps) {
|
||||
const active = useSignal(false);
|
||||
useEffect(() => {
|
||||
active.value = true;
|
||||
}, []);
|
||||
|
||||
const deno = Deno.env.get("FRESH_PUBLIC_TEST_FOO");
|
||||
const deno2 = Deno.env.get("FRESH_PUBLIC_TEST_FOO");
|
||||
// @ts-ignore f
|
||||
const processEnv = process.env.FRESH_PUBLIC_TEST_FOO;
|
||||
|
||||
let denoPrivateEnv = "not ok";
|
||||
try {
|
||||
Deno.env.get("FRESH_PRIVATE_TEST_FOO");
|
||||
} catch {
|
||||
denoPrivateEnv = "ok";
|
||||
}
|
||||
|
||||
let denoPrivateEnv2 = "not ok";
|
||||
try {
|
||||
Deno.env.get("FRESH_PRIVATE_TEST_FOO");
|
||||
} catch {
|
||||
denoPrivateEnv2 = "ok";
|
||||
}
|
||||
|
||||
let processEnvPrivate = "not ok";
|
||||
try {
|
||||
process.env.FRESH_PRIVATE_TEST_FOO;
|
||||
} catch {
|
||||
processEnvPrivate = "ok";
|
||||
}
|
||||
|
||||
return (
|
||||
<div id={props.id} class={active.value ? "ready" : ""}>
|
||||
<pre class="deno-env">{deno}</pre>
|
||||
<pre class="deno-env-2">{deno2}</pre>
|
||||
<pre class="process-env">{processEnv}</pre>
|
||||
|
||||
<pre class="deno-env-private">{denoPrivateEnv}</pre>
|
||||
<pre class="deno-env-private-2">{denoPrivateEnv2}</pre>
|
||||
<pre class="process-env-private">{processEnvPrivate}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -28,7 +28,10 @@ import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts";
|
||||
import { ComputedSignal } from "./fixtures_islands/Computed.tsx";
|
||||
import { EnvIsland } from "./fixtures_islands/EnvIsland.tsx";
|
||||
|
||||
Deno.env.set("FRESH_PUBLIC_TEST_FOO", "test-env-value");
|
||||
Deno.env.set("FRESH_PRIVATE_TEST_FOO", "i-should-not-be-visible");
|
||||
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
|
||||
const islandGroupCache = await buildProd({ root: ISLAND_GROUP_DIR });
|
||||
|
||||
@@ -689,6 +692,52 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "islands - inlines FRESH_PUBLIC_* env vars",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
<EnvIsland id="test" />
|
||||
</Doc>,
|
||||
));
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
const denoEnv = await page.locator<HTMLDivElement>(".deno-env")
|
||||
.evaluate((el) => el.textContent);
|
||||
const denoEnv2 = await page.locator<HTMLDivElement>(".deno-env-2")
|
||||
.evaluate((el) => el.textContent);
|
||||
const processEnv = await page.locator<HTMLDivElement>(".process-env")
|
||||
.evaluate((el) => el.textContent);
|
||||
|
||||
const denoEnvPrivate = await page.locator<HTMLDivElement>(
|
||||
".deno-env-private",
|
||||
)
|
||||
.evaluate((el) => el.textContent);
|
||||
const denoEnvPrivate2 = await page.locator<HTMLDivElement>(
|
||||
".deno-env-private-2",
|
||||
)
|
||||
.evaluate((el) => el.textContent);
|
||||
const processEnvPrivate = await page.locator<HTMLDivElement>(
|
||||
".process-env-private",
|
||||
)
|
||||
.evaluate((el) => el.textContent);
|
||||
|
||||
expect(denoEnv).toEqual("test-env-value");
|
||||
expect(denoEnv2).toEqual("test-env-value");
|
||||
expect(processEnv).toEqual("test-env-value");
|
||||
|
||||
expect(denoEnvPrivate).toEqual("ok");
|
||||
expect(denoEnvPrivate2).toEqual("ok");
|
||||
expect(processEnvPrivate).toEqual("ok");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "fsRoutes - load islands from group folder",
|
||||
fn: async () => {
|
||||
|
||||
Reference in New Issue
Block a user