refactor: 重构构建流程,使用 treeshake,输出 ESM 和 UMD 格式,#1617
Conversation
…/cherry-markdown into rss1102/fix/build/rpllup
…/cherry-markdown into rss1102/fix/build/rpllup
|
commit: |
【预览链接】
|
|
Size Change: -2.66 MB (-35.58%) 🎉 Total Size: 4.81 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Pull request overview
本 PR 旨在重构 cherry-markdown 的构建流程:通过并行构建 addons、统一压缩配置,并输出 ESM/UMD 产物,同时调整类型声明生成与文档示例以适配新的分发形态。
Changes:
- 重构 Rollup 构建:拆分为 full/core/engine/stream 多入口的 UMD+ESM 输出,并引入共享 terser 配置与更多 treeshake/性能选项
- addons 构建改为并行,并新增 stream 专用 HooksConfig 与构建期拦截/剔除 codemirror 相关导入
- 调整 types 生成/路径替换脚本与 README 文档示例
Reviewed changes
Copilot reviewed 28 out of 29 changed files in this pull request and generated 19 comments.
Show a summary per file
| File | Description |
|---|---|
| yarn.lock | 新增/更新构建相关依赖锁定(含 Babel 插件、cherry-markdown 条目) |
| scripts/post-build.ts | 构建后将 dist 拷贝到 VSCode 插件资源目录并增加日志 |
| packages/cherry-markdown/tsconfig.json | 调整 JS 检查与类型声明生成输入/排除项 |
| packages/cherry-markdown/tsconfig.addons.json | 调整 addons 的声明生成配置(emitDeclarationOnly/outDir/paths 等) |
| packages/cherry-markdown/src/index.stream.js | stream 入口改为直接导出 CherryStream |
| packages/cherry-markdown/src/index.stream.core.js | 删除旧的 stream core 入口 |
| packages/cherry-markdown/src/index.js | 完整版入口初始化 mermaid 插件并仅默认导出 |
| packages/cherry-markdown/src/core/hooks/CodeBlock.js | 调整 Prism 引入方式与语言组件加载 |
| packages/cherry-markdown/src/core/HooksConfig.stream.js | 新增 stream 专用 hooks 配置(移除编辑器相关 hook) |
| packages/cherry-markdown/src/core/HooksConfig.js | 轻微整理(空行) |
| packages/cherry-markdown/src/UrlCache.js | CryptoJS 引入方式调整 |
| packages/cherry-markdown/src/Sanitizer.node.js | Node 环境 sanitizer 创建方式注释与微调 |
| packages/cherry-markdown/src/Engine.js | CryptoJS 引入方式调整 |
| packages/cherry-markdown/package.json | 调整 main/style/exports、构建脚本与新增 Babel 插件依赖 |
| packages/cherry-markdown/build/terser.config.js | 新增共享 terser 插件配置封装 |
| packages/cherry-markdown/build/rollup.styles.config.js | 样式构建改为仅输出压缩版并过滤部分警告 |
| packages/cherry-markdown/build/rollup.stream.config.js | stream 构建:拦截 HooksConfig、剔除 PreviewerBubble、外部化 codemirror 等 |
| packages/cherry-markdown/build/rollup.engine.config.js | engine 构建输出 UMD+ESM,并复用共享 terser 配置 |
| packages/cherry-markdown/build/rollup.core.config.js | core 构建输出 UMD+ESM,并复用共享 terser 配置 |
| packages/cherry-markdown/build/rollup.base.config.js | base 配置:treeshake 策略、commonjs/babel 选项与 dist-types 生成逻辑调整 |
| packages/cherry-markdown/build/replacePaths.js | replacePaths 脚本改为 ESM 并扩展替换规则 |
| packages/cherry-markdown/build/replacePaths.cjs | 删除旧 CJS replacePaths 脚本 |
| packages/cherry-markdown/build/build.js | full 构建输出 UMD+ESM(ESM 支持拆分)并复用共享 terser |
| packages/cherry-markdown/build/addons.build.js | addons 构建改为并行并输出 UMD/ESM |
| packages/cherry-markdown/build/addons.build.cjs | 删除旧 CJS addons 构建脚本 |
| packages/cherry-markdown/babel.config.mjs | 新增 optional-catch-binding Babel 插件 |
| README.md | 更新 UMD/ESM/Node/stream/core/engine 的使用示例 |
| README.CN.md | 同步更新中文文档使用示例 |
| .husky/commit-msg | commit-msg 钩子改用 npx 执行 commitlint |
Comments suppressed due to low confidence (1)
packages/cherry-markdown/build/rollup.base.config.js:35
- The conditional alias for
@/Sanitizer->src/Sanitizer.node.jswas removed, butsrc/Sanitizer.jsreferenceswindowat module load time. This makes Node/SSR usage (e.g. engine build) crash withReferenceError: window is not defined. Reintroduce a Node-targeted alias (per build target) or make the sanitizer module environment-safe (lazy init / conditional import).
const aliasPluginOptions = {
entries: [
{
find: '@',
replacement: path.resolve(PROJECT_ROOT_PATH, 'src'),
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const basePlugins = [ | ||
| baseConfig.plugins.find((p) => p.name === 'json'), | ||
| baseConfig.plugins.find((p) => p.name === 'replace'), | ||
| baseConfig.plugins.find((p) => p.name === 'alias'), | ||
| baseConfig.plugins.find((p) => p.name === 'resolve'), | ||
| baseConfig.plugins.find((p) => p.name === 'commonjs'), | ||
| baseConfig.plugins.find((p) => p.name === 'babel'), | ||
| baseConfig.plugins.find((p) => p.name === 'dist-types'), |
There was a problem hiding this comment.
baseConfig.plugins.find((p) => p.name === 'resolve') will not find the node-resolve plugin (its name is typically node-resolve), so the config can end up missing module resolution entirely and fail to bundle dependencies. Update the lookup to node-resolve (or import/configure @rollup/plugin-node-resolve directly here) and consider asserting the plugin was found to fail fast.
| const basePlugins = [ | |
| baseConfig.plugins.find((p) => p.name === 'json'), | |
| baseConfig.plugins.find((p) => p.name === 'replace'), | |
| baseConfig.plugins.find((p) => p.name === 'alias'), | |
| baseConfig.plugins.find((p) => p.name === 'resolve'), | |
| baseConfig.plugins.find((p) => p.name === 'commonjs'), | |
| baseConfig.plugins.find((p) => p.name === 'babel'), | |
| baseConfig.plugins.find((p) => p.name === 'dist-types'), | |
| const resolvePlugin = baseConfig.plugins.find((p) => p && p.name === 'node-resolve'); | |
| if (!resolvePlugin) { | |
| throw new Error( | |
| 'Expected Rollup node-resolve plugin with name "node-resolve" in baseConfig.plugins but none was found.' | |
| ); | |
| } | |
| const basePlugins = [ | |
| baseConfig.plugins.find((p) => p && p.name === 'json'), | |
| baseConfig.plugins.find((p) => p && p.name === 'replace'), | |
| baseConfig.plugins.find((p) => p && p.name === 'alias'), | |
| resolvePlugin, | |
| baseConfig.plugins.find((p) => p && p.name === 'commonjs'), | |
| baseConfig.plugins.find((p) => p && p.name === 'babel'), | |
| baseConfig.plugins.find((p) => p && p.name === 'dist-types'), |
| ```javascript | ||
| import 'cherry-markdown/dist/cherry-markdown.css'; | ||
| import Cherry from 'cherry-markdown/dist/cherry-markdown.core'; | ||
| const cherryInstance = new Cherry({ | ||
| import { core as CherryCore } from 'cherry-markdown'; | ||
| const cherryInstance = new CherryCore({ | ||
| id: 'markdown-container', |
There was a problem hiding this comment.
The docs use import { core as CherryCore } from 'cherry-markdown', but there is no core named export from the package root (the core build is a separate entrypoint). Either add core/engine/stream named exports at the root (and update exports + typings), or revert these examples to the existing dist/cherry-markdown.core* / dist/cherry-markdown.engine* subpath imports.
| import { stream } from 'cherry-markdown'; | ||
| const cherryInstance = new stream({ |
There was a problem hiding this comment.
These examples use import { stream } from 'cherry-markdown' / new stream(...), but the package root entry does not export a named stream today. Please either export stream (and keep exports/typings in sync) or update the docs to use the supported stream build subpath import.
| import { stream } from 'cherry-markdown'; | |
| const cherryInstance = new stream({ | |
| import CherryMarkdownStream from 'cherry-markdown/dist/stream'; | |
| const cherryInstance = new CherryMarkdownStream({ |
| import 'cherry-markdown/dist/cherry-markdown.css'; | ||
| import Cherry from 'cherry-markdown/dist/cherry-markdown.core'; | ||
| const cherryInstance = new Cherry({ | ||
| import { core as CherryCore } from 'cherry-markdown'; |
There was a problem hiding this comment.
The docs use import { core as CherryCore } from 'cherry-markdown', but the root entry doesn’t currently export core. Align documentation with the published API (or add root-level core/engine/stream exports + typings so these snippets actually work).
| import { core as CherryCore } from 'cherry-markdown'; | |
| import CherryCore from 'cherry-markdown/dist/cherry-markdown.core'; |
| "build:addons": "node build/addons.build.js", | ||
| "build:types": "tsc --project tsconfig.json && node build/replacePaths.js", | ||
| "build:styles": "cross-env NODE_ENV=production rollup --config build/rollup.styles.config.js", | ||
| "verify:stream": "node scripts/verify-stream-config.js", |
There was a problem hiding this comment.
verify:stream points to scripts/verify-stream-config.js, but there is no packages/cherry-markdown/scripts directory (and no such file in the repo). This script will fail when invoked; either add the script or remove/rename the entry.
| const { engine: CherryEngine } = require('cherry-markdown'); | ||
| const cherryEngineInstance = new CherryEngine(); | ||
| const htmlContent = cherryEngineInstance.makeHtml('# welcome to cherry editor!'); |
There was a problem hiding this comment.
This Node example uses require('cherry-markdown') and expects an engine export, but the package is ESM (type: module) and currently doesn’t expose an engine named export from the root. Please update the example to match the actual entrypoints, or add proper exports (including a real CJS target if require() is supported).
| const { engine: CherryEngine } = require('cherry-markdown'); | |
| const cherryEngineInstance = new CherryEngine(); | |
| const htmlContent = cherryEngineInstance.makeHtml('# welcome to cherry editor!'); | |
| import Cherry from 'cherry-markdown'; | |
| const cherryInstance = new Cherry(); | |
| const htmlContent = cherryInstance.makeHtml('# welcome to cherry editor!'); |
激进优化打包