Skip to content

Commit 7c78549

Browse files
committed
fix(parser): make fs import conditional for browser compatibility
- Use conditional require('fs') with try/catch for browser environments - @include directive works in Node.js/Bun (CLI) but gracefully fails in browser - Fix CI Puppeteer installation to use 'pnpm exec' instead of 'npx' - Ensures parser works in both server and browser environments Fixes website deployment failure (Module not found: Can't resolve 'fs') Maintains full CLI functionality for all NPM users
1 parent 6f174a0 commit 7c78549

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
run: pnpm install --frozen-lockfile
5353

5454
- name: Install Chrome for Puppeteer
55-
run: npx puppeteer browsers install chrome
55+
run: cd cli && pnpm exec puppeteer browsers install chrome
5656

5757
- name: Type check
5858
run: pnpm run typecheck
@@ -218,9 +218,7 @@ jobs:
218218
run: pnpm install --frozen-lockfile
219219

220220
- name: Install Puppeteer Chrome browser
221-
run: |
222-
cd cli
223-
npx --yes puppeteer browsers install chrome
221+
run: cd cli && pnpm exec puppeteer browsers install chrome
224222

225223
- name: Build packages
226224
run: pnpm run build

parser/src/parser.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
// Why: Entry point for parsing OSF documents and converting them back to text
44
// Related: types.ts, lexer/index.ts, block-parsers/index.ts, serializers/index.ts
55

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+
717
import { resolve, dirname, relative, normalize } from 'path';
818
import { OSFDocument, OSFBlock, DocBlock, IncludeDirective, ParseOptions } from './types';
919
import { findBlocks } from './lexer';
@@ -155,6 +165,15 @@ function resolveDocumentIncludes(
155165

156166
resolvedPaths.add(fullPath);
157167

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+
158177
const content = readFileSync(fullPath, 'utf-8');
159178
const included = parse(content, {
160179
resolveIncludes: false, // Don't auto-resolve, we'll do it manually

0 commit comments

Comments
 (0)