Skip to content

fix: 将 highlight.js 改为静态导入以兼容 Bun --compile 模式#335

Merged
claude-code-best merged 1 commit into
claude-code-best:mainfrom
realorange1994:feature/cli-highlight
Apr 23, 2026
Merged

fix: 将 highlight.js 改为静态导入以兼容 Bun --compile 模式#335
claude-code-best merged 1 commit into
claude-code-best:mainfrom
realorange1994:feature/cli-highlight

Conversation

@realorange1994
Copy link
Copy Markdown

@realorange1994 realorange1994 commented Apr 23, 2026

  • cliHighlight.ts: 使用静态 import 替换 dynamic import('highlight.js'), 因为编译模式下模块解析指向内部 bunfs 路径导致无法找到产生崩溃
  • color-diff-napi/src/index.ts: 同样改为静态导入,移除 createRequire 延迟加载

Summary by CodeRabbit

  • Refactor
    • Updated syntax highlighting module loading for improved compatibility across different JavaScript runtime environments.

- cliHighlight.ts: 使用静态 import 替换 dynamic import('highlight.js'),
  因为编译模式下模块解析指向内部 bunfs 路径导致无法找到
- color-diff-napi/src/index.ts: 同样改为静态导入,移除 createRequire 延迟加载
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

📝 Walkthrough

Walkthrough

Both files transition from runtime dynamic imports to static imports of highlight.js, with new CJS/ESM interoperability helpers to normalize module loading across Bun/ESM and Node/CJS environments.

Changes

Cohort / File(s) Summary
highlight.js Import Refactoring
packages/color-diff-napi/src/index.ts, src/utils/cliHighlight.ts
Replaces runtime dynamic imports (require() and await import()) with static top-level imports. Introduces interop helpers (hljsApi()) to handle CJS/ESM module resolution, selecting default export when available. Removes lazy-loader function and adjusts initialization logic with fallback patterns for getLanguage access.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Static imports hop in with grace,
No more dynamic loading to chase,
Bun and Node now share one space,
With interop helpers keeping pace! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: converting highlight.js to static imports for Bun --compile compatibility, which matches the core objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/color-diff-napi/src/index.ts`:
- Line 21: The file imports highlight.js via "import hljs from 'highlight.js'"
making it a runtime dependency; add "highlight.js" to the dependencies section
of packages/color-diff-napi/package.json (pick a compatible semver version, e.g.
the latest stable), run your package manager to update lockfile, and verify the
build/tests so the runtime importer (hljs) resolves correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7872d6c9-cd38-43c1-b18e-0a95b148f67d

📥 Commits

Reviewing files that changed from the base of the PR and between 1173a62 and 7d4c427.

📒 Files selected for processing (2)
  • packages/color-diff-napi/src/index.ts
  • src/utils/cliHighlight.ts

import { createRequire } from 'node:module'
import { diffArrays } from 'diff'
import type * as hljsNamespace from 'highlight.js'
import hljs from 'highlight.js'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

pkg="$(fd -t f '^package\.json$' packages/color-diff-napi | head -n 1)"

if [ -z "${pkg}" ]; then
  printf 'packages/color-diff-napi/package.json was not found\n' >&2
  exit 1
fi

printf 'Checking %s\n' "$pkg"
jq '{
  name,
  dependencies: .dependencies["highlight.js"],
  optionalDependencies: .optionalDependencies["highlight.js"],
  peerDependencies: .peerDependencies["highlight.js"],
  devDependencies: .devDependencies["highlight.js"]
}' "$pkg"

Repository: claude-code-best/claude-code

Length of output: 265


Add highlight.js to runtime dependencies.

The static import on line 21 makes highlight.js a direct runtime requirement, but it is not declared in package.json dependencies. This will cause resolution failures under strict package managers or isolated builds. Add highlight.js to the dependencies field in packages/color-diff-napi/package.json.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/color-diff-napi/src/index.ts` at line 21, The file imports
highlight.js via "import hljs from 'highlight.js'" making it a runtime
dependency; add "highlight.js" to the dependencies section of
packages/color-diff-napi/package.json (pick a compatible semver version, e.g.
the latest stable), run your package manager to update lockfile, and verify the
build/tests so the runtime importer (hljs) resolves correctly.

@claude-code-best claude-code-best merged commit b642977 into claude-code-best:main Apr 23, 2026
2 of 3 checks passed
dfsfdfse pushed a commit to dfsfdfse/claude-code that referenced this pull request Apr 25, 2026
…li-highlight

fix: 将 highlight.js 改为静态导入以兼容 Bun --compile 模式
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.

2 participants