Skip to content

Conversation

@charlespwd
Copy link
Contributor

Summary

Resolves ESM/CJS incompatibility so liquid-html-parser can consume the ESM-only @ohm-js/wasm package.

Key Changes

  • ESM tsconfig: Configure TypeScript for ESM output (module: "ESNext", moduleResolution: "bundler")
  • @ohm-js/wasm/compat imports: Use the compatibility API that works in both Node.js and browser
  • res.use() patterns: Replace global use() calls with resource-scoped res.use() for grammar loading
  • Webpack fix for OHM_DEBUG: Define process.env.OHM_DEBUG in browser bundle to prevent runtime errors

Test Results

  • Node.js: 1737/1740 tests pass (3 expected message format changes due to ohm-js version)
  • Browser: 3/3 tests pass

Context

Builds on Patrick's PR #1070 work for browser test infrastructure.

Update tsconfig.json for ES2020 target and node20 module system.
Add explicit moduleResolution: node to codemirror-language-client
and lang-jsonc packages.

This is preliminary configuration work for ESM compatibility.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
charlespwd and others added 2 commits January 31, 2026 10:54
Replace ohm-js with @ohm-js/wasm for browser compatibility.

Key changes:
- Add @ohm-js/wasm dependency
- Update imports to use @ohm-js/wasm and @ohm-js/wasm/compat
- Replace toAST() with new AstBuilder(mappings).toAst() pattern
- Update CST mapping functions for WASM CST structure:
  - Use opt.when({ Some:..., None:... }) for optional nodes
  - Use iter.when() for iteration nodes
  - Adjust numeric indices for non-flattened Opt nodes
  - Use res.use() for MatchResult lifetime management
- Update CstNode types and AstBuilder generics

This enables the parser to work in browser environments where
CommonJS require() is not available.

Co-Authored-By: Patrick Dubroy <pdubroy@gmail.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The @ohm-js/wasm Compiler module references process.env.OHM_DEBUG for
debug logging. This doesn't exist in browser environments, causing
"process is not defined" errors when the extension activates.

Add DefinePlugin entries for both browserClientConfig and
browserServerConfig to set this to false at build time.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
charlespwd and others added 3 commits January 31, 2026 12:04
The previous ESM commit (542561b) configured TypeScript but left
the source files using CommonJS patterns. This commit completes
the migration:

- Add `"type": "module"` to package.json (required for Node.js to
  recognize .js files as ESM)
- Add .js extensions to all relative imports (required by ESM spec)
- Convert grammar loader from CommonJS require() to ESM import
- Rename shims.js to shims.cjs (build script uses CommonJS)
- Update shims to generate `export default` instead of `module.exports`
- Exclude grammar folder from TypeScript build (prevents overwrite error)

Without `"type": "module"`, TypeScript's node20 module setting
defaults to CommonJS output based on the package.json type field.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The CI build failed because TypeScript tried to emit the grammar
JS file back to its source location. This happened because:
1. Root tsconfig has `allowJs: true`
2. TypeScript processed the imported grammar file as input
3. With ESM, the grammar file is in the module graph

Fix by:
- Set `allowJs: false` in tsconfig.build.json to prevent TS from
  processing the .js file as an input
- Generate a .d.ts alongside the .js in shims.cjs to provide types

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The previous commits attempted a full ESM migration for liquid-html-parser
which broke downstream CommonJS consumers (ERR_REQUIRE_ESM error).

Revert to CommonJS output while keeping @ohm-js/wasm imports. This works
because:
1. TypeScript's module:node20 outputs CommonJS when there's no "type":"module"
2. Node.js can require() ESM packages from CommonJS code (internal handling)
3. Webpack bundles both CJS and ESM seamlessly

Key reversions:
- Remove "type": "module" from package.json
- Revert grammar.ts to use require() for grammar file
- Revert shims.cjs to generate module.exports
- Remove .js extensions from relative imports
- Remove allowJs:false and .d.ts generation

This reverts commits 39c29d8 and 75093c7.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant