@@ -99,22 +99,43 @@ export type ORPCErrorOptions<TData>
9999 & { defined ?: boolean , status ?: number , message ?: string }
100100 & ( undefined extends TData ? { data ?: TData } : { data : TData } )
101101
102- /**
103- * Store all ORPCError constructors
104- * for workaround of instanceof check in case multiple dependency graphs exist
105- *
106- * @info `Symbol.for` is global symbol registry and shared across different dependency graphs
107- */
108- const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol . for ( `__${ ORPC_CLIENT_PACKAGE_NAME } @${ ORPC_CLIENT_PACKAGE_VERSION } /error/ORPC_ERROR_CONSTRUCTORS__` )
109- void ( ( globalThis as any ) [ GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL ] ??= new WeakSet ( ) )
110- const globalORPCErrorConstructors : WeakSet < object > = ( globalThis as any ) [ GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL ]
102+ let globalORPCErrorConstructors : WeakSet < object >
111103
112104export class ORPCError < TCode extends ORPCErrorCode , TData > extends Error {
113105 readonly defined : boolean
114106 readonly code : TCode
115107 readonly status : number
116108 readonly data : TData
117109
110+ /**
111+ * Placed inside a static block (rather than at module level) to ensure this
112+ * registration is treated as part of the class definition by bundlers.
113+ *
114+ * With `"sideEffects": false` in package.json, bundlers like webpack/Rollup
115+ * are allowed to tree-shake any module-level statements that appear to have
116+ * no consumers. A free-floating `globalORPCErrorConstructors.add(ORPCError)`
117+ * at module level could be dropped entirely if the bundler decides the module
118+ * is only partially used.
119+ *
120+ * By placing this inside `static {}`, the registration becomes inseparable
121+ * from the class body itself — a bundler cannot include `ORPCError` without
122+ * executing this block.
123+ *
124+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks
125+ */
126+ static {
127+ /**
128+ * Store all ORPCError constructors
129+ * for workaround of instanceof check in case multiple dependency graphs exist
130+ *
131+ * @info `Symbol.for` is global symbol registry and shared across different dependency graphs
132+ */
133+ const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol . for ( `__${ ORPC_CLIENT_PACKAGE_NAME } @${ ORPC_CLIENT_PACKAGE_VERSION } /error/ORPC_ERROR_CONSTRUCTORS__` )
134+ void ( ( globalThis as any ) [ GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL ] ??= new WeakSet ( ) )
135+ globalORPCErrorConstructors = ( globalThis as any ) [ GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL ]
136+ globalORPCErrorConstructors . add ( ORPCError )
137+ }
138+
118139 constructor ( code : TCode , ...rest : MaybeOptionalOptions < ORPCErrorOptions < TData > > ) {
119140 const options = resolveMaybeOptionalOptions ( rest )
120141
@@ -167,11 +188,6 @@ export class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
167188 return super [ Symbol . hasInstance ] ( instance )
168189 }
169190}
170- /**
171- * Store ORPCError constructor
172- * for workaround of instanceof check in case multiple dependency graphs exist
173- */
174- globalORPCErrorConstructors . add ( ORPCError )
175191
176192export type ORPCErrorJSON < TCode extends string , TData > = Pick < ORPCError < TCode , TData > , 'defined' | 'code' | 'status' | 'message' | 'data' >
177193
0 commit comments