Skip to content

fix(ui): lazy-load optional markdown peer deps to prevent import crashes - #210

Merged
zuohuadong merged 4 commits into
mainfrom
codex/fix-markdown-optional-peer-lazy-import
Aug 5, 2026
Merged

fix(ui): lazy-load optional markdown peer deps to prevent import crashes#210
zuohuadong merged 4 commits into
mainfrom
codex/fix-markdown-optional-peer-lazy-import

Conversation

@zuohuadong

Copy link
Copy Markdown
Owner

Problem

MarkdownRenderer statically imports optional peer dependencies (marked, highlight.js, isomorphic-dompurify). When a consumer app hasn't installed these packages, the import crashes at module load time — even if MarkdownRenderer is never actually rendered.

Solution

Convert the static imports to dynamic import() calls inside onMount. The component now:

  • Loads marked, marked-highlight, highlight.js, and isomorphic-dompurify asynchronously on mount
  • Falls back to escaped-text rendering if any dependency is missing
  • Properly cleans up via the onMount return function

Tests

Updated markdown-renderer.test.ts to use waitFor/waitForMarkdown helpers for async rendering. All 4 existing tests pass.

MarkdownRenderer now dynamically imports marked, highlight.js, and
isomorphic-dompurify inside onMount instead of at module scope. This
prevents consumers that haven't installed these optional peer deps from
crashing when MarkdownRenderer is bundled but never rendered.

Tests updated to use waitFor/waitForMarkdown for async rendering.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploying svadmin-example with  Cloudflare Pages  Cloudflare Pages

Latest commit: 77fb243
Status: ✅  Deploy successful!
Preview URL: https://580057e7.svadmin-example.pages.dev
Branch Preview URL: https://codex-fix-markdown-optional.svadmin-example.pages.dev

View logs

@zuohuadong

Copy link
Copy Markdown
Owner Author

审查结论:暂不合并(FAIL)。

阻塞项:当前 head 9af3573 的 GitHub Lint & Test 确定性失败,共 9 个本 PR 引入的 ESLint 错误:

  • MarkdownRenderer.svelte:7 个 no-explicit-any、1 个禁止的非空断言;
  • markdown-renderer.test.ts:未使用的 tick 导入。

因此 E2E 被跳过,尚无可接受的完成证据。请先修正类型/导入并让 CI 全绿。随后建议补一个最小消费者验收:不安装 Markdown 可选 peer,仅导入 @svadmin/ui 且不渲染 MarkdownRenderer 时,构建/启动不得失败;当前单元测试只验证安装了依赖后的异步渲染,未覆盖 PR 的核心消费者场景。

Svelte autofixer另提示现有 use:enhanceCodeBlocks 可考虑迁移 attachment;这不是本轮合并阻塞项。

@zuohuadong
zuohuadong merged commit ba73f1b into main Aug 5, 2026
4 checks passed
@zuohuadong
zuohuadong deleted the codex/fix-markdown-optional-peer-lazy-import branch August 5, 2026 15:16
@zuohuadong zuohuadong mentioned this pull request Aug 5, 2026
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