@@ -6,47 +6,116 @@ This project supports multiple runtime platforms (Browser, Node.js, React Native
66
77## Naming Convention
88
9- Platform-specific files use a suffix pattern:
9+ Platform-specific files can be identified in two ways:
10+
11+ ### 1. File Naming Convention (Single Platform)
12+
13+ For files specific to a single platform, use a suffix pattern:
1014- ` .browser.ts ` - Browser-specific implementation
1115- ` .node.ts ` - Node.js-specific implementation
1216- ` .react_native.ts ` - React Native-specific implementation
1317- ` .ts ` (no suffix) - Universal code (works across all platforms)
1418
19+ ### 2. Export Declaration (Multiple Platforms)
20+
21+ For files that support multiple platforms but not all (e.g., Browser + React Native, but not Node.js), export a ` __supportedPlatforms ` array:
22+
23+ ``` typescript
24+ // lib/utils/web-features.ts
25+ export const __supportedPlatforms = [' browser' , ' react_native' ];
26+
27+ // Your code that works on both browser and react_native
28+ export function getWindowSize() {
29+ // Implementation that works on both platforms
30+ }
31+ ```
32+
33+ Valid platform identifiers: ` 'browser' ` , ` 'node' ` , ` 'react_native' `
34+
35+ ### Priority
36+
37+ If a file has both a platform suffix in its name AND a ` __supportedPlatforms ` export, the ` __supportedPlatforms ` export ** takes priority** . This allows you to keep the ` .browser.ts ` naming convention while expanding support to additional platforms like React Native.
38+
1539## Import Rules
1640
1741Each platform-specific file can ** only** import from:
1842
19- 1 . ** Universal files** (no platform suffix )
20- 2 . ** Same- platform files** (matching platform suffix )
43+ 1 . ** Universal files** (no platform restrictions )
44+ 2 . ** Compatible platform files** (files that support ALL the required platforms )
21453 . ** External packages** (node_modules)
2246
47+ A file is compatible if:
48+ - It's universal (no platform restrictions)
49+ - For single-platform files: The import supports at least that platform
50+ - For multi-platform files: The import supports ALL of those platforms
51+
52+ ### Compatibility Examples
53+
54+ ** Single Platform File (` .browser.ts ` or ` __supportedPlatforms = ['browser'] ` )**
55+ - ✅ Can import from: universal files, ` .browser.ts ` files, files with ` ['browser'] ` or ` ['browser', 'react_native'] `
56+ - ❌ Cannot import from: ` .node.ts ` files, files with ` ['node'] ` or ` ['react_native'] ` only
57+
58+ ** Multi-Platform File (` __supportedPlatforms = ['browser', 'react_native'] ` )**
59+ - ✅ Can import from: universal files, files with exactly ` ['browser', 'react_native'] `
60+ - ❌ Cannot import from: ` .browser.ts ` (browser only), ` .react_native.ts ` (react_native only), ` .node.ts `
61+ - ** Why?** A file supporting both platforms needs imports that work in BOTH environments
62+
2363### Examples
2464
2565✅ ** Valid Imports**
2666
2767``` typescript
28- // In lib/index.browser.ts
68+ // In lib/index.browser.ts (Browser platform only)
2969import { Config } from ' ./shared_types' ; // ✅ Universal file
30- import { BrowserRequestHandler } from ' ./utils/http_request_handler/request_handler.browser' ; // ✅ Same platform
70+ import { BrowserRequestHandler } from ' ./utils/http_request_handler/request_handler.browser' ; // ✅ browser + react_native (supports browser)
3171import { uuid } from ' uuid' ; // ✅ External package
3272```
3373
3474``` typescript
35- // In lib/index.node.ts
75+ // In lib/index.node.ts (Node platform only)
3676import { Config } from ' ./shared_types' ; // ✅ Universal file
3777import { NodeRequestHandler } from ' ./utils/http_request_handler/request_handler.node' ; // ✅ Same platform
3878```
3979
80+ ``` typescript
81+ // In lib/index.react_native.ts (React Native platform only)
82+ import { Config } from ' ./shared_types' ; // ✅ Universal file
83+
84+ // If web-features.ts has: __supportedPlatforms = ['browser', 'react_native']
85+ import { getWindowSize } from ' ./utils/web-features' ; // ✅ Compatible (supports react_native)
86+ ```
87+
88+ ``` typescript
89+ // In lib/utils/web-api.ts
90+ // export const __supportedPlatforms = ['browser', 'react_native'];
91+
92+ import { Config } from ' ./shared_types' ; // ✅ Universal file
93+
94+ // If dom-helpers.ts has: __supportedPlatforms = ['browser', 'react_native']
95+ import { helpers } from ' ./dom-helpers' ; // ✅ Compatible (supports BOTH browser and react_native)
96+ ```
97+
4098❌ ** Invalid Imports**
4199
42100``` typescript
43- // In lib/index.browser.ts
44- import { NodeRequestHandler } from ' ./utils/http_request_handler/request_handler.node' ; // ❌ Different platform
101+ // In lib/index.browser.ts (Browser platform only)
102+ import { NodeRequestHandler } from ' ./utils/http_request_handler/request_handler.node' ; // ❌ Node-only file
103+ ```
104+
105+ ``` typescript
106+ // In lib/index.node.ts (Node platform only)
107+ // If web-features.ts has: __supportedPlatforms = ['browser', 'react_native']
108+ import { getWindowSize } from ' ./utils/web-features' ; // ❌ Not compatible with Node
45109```
46110
47111``` typescript
48- // In lib/index.node.ts
49- import { BrowserRequestHandler } from ' ./utils/http_request_handler/request_handler.browser' ; // ❌ Different platform
112+ // In lib/utils/web-api.ts
113+ // export const __supportedPlatforms = ['browser', 'react_native'];
114+
115+ // If helper.browser.ts is browser-only (no __supportedPlatforms export)
116+ import { helper } from ' ./helper.browser' ; // ❌ Browser-only, doesn't support react_native
117+
118+ // This file needs imports that work in BOTH browser AND react_native
50119```
51120
52121## Automatic Validation
@@ -95,11 +164,13 @@ If platform isolation is violated, the build will fail with a detailed error mes
95164
96165When creating new platform-specific implementations:
97166
167+ ### Single Platform
168+
981691 . Name the file with the appropriate platform suffix (e.g., ` my-feature.browser.ts ` )
991702 . Only import from universal or same-platform files
1001713 . Create a universal factory or interface if multiple platforms need different implementations
101172
102- ### Example: Creating a Platform-Specific Feature
173+ ** Example:**
103174
104175``` typescript
105176// lib/features/my-feature.ts (universal interface)
@@ -130,6 +201,39 @@ import { NodeMyFeature } from './my-feature.node';
130201export const createMyFeature = () => new NodeMyFeature ();
131202```
132203
204+ ### Multiple Platforms (But Not All)
205+
206+ For code that works on multiple platforms but not all, use the ` __supportedPlatforms ` export:
207+
208+ ** Example: Browser + React Native only**
209+
210+ ``` typescript
211+ // lib/utils/dom-helpers.ts
212+ export const __supportedPlatforms = [' browser' , ' react_native' ];
213+
214+ // This code works on both browser and react_native, but not node
215+ export function getElementById(id : string ): Element | null {
216+ if (typeof document !== ' undefined' ) {
217+ return document .getElementById (id );
218+ }
219+ // React Native polyfill or alternative
220+ return null ;
221+ }
222+ ```
223+
224+ ** Example: Node + React Native only**
225+
226+ ``` typescript
227+ // lib/utils/native-crypto.ts
228+ export const __supportedPlatforms = [' node' , ' react_native' ];
229+
230+ import crypto from ' crypto' ; // Available in both Node and React Native
231+
232+ export function generateHash(data : string ): string {
233+ return crypto .createHash (' sha256' ).update (data ).digest (' hex' );
234+ }
235+ ```
236+
133237## Troubleshooting
134238
135239If you encounter a platform isolation error:
0 commit comments