Skip to content

Commit b0d8649

Browse files
authored
refactor: simplify isProduction check and remove unused assertions (#552)
Fixes #544 (partially) - will limit it to: ``` function assertTruthy(condition, message) { return; } ``` In the production bundle. Removing it entirely is too intrusive right now as we would need to inline every `assertTruthy` check.
2 parents a82afca + f883ea0 commit b0d8649

File tree

7 files changed

+18
-24
lines changed

7 files changed

+18
-24
lines changed

examples/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ export default defineConfig(({ command, mode, isSsrBuild }) => {
8181
'Cross-Origin-Embedder-Policy': 'require-corp',
8282
},
8383
},
84+
define: {
85+
__DEV__: true,
86+
},
8487
};
8588
});

src/core/CoreNode.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import {
2121
assertTruthy,
2222
getNewId,
23-
isProductionEnvironment,
2423
mergeColorAlphaPremultiplied,
24+
isProductionEnvironment,
2525
} from '../utils.js';
2626
import type { TextureOptions } from './CoreTextureManager.js';
2727
import type { CoreRenderer } from './renderers/CoreRenderer.js';
@@ -1369,8 +1369,6 @@ export class CoreNode extends EventEmitter {
13691369
}
13701370

13711371
createRenderBounds(): void {
1372-
assertTruthy(this.stage);
1373-
13741372
if (this.parent !== null && this.parent.strictBound !== undefined) {
13751373
// we have a parent with a valid bound, copy it
13761374
const parentBound = this.parent.strictBound;
@@ -2176,10 +2174,6 @@ export class CoreNode extends EventEmitter {
21762174
this.props.parent = newParent;
21772175
if (oldParent) {
21782176
const index = oldParent.children.indexOf(this);
2179-
assertTruthy(
2180-
index !== -1,
2181-
"CoreNode.parent: Node not found in old parent's children!",
2182-
);
21832177
oldParent.children.splice(index, 1);
21842178
oldParent.setUpdateType(
21852179
UpdateType.Children | UpdateType.ZIndexSortedChildren,

src/core/CoreTextNode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import type {
3434
} from '../common/CommonTypes.js';
3535
import type { RectWithValid } from './lib/utils.js';
3636
import { assertTruthy } from '../utils.js';
37-
import { Matrix3d } from './lib/Matrix3d.js';
3837

3938
export interface CoreTextNodeProps extends CoreNodeProps, TrProps {
4039
/**

src/core/TextureMemoryManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export class TextureMemoryManager {
352352
criticalThreshold: this.criticalThreshold,
353353
});
354354

355-
if (this.debugLogging === true || isProductionEnvironment() === false) {
355+
if (this.debugLogging === true || isProductionEnvironment === false) {
356356
console.warn(
357357
`[TextureMemoryManager] Memory usage above critical threshold after cleanup: ${this.memUsed}`,
358358
);

src/main-api/Inspector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class Inspector {
172172
private scaleY = 1;
173173

174174
constructor(canvas: HTMLCanvasElement, settings: RendererMainSettings) {
175-
if (isProductionEnvironment()) return;
175+
if (isProductionEnvironment === true) return;
176176

177177
if (!settings) {
178178
throw new Error('settings is required');

src/main-api/Renderer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import type { ExtractProps, TextureMap } from '../core/CoreTextureManager.js';
2121
import { EventEmitter } from '../common/EventEmitter.js';
22-
import { assertTruthy, isProductionEnvironment } from '../utils.js';
22+
import { isProductionEnvironment } from '../utils.js';
2323
import { Stage } from '../core/Stage.js';
2424
import { CoreNode, type CoreNodeProps } from '../core/CoreNode.js';
2525
import { type CoreTextNodeProps } from '../core/CoreTextNode.js';
@@ -466,7 +466,7 @@ export class RendererMain extends EventEmitter {
466466
targetEl.appendChild(canvas);
467467

468468
// Initialize inspector (if enabled)
469-
if (inspector && !isProductionEnvironment()) {
469+
if (inspector && isProductionEnvironment === false) {
470470
this.inspector = new inspector(canvas, resolvedSettings);
471471
}
472472
}
@@ -489,8 +489,6 @@ export class RendererMain extends EventEmitter {
489489
createNode<ShNode extends CoreShaderNode<any>>(
490490
props: Partial<INodeProps<ShNode>>,
491491
): INode<ShNode> {
492-
assertTruthy(this.stage, 'Stage is not initialized');
493-
494492
const node = this.stage.createNode(props as Partial<CoreNodeProps>);
495493

496494
if (this.inspector) {

src/utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ export function createWebGLContext(
6464
return gl;
6565
}
6666

67+
/**
68+
* Checks if we're in a development environment or not.
69+
*
70+
* @returns
71+
*/
72+
declare const __DEV__: boolean;
73+
export const isProductionEnvironment =
74+
typeof __DEV__ !== 'undefined' ? !__DEV__ : true;
75+
6776
/**
6877
* Asserts a condition is truthy, otherwise throws an error
6978
*
@@ -80,7 +89,7 @@ export function assertTruthy(
8089
condition: unknown,
8190
message?: string,
8291
): asserts condition {
83-
if (isProductionEnvironment() === true) return;
92+
if (isProductionEnvironment === true) return;
8493
if (!condition) {
8594
throw new Error(message || 'Assertion failed');
8695
}
@@ -230,15 +239,6 @@ export function getImageAspectRatio(width: number, height: number): number {
230239
return width / height;
231240
}
232241

233-
/**
234-
* Checks import.meta if env is production
235-
*
236-
* @returns
237-
*/
238-
export function isProductionEnvironment(): boolean {
239-
return import.meta.env && import.meta.env.PROD;
240-
}
241-
242242
/**
243243
* Returns a new unique ID
244244
*/

0 commit comments

Comments
 (0)