Skip to content

Commit 9dae0be

Browse files
authored
Fix NODE_ENV var in browser (#189)
* feat: implement environment detection for NODE_ENV - Added a new utility module for browser-safe environment detection, exporting the current NODE_ENV value. - Updated the Application and validation utilities to use the new env module for environment checks instead of directly accessing process.env. * refactor: update environment detection comment for clarity - Changed comment in env.ts to better reflect the purpose of the exported variable, clarifying that it represents a browser-safe environment variable. * changeset
1 parent 083e646 commit 9dae0be

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

.changeset/new-teams-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@playcanvas/react": patch
3+
---
4+
5+
Ensures the NODE_ENV is compatible in browser only environments. Defaulting to production

packages/lib/src/Application.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { validatePropsWithDefaults, createComponentDefinition, Schema, getNullAp
2121
import { PublicProps } from './utils/types-utils.ts';
2222
import { GraphicsDeviceOptions, defaultGraphicsDeviceOptions } from './types/graphics-device-options.ts';
2323
import { DeviceType, internalCreateGraphicsDevice } from './utils/create-graphics-device.ts';
24+
import { env } from './utils/env.ts';
2425

2526
/**
2627
* The **Application** component is the root node of the PlayCanvas React API. It creates a canvas element
@@ -281,7 +282,7 @@ componentDefinition.schema = {
281282
* In test environments, we default to a Null device, because we don't cant use WebGL2/WebGPU.
282283
* This is just for testing purposes so we can test the fallback logic, without initializing WebGL2/WebGPU.
283284
*/
284-
default: process.env.NODE_ENV === 'test' ? [DEVICETYPE_NULL] : [DEVICETYPE_WEBGL2]
285+
default: env === 'test' ? [DEVICETYPE_NULL] : [DEVICETYPE_WEBGL2]
285286
},
286287
className: {
287288
validate: (value: unknown) => typeof value === 'string',

packages/lib/src/utils/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Browser safe environment var.
3+
*/
4+
export const env = typeof process !== 'undefined' && process?.env?.NODE_ENV
5+
? process.env.NODE_ENV
6+
: 'production';

packages/lib/src/utils/validation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Color, Quat, Vec2, Vec3, Vec4, Mat4, Application, NullGraphicsDevice, Material } from "playcanvas";
22
import { getColorFromName } from "./color.ts";
33
import { Serializable } from "./types-utils.ts";
4+
import { env } from "./env.ts";
45

56
// Limit the size of the warned set to prevent memory leaks
67
const MAX_WARNED_SIZE = 1000;
78
const warned = new Set<string>();
89

910
export const warnOnce = (message: string) => {
1011
if (!warned.has(message)) {
11-
if (process.env.NODE_ENV !== 'production') {
12+
if (env !== 'production') {
1213

1314
// Use setTimeout to break the call stack
1415
setTimeout(() => {
@@ -73,7 +74,7 @@ export function validateAndSanitize<T, InstanceType>(
7374
): T {
7475
const isValid = value !== undefined && propDef.validate(value);
7576

76-
if (!isValid && value !== undefined && process.env.NODE_ENV !== 'production') {
77+
if (!isValid && value !== undefined && env !== 'production') {
7778
warnOnce(
7879
`Invalid prop "${propName}" in \`<${componentName} ${propName}={${JSON.stringify(value)}} />\`\n` +
7980
` ${propDef.errorMsg(value)}\n` +
@@ -131,7 +132,7 @@ export function validatePropsPartial<T, InstanceType>(
131132
});
132133

133134
// Warn about unknown props in development mode
134-
if (process.env.NODE_ENV !== 'production' && warnUnknownProps && unknownProps.length > 0) {
135+
if (env !== 'production' && warnUnknownProps && unknownProps.length > 0) {
135136
warnOnce(
136137
`Unknown props in "<${name}/>."\n` +
137138
`The following props are invalid and will be ignored: "${unknownProps.join('", "')}"\n\n` +
@@ -183,7 +184,7 @@ export function validatePropsWithDefaults<T extends object, InstanceType>(
183184

184185
// Optionally warn about unknown props
185186
if (
186-
process.env.NODE_ENV !== 'production' &&
187+
env !== 'production' &&
187188
warnUnknownProps &&
188189
rawProps
189190
) {

0 commit comments

Comments
 (0)