Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if process exists before accessing it #1784

Merged
merged 4 commits into from
Oct 22, 2024
Merged

Check if process exists before accessing it #1784

merged 4 commits into from
Oct 22, 2024

Conversation

handeyeco
Copy link
Contributor

@handeyeco handeyeco commented Oct 21, 2024

Summary:

process is a NodeJS thing, so it's not guaranteed to be on the FE. This checks that it exists before accessing it.

@handeyeco handeyeco self-assigned this Oct 21, 2024
@khan-actions-bot khan-actions-bot requested a review from a team October 21, 2024 22:08
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Oct 21, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/polite-pigs-look.md, packages/math-input/src/components/i18n-context.tsx, packages/perseus/src/components/i18n-context.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@handeyeco handeyeco changed the title check if process exists before accessing it Check if process exists before accessing it Oct 21, 2024
Copy link
Contributor

github-actions bot commented Oct 21, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (84fd157) and published it to npm. You
can install it using the tag PR1784.

Example:

yarn add @khanacademy/perseus@PR1784

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1784

Copy link
Contributor

github-actions bot commented Oct 21, 2024

Size Change: +3.38 kB (+0.39%)

Total Size: 870 kB

Filename Size Change
packages/math-input/dist/es/index.js 78.5 kB +760 B (+0.98%)
packages/perseus/dist/es/index.js 422 kB +2.62 kB (+0.62%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.8 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-editor/dist/es/index.js 281 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/strings.js 3.4 kB
packages/pure-markdown/dist/es/index.js 3.66 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

@@ -15,10 +15,14 @@ type I18nContextType = {
locale: string;
};

const shouldMockStrings: boolean = Boolean(
process?.env?.NODE_ENV === "test" || process?.env?.STORYBOOK,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will do what you want. If the process global variable doesn't exist, accessing it directly will fail. process?.env just returns undefined if process is nullish, but in order for that to work the variable process has to exist.

I think this will work:

typeof process !== "undefined" && (process?.env?.NODE_ENV === "test" || ... )

However, storing the result of a process.env check in a variable like this will prevent most build tools from doing dead code elimination. Vite, webpack, etc. will optimize an expression like:

process.env.NODE_ENV === "test" ? foo : bar

to foo in test environments and bar elsewhere. The env check won't actually appear in the built code. This behavior might be why this code currently works in browsers.

If the env check is abstracted at all, dead code elimination generally won't happen. I think that doesn't matter in this case, because the amount of code that would be eliminated here is tiny and not side-effecting, but I think as a general practice we'd want to keep env checks inline.

Copy link
Contributor Author

@handeyeco handeyeco Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it didn't feel right (and was breaking the SB build). Thanks for the feedback, fixed.

@handeyeco handeyeco merged commit a8f0165 into main Oct 22, 2024
8 of 9 checks passed
@handeyeco handeyeco deleted the process-check branch October 22, 2024 21:42
handeyeco added a commit that referenced this pull request Oct 23, 2024
handeyeco added a commit that referenced this pull request Oct 23, 2024
## Summary:

Reverts #1784 because it broke (static) Storybook

Author: handeyeco

Reviewers: jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, 🚫 Publish npm snapshot (ubuntu-latest, 20.x), 🚫 Check for .changeset entries for all changed files (ubuntu-latest, 20.x), 🚫 Cypress (ubuntu-latest, 20.x), 🚫 Check builds for changes in size (ubuntu-latest, 20.x), 🚫 Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ gerald, 🚫 Publish npm snapshot (ubuntu-latest, 20.x), 🚫 Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), 🚫 Check builds for changes in size (ubuntu-latest, 20.x), 🚫 Cypress (ubuntu-latest, 20.x), 🚫 Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, 🚫 Publish npm snapshot (ubuntu-latest, 20.x), 🚫 Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), 🚫 Cypress (ubuntu-latest, 20.x), 🚫 Check for .changeset entries for all changed files (ubuntu-latest, 20.x), 🚫 Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1790
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants