Skip to content

Commit

Permalink
feat: inject process.env.NODE_ENV for third party libs (#2481)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored May 31, 2024
1 parent 88c3671 commit 9347b84
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/dev/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export async function bundleJs(
write: false,
metafile: true,

define: {
"process.env.NODE_ENV": JSON.stringify(
options.dev ? "development" : "production",
),
},

plugins: [
preactDebugger(PREACT_ENV),
buildIdPlugin(options.buildId),
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures_islands/NodeProcess.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect } from "preact/hooks";
import { useSignal } from "@preact/signals";
import { IS_BROWSER } from "@fresh/core/runtime";

export function NodeProcess() {
const active = useSignal(false);

useEffect(() => {
active.value = true;
}, []);

// @ts-ignore bundling
const value = IS_BROWSER ? process.env.NODE_ENV : "no";

return (
<div class={active.value ? "ready" : ""}>
value: {value}
</div>
);
}
29 changes: 29 additions & 0 deletions tests/islands_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NullIsland } from "./fixtures_islands/NullIsland.tsx";
import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx";
import { JsxIsland } from "./fixtures_islands/JsxIsland.tsx";
import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
import { signal } from "@preact/signals";
import {
allIslandApp,
Expand Down Expand Up @@ -546,6 +547,34 @@ Deno.test({
},
});

Deno.test({
name: "islands - stub Node 'process.env'",
fn: async () => {
const nodeProcess = getIsland("NodeProcess.tsx");

const app = testApp()
.use(staticFiles())
.island(nodeProcess, "NodeProcess", NodeProcess)
.get("/", (ctx) =>
ctx.render(
<Doc>
<NodeProcess />
</Doc>,
));

await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/`, { waitUntil: "load" });
await page.locator(".ready").wait();

// Page would error here
const text = await page
.locator<HTMLDivElement>(".ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("value: production");
});
},
});

Deno.test({
name: "fsRoutes - load islands from group folder",
fn: async () => {
Expand Down
4 changes: 3 additions & 1 deletion tests/test_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { PassThrough } from "./fixtures_islands/PassThrough.tsx";
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx";
import { Foo } from "./fixture_island_groups/routes/foo/(_islands)/Foo.tsx";
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";

export function getIsland(pathname: string) {
return path.join(
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function withBrowserApp(

const browser = await launch({
args: ["--no-sandbox"],
// headless: false,
headless: !Deno.args.includes("--headful"),
});

const page = await browser.newPage();
Expand Down Expand Up @@ -376,6 +377,7 @@ export const allIslandApp = new App()
.island(getIsland("PartialInIsland.tsx"), "PartialInIsland", PartialInIsland)
.island(getIsland("PassThrough.tsx"), "PassThrough", PassThrough)
.island(getIsland("SelfCounter.tsx"), "SelfCounter", SelfCounter)
.island(getIsland("NodeProcess.tsx"), "NodeProcess", NodeProcess)
.island(
getIsland("../fixture_island_groups/routes/foo/(_islands)/Foo.tsx"),
"Foo",
Expand Down

0 comments on commit 9347b84

Please sign in to comment.