Repository navigation
Open
Labels
enhancementNew feature or request
Description
As is
@d-zero/html-distiller(2.0.12)の distill()(packages/@d-zero/html-distiller/src/distill.ts)は、html-minifier-terser で最小化した HTML を、実行時依存(dependencies)の prettier(3.9.5)で整形してから parse5 に渡している。
const formatted = await format(minified, {
parser: 'html',
useTabs: true,
printWidth: 100_000,
});
const ast = parse(formatted);リポジトリの開発用ツールは ESLint・Prettier・cspell から oxlint・oxfmt に移行したため、整形ツールが 2 系統(開発用の oxfmt と、この実行時依存の prettier)になっている。prettier はこのパッケージの package.json にだけ残り、Renovate でも追い続ける必要がある。
distill() の後段 walk()(src/walk.ts)は #text を trim() し、空文字は結果に入れない。整形が結果の木に与える影響は、空白と、title / link / script / style / iframe の tagString() が連結する文字列に限られる見込みだが、これは出力を比べるまで確定しない。
To be
distill() が実行時に prettier を使わない。整形の依存はリポジトリで使う oxfmt に一本化するか、整形が不要と確認できれば依存ごと外す。distill() が返す木は、既存の入力すべてで変わらない(変わる場合は破壊的変更として版を上げる)。
Why
- 置き換え先の候補は実在する。
oxfmt@0.62.0は Node API としてformat(fileName: string, sourceText: string, options?: FormatConfig): Promise<FormatResult>を公開し(dist/index.d.ts)、FormatResultはcodeとerrorsを返す。設定のuseTabsとprintWidthは Prettier と同じ名前のオプションで、bracketSameLineなど HTML を対象言語に挙げるオプションもある。oxfmt の Prettier からの移行手順は oxfmt の README がhttps://oxc.rs/docs/guide/usage/formatter/migrate-from-prettier.htmlとして案内している。 @d-zero/html-distillerは公開パッケージで、@d-zero/*を exact pin で使う社内の各リポジトリが使う。distill()の出力が変わると利用側を壊すため、置き換えは出力が同一であることの確認が前提になる。
How
packages/@d-zero/html-distiller/src/distill.tsのimport { format } from 'prettier'を oxfmt のformat()に替える。ファイル名は*.htmlを渡し(拡張子で言語が決まる)、useTabs: trueとprintWidth: 100_000を同じ値で渡す。戻り値は.codeを使い、.errorsが空でないときの扱いを決める(現状のprettierは構文エラーで throw する)。packages/@d-zero/html-distiller/package.jsonのdependenciesからprettierを外し、oxfmtを exact pin で足す(ネイティブバインディングが optional dependencies に入るため、公開後のnpm installでプラットフォーム別の取得になる点を確認する)。.github/renovate.jsonのoxlintグループ(oxlint・oxlint-tsgolint)にoxfmtを入れるか判断する。- 出力の同一性を確認する。
packages/@d-zero/html-distiller/src/distill.spec.tsを通すだけでなく、旧実装(prettier)と新実装(oxfmt)のdistill()の結果を、distill.spec.tsの入力とpackages/@d-zero/anatomist/src/__fixtures__/*.htmlの入力で比べる。差が出るなら、walk()のtrim()で吸収できない差(tagString()の連結結果など)かを見て、破壊的変更として版を上げるか、整形自体が不要かを判断する。
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity