Skip to content

Commit a12858d

Browse files
committed
doc updates
1 parent 4bc72f7 commit a12858d

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

docs/PLATFORM_ISOLATION.md

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@
44

55
This project supports multiple runtime platforms (Browser, Node.js, React Native, and Universal), with separate entry points for each. To ensure the build artifacts work correctly, platform-specific code must not be mixed.
66

7-
## Naming Convention
7+
## Platform Declaration
88

9-
Platform-specific files can be identified in two ways:
9+
**Every non-test source file MUST export a `__platforms` array** to declare which platforms it supports. This is enforced by ESLint and validated at build time.
1010

11-
### 1. File Naming Convention (Single Platform)
11+
### Export Declaration (Required)
1212

13-
For files specific to a single platform, use a suffix pattern:
14-
- `.browser.ts` - Browser-specific implementation
15-
- `.node.ts` - Node.js-specific implementation
16-
- `.react_native.ts` - React Native-specific implementation
17-
- `.ts` (no suffix) - Universal code (works across all platforms)
13+
All files must include a `__platforms` export:
1814

19-
### 2. Export Declaration (Multiple Platforms)
15+
**For universal files (all platforms):**
16+
```typescript
17+
export const __platforms = ['__universal__'];
18+
```
19+
20+
**For platform-specific files:**
21+
```typescript
22+
export const __platforms = ['browser']; // Browser only
23+
export const __platforms = ['node']; // Node.js only
24+
export const __platforms = ['react_native']; // React Native only
25+
```
2026

21-
For files that support multiple platforms but not all (e.g., Browser + React Native, but not Node.js), export a `__platforms` array:
27+
**For multi-platform files (but not all):**
2228

2329
```typescript
2430
// lib/utils/web-features.ts
@@ -30,11 +36,17 @@ export function getWindowSize() {
3036
}
3137
```
3238

33-
Valid platform identifiers: `'browser'`, `'node'`, `'react_native'`
39+
Valid platform identifiers: `'browser'`, `'node'`, `'react_native'`, `'__universal__'`
3440

35-
### Priority
41+
### File Naming Convention (Optional)
3642

37-
If a file has both a platform suffix in its name AND a `__platforms` export, the `__platforms` export **takes priority**. This allows you to keep the `.browser.ts` naming convention while expanding support to additional platforms like React Native.
43+
While not enforced, you may optionally use file name suffixes for clarity:
44+
- `.browser.ts` - Typically browser-specific
45+
- `.node.ts` - Typically Node.js-specific
46+
- `.react_native.ts` - Typically React Native-specific
47+
- `.ts` (no suffix) - Typically universal
48+
49+
**Note:** The validator currently enforces only the `__platforms` export declaration. File naming is informational and not validated. The `__platforms` export is the source of truth.
3850

3951
## Import Rules
4052

@@ -51,9 +63,9 @@ A file is compatible if:
5163

5264
### Compatibility Examples
5365

54-
**Single Platform File (`.browser.ts` or `__platforms = ['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
66+
**Single Platform File (`__platforms = ['browser']`)**
67+
- ✅ Can import from: universal files, files with `['browser']` or `['browser', 'react_native']`
68+
- ❌ Cannot import from: files with `['node']` or `['react_native']` only
5769

5870
**Multi-Platform File (`__platforms = ['browser', 'react_native']`)**
5971
- ✅ Can import from: universal files, files with exactly `['browser', 'react_native']`
@@ -134,13 +146,15 @@ npm run build
134146

135147
### How It Works
136148

137-
The validation script (`scripts/validate-platform-isolation.js`):
149+
The validation script (`scripts/validate-platform-isolation-ts.js`):
150+
151+
1. Scans all source files in the `lib/` directory (excluding tests)
152+
2. **Verifies every file has a `__platforms` export** - fails immediately if any file is missing this
153+
3. Parses import statements using TypeScript AST (ES6 imports, require, dynamic imports)
154+
4. Checks that each import follows the platform isolation rules based on declared platforms
155+
5. Fails the build if violations are found or if any file lacks `__platforms` export
138156

139-
1. Scans all source files in the `lib/` directory
140-
2. Identifies platform-specific files by their suffix
141-
3. Parses import statements (ES6 imports, require, dynamic imports)
142-
4. Checks that each import follows the platform isolation rules
143-
5. Fails the build if violations are found
157+
**ESLint Integration:** The `require-platform-declaration` ESLint rule also enforces the `__platforms` export requirement during development.
144158

145159
### Build Integration
146160

@@ -166,9 +180,10 @@ When creating new platform-specific implementations:
166180

167181
### Single Platform
168182

169-
1. Name the file with the appropriate platform suffix (e.g., `my-feature.browser.ts`)
170-
2. Only import from universal or same-platform files
171-
3. Create a universal factory or interface if multiple platforms need different implementations
183+
1. **Add `__platforms` export** declaring the platform (e.g., `export const __platforms = ['browser'];`)
184+
2. Optionally name the file with a platform suffix for clarity (e.g., `my-feature.browser.ts`)
185+
3. Only import from universal or compatible platform files
186+
4. Create a universal factory or interface if multiple platforms need different implementations
172187

173188
**Example:**
174189

@@ -179,13 +194,17 @@ export interface MyFeature {
179194
}
180195

181196
// lib/features/my-feature.browser.ts
197+
export const __platforms = ['browser'];
198+
182199
export class BrowserMyFeature implements MyFeature {
183200
doSomething(): void {
184201
// Browser-specific implementation
185202
}
186203
}
187204

188205
// lib/features/my-feature.node.ts
206+
export const __platforms = ['node'];
207+
189208
export class NodeMyFeature implements MyFeature {
190209
doSomething(): void {
191210
// Node.js-specific implementation

eslint-local-rules/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ This directory contains custom ESLint rules specific to this project.
66

77
### `require-platform-declaration`
88

9-
**Purpose:** Ensures all source files (except tests) export `__platforms` to declare which platforms they support.
9+
**Purpose:** **Enforces that every non-test source file exports `__platforms`** to declare which platforms it supports.
1010

11-
**Why:** This enforces platform isolation at the linting level, catching missing declarations before build time.
11+
**Why:** This is a mandatory requirement for platform isolation. The rule catches missing declarations at lint time, before build or runtime.
12+
13+
**Requirement:** Every `.ts`/`.js` file in `lib/` (except tests) MUST export `__platforms` array with valid platform values.
1214

1315
**Enforcement:**
1416
- ✅ Enabled for all `.ts` files in `lib/` directory

scripts/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ npm run build
2323

2424
The script:
2525
1. Scans all TypeScript/JavaScript files in the `lib/` directory
26-
2. Identifies platform-specific files by:
27-
- Naming convention (`.browser.ts`, `.node.ts`, `.react_native.ts`)
28-
- `__platforms` export for multi-platform files
29-
3. Parses import statements (ES6 imports, require(), dynamic imports)
30-
4. Validates that each import is compatible with the file's platform
31-
5. Fails with exit code 1 if any violations are found
26+
2. **Requires every non-test file to export `__platforms` array** declaring supported platforms
27+
3. Parses import statements (ES6 imports, require(), dynamic imports) using TypeScript AST
28+
4. Validates that each import is compatible with the file's declared platforms
29+
5. Fails with exit code 1 if any violations are found or if `__platforms` export is missing
30+
31+
**Note:** The validator can theoretically support file naming conventions (`.browser.ts`, etc.) in addition to `__platforms` exports, but currently enforces only the `__platforms` export. File naming is not validated and is considered redundant.
3232

3333
### Exit Codes
3434

0 commit comments

Comments
 (0)