Skip to content

Commit

Permalink
fix(lib/env,ssr): determine env paths properly (#8219)
Browse files Browse the repository at this point in the history
- fix(lib/env): load env from `cwd()`, but build from root
- fix(ssr): get env path by determining (current) dirname first
- chore(lib/env): unexports ROOT, as it is not used anywhere else.

Also adds some paths to the `.npmignore` file.
  • Loading branch information
caugner authored Feb 17, 2023
1 parent 79fb3fc commit 676b606
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ server/node_modules/
# This won't be relevant or needed once the content is gone.
/content/files/en-us/

/client/venv/
/deployer
kumascript/tests
testing/
Expand Down Expand Up @@ -43,3 +44,6 @@ jest.config.js
*.test.js
.storybook/
.nvmrc

# Don't pack the build.
mdn-yari-*.tgz
1 change: 0 additions & 1 deletion libs/env/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const ROOT: string;
export const BUILD_OUT_ROOT: string;
export const DEFAULT_FLAW_LEVELS: string;
export const FILES: string;
Expand Down
9 changes: 4 additions & 5 deletions libs/env/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import fs from "node:fs";
import path from "node:path";
import { cwd } from "node:process";
import { fileURLToPath } from "node:url";

import dotenv from "dotenv";

import { VALID_FLAW_CHECKS } from "../constants/index.js";

// Spread into two lines to get the ROOT path,
// to prevent Webpack from treating 'new URL("../..", import.meta.url)' as an import.
const currentDir = path.dirname(fileURLToPath(import.meta.url));
export const ROOT = path.join(currentDir, "..", "..");
const dirname = fileURLToPath(new URL(".", import.meta.url));
const ROOT = path.join(dirname, "..", "..");

dotenv.config({
path: path.join(ROOT, process.env.ENV_FILE || ".env"),
path: path.join(cwd(), process.env.ENV_FILE || ".env"),
});

// -----
Expand Down
9 changes: 4 additions & 5 deletions ssr/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import { fileURLToPath } from "node:url";

import dotenv from "dotenv";
Expand All @@ -7,12 +8,10 @@ import { StaticRouter } from "react-router-dom/server";
import { App } from "../client/src/app";
import render from "./render";

// This is necessary because the ssr.js is in dist/ssr.js
// and we need to reach the .env this way.
const dirname = fileURLToPath(new URL(".", import.meta.url));

dotenv.config({
path: fileURLToPath(
new URL("../" + (process.env.ENV_FILE || ".env"), import.meta.url)
),
path: path.join(dirname, "..", process.env.ENV_FILE || ".env"),
});

export function renderHTML(url, context) {
Expand Down

0 comments on commit 676b606

Please sign in to comment.