|
3 | 3 | // Why: Entry point for parsing OSF documents and converting them back to text |
4 | 4 | // Related: types.ts, lexer/index.ts, block-parsers/index.ts, serializers/index.ts |
5 | 5 |
|
6 | | -import { readFileSync } from 'fs'; |
| 6 | +// Conditional fs import - only available in Node.js/Bun environments |
| 7 | +// eslint-disable-next-line no-unused-vars |
| 8 | +let readFileSync: ((path: string, encoding: string) => string) | undefined; |
| 9 | +try { |
| 10 | + // This will work in Node.js and Bun, but throw in browsers |
| 11 | + readFileSync = require('fs').readFileSync; |
| 12 | +} catch { |
| 13 | + // Browser environment - fs not available |
| 14 | + // @include directive will throw helpful error if attempted |
| 15 | +} |
| 16 | + |
7 | 17 | import { resolve, dirname, relative, normalize } from 'path'; |
8 | 18 | import { OSFDocument, OSFBlock, DocBlock, IncludeDirective, ParseOptions } from './types'; |
9 | 19 | import { findBlocks } from './lexer'; |
@@ -155,6 +165,15 @@ function resolveDocumentIncludes( |
155 | 165 |
|
156 | 166 | resolvedPaths.add(fullPath); |
157 | 167 |
|
| 168 | + // Check if fs is available (Node.js/Bun environment) |
| 169 | + if (!readFileSync) { |
| 170 | + throw new Error( |
| 171 | + `@include directive requires a Node.js or Bun environment. ` + |
| 172 | + `The parser is running in a browser where file system access is not available. ` + |
| 173 | + `Path attempted: ${fullPath}` |
| 174 | + ); |
| 175 | + } |
| 176 | + |
158 | 177 | const content = readFileSync(fullPath, 'utf-8'); |
159 | 178 | const included = parse(content, { |
160 | 179 | resolveIncludes: false, // Don't auto-resolve, we'll do it manually |
|
0 commit comments