buttonの全variantがループしてcssへ出力されないようにmixinを調整#100
Open
itoh4126 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
mixinベースのスタイリング(エイリアシング)でボタンを使う際に、
$optionsで指定した値のスタイルだけをCSSに出力するようにstylemixinを調整しました。背景・課題
READMEで推奨しているエイリアシングの使い方で、
と書いても、従来の実装では指定した
size: m以外のsやxl、appearanceやcolorの他の値まで、すべてのvariantをループして.button-submitに対して出力していました(1回の@includeで約1000ルールセット)。エイリアシングではオプションを固定して使うことが多くの比重を占めるはずで、modifier用のスタイル(mixinベースでのエイリアシングでスタイリングしたinhouse buttonに対して-size-lなどclassベースのオプション指定を追加すること)は実質的に不要なCSSになっていると言えます。変更内容
styleの引数のデフォルトを$options: nullに変更し、挙動を分岐$options省略時: 従来どおり、デフォルトのスタイル + すべてのmodifier(-appearance-*,-color-*など)のスタイルを出力(export=.in-buttonはこのパターンです)$options指定時: 指定した値(未指定の属性はデフォルト値)のスタイルだけを出力し、modifierのスタイルは出力しない(上記の例では9ルールセットになる)skeleton-styleにも同じ扱いを適用互換性
export(.in-button/.in-skeleton-button)の出力は変更前後で同じでした(コンパイル結果をdiffで確認した)style/style-with-variantsのシグネチャは変わらなくて、引数なしの@include button.styleも従来と同一の出力でした。.in-callout配下です。calloutが内部でbutton.style($options: (size: $size, appearance: transparent))を呼んでいて、そこに展開されていた未使用のボタン全variant(3,716ルールセット)が出力されなくなります(.in-callout ._actionにmodifierを当てる使い方はドキュメント・リポジトリ内に存在しないことを確認してます)。この結果としてビルド済みCSS全体は45,191行 → 18,015行に削減されるようです。動作確認は
npm test(sassunit 全パッケージ): passed / failed: 0npm run lint:style: エラーなしinhouse-components-web.scssのコンパイル結果の変更前後diffで、上記callout以外に差分がないことを確認file:)して、サイト全体の表示・アイコンフォント・GlobalHeaderなどのbutton.style($options...)利用箇所に影響がないことを確認などを行いましたが、レビュワーはローカルで別プロジェクトへこのbranchのinhouse buttonをインストールして表示の変わり映えがないか・その上でコードが削減されているかなど確認してみてください。
補足
chip や sticker など他のパッケージにも全variantを無条件にループする同様の実装がありますが、まず使用頻度とvariantの数の多さゆえ最もCSSの削減が効果的と考えられるbuttonのみ対応してます。