<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sass &#8211; Mach3.laBlog</title>
	<atom:link href="https://blog.mach3.jp/tag/sass/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.mach3.jp</link>
	<description></description>
	<lastBuildDate>Tue, 26 Mar 2013 01:28:16 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>コーダーさんの為のGrunt入門（後編）〜CSSプリコンパイラ編</title>
		<link>https://blog.mach3.jp/2013/03/26/gruntjs-css.html</link>
		
		<dc:creator><![CDATA[mach3]]></dc:creator>
		<pubDate>Tue, 26 Mar 2013 01:28:16 +0000</pubDate>
				<category><![CDATA[Laboratory]]></category>
		<category><![CDATA[Application]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Grunt.js]]></category>
		<category><![CDATA[LESS]]></category>
		<category><![CDATA[Sass]]></category>
		<category><![CDATA[Stylus]]></category>
		<guid isPermaLink="false">http://blog.mach3.jp/?p=3066</guid>

					<description><![CDATA[前編の導入編 に続いて、 コーダーさんにとって強力な武器である、Less/Sass/Stylus等のCSSプリコンパイラのコンパイルを Gruntにお任せしてみる方法を紹介していきます。 Less/Sass/Stylus [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><a href="/2013/03/gruntjs.html">前編の導入編</a> に続いて、 コーダーさんにとって強力な武器である、Less/Sass/Stylus等のCSSプリコンパイラのコンパイルを Gruntにお任せしてみる方法を紹介していきます。</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh3.googleusercontent.com/-InKstkN_iLo/UUl5hpVLk2I/AAAAAAAACAg/zGNCjzkb8f0/s400/20120320-00.png" alt="コーダーさんの為のGrunt入門（後編）〜CSSプリコンパイラ編"/></figure>



<p></p>



<span id="more-3066"></span>



<h2 class="wp-block-heading">Less/Sass/Stylus をGruntでコンパイルする</h2>



<p>ではさっそく、GruntでCSSプリコンパイラを扱うタスクを紹介します。</p>



<ul class="wp-block-list">
<li><a href="https://github.com/gruntjs/grunt-contrib-less">grunt-contrib-less</a> &#8211; LessをCSSにコンパイルします</li>



<li><a href="https://github.com/gruntjs/grunt-contrib-sass">grunt-contrib-sass</a> &#8211; SassをCSSにコンパイルします</li>



<li><a href="https://github.com/gruntjs/grunt-contrib-stylus">grunt-contrib-stylus</a> &#8211; StylusをCSSにコンパイルします</li>
</ul>



<p>指定できるオプションは異なりますが、設定の仕方に差はありません。</p>



<p>違いを挙げるならば、Less/StylusはJavaScriptで書かれているのでnode.js上で動作させられますが、 RubyベースのSassは外部のsassコマンドを呼び出す仕組みになっており、別途インストールが必要です。 結果、Gruntとより相性が良いのはLess/Stylusであると言えますね。</p>



<p>ここでは、私の好きなLessを例にあげてみます。</p>



<h2 class="wp-block-heading">Lessをコンパイルする（grunt-contrib-less）</h2>



<h3 class="wp-block-heading">基本設定</h3>



<p>まず &#8220;grunt-contrib-less&#8221; をインストールしておきます。</p>


<pre class="wp-block-code"><span><code class="hljs">$ npm install grunt-contrib-less
</code></span></pre>


<p>Gruntfile.js の内容は、例えば次のようになります。</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">grunt</span>)</span>{

    <span class="hljs-comment">// grunt-contrib-less タスクの読み込み</span>
    grunt.loadNpmTasks(<span class="hljs-string">"grunt-contrib-less"</span>);

    grunt.initConfig({
        <span class="hljs-attr">less</span> : {
            <span class="hljs-comment">// 作業のラベルは"dist"にしてみます</span>
            <span class="hljs-attr">dist</span> : {
                <span class="hljs-comment">// オプションの指定</span>
                <span class="hljs-attr">options</span> : {
                    <span class="hljs-attr">compress</span> : <span class="hljs-literal">true</span>
                },
                <span class="hljs-comment">// コンパイルするファイルの指定</span>
                <span class="hljs-attr">files</span> : {
                    <span class="hljs-string">"./assets/css/style.css"</span> : <span class="hljs-string">"./assets/less/style.less"</span>
                }
            }
        }
    });
};
</code></span></pre>


<p>※ 基本的なタスクの書き方については <a href="/2013/03/gruntjs.html">前編</a> にて</p>



<p><em>&#8220;options&#8221;</em>セクションでオプションの設定を行い、 <em>&#8220;files&#8221;</em>セクションで出力するファイルの指定を行います。</p>



<p><em>&#8220;files&#8221;</em>のキー（左辺）には出力先のCSSファイル、値（右辺）には元となるLessファイルへのパスを指定してください。 上の例では、 &#8220;./assets/less/style.less&#8221; がコンパイルされた結果が、&#8221;./assets/css/style.css&#8221; に保存されます。</p>



<h3 class="wp-block-heading">実行</h3>


<pre class="wp-block-code"><span><code class="hljs">$ grunt less:dist
</code></span></pre>


<h3 class="wp-block-heading">オプション</h3>



<p>多く使われるであろうオプションは次の物でしょう。</p>



<ul class="wp-block-list">
<li><em>compress</em> : Boolean (false) &#8211; 空白などを除去して圧縮します</li>



<li><em>yuicompress</em> : Boolean (false) &#8211; cssmin.js（YUICompressorのJS版）で圧縮します</li>
</ul>



<p>その他のオプションについては <a href="https://github.com/gruntjs/grunt-contrib-less">grunt-contrib-lessのREADME</a> を参照してください。</p>



<h2 class="wp-block-heading">ファイルの更新を検知してコンパイルする（grunt-contrib-watch）</h2>



<h3 class="wp-block-heading">基本設定</h3>



<p>多くのコンパイラにも実装されているファイル監視機能ですが、 Gruntでも <a href="http://github.com/gruntjs/grunt-contrib-watch">grunt-contrib-watch</a> というプラグインとして提供されています。</p>


<pre class="wp-block-code"><span><code class="hljs">npm install grunt-contrib-watch
</code></span></pre>


<p>先ほどのLessのタスクを、ファイルを監視して実行してみます。</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">grunt</span>)</span>{
    grunt.loadNpmTasks(<span class="hljs-string">"grunt-contrib-less"</span>);
    grunt.loadNpmTasks(<span class="hljs-string">"grunt-contrib-watch"</span>);

    grunt.initConfig({
        <span class="hljs-attr">less</span> : {
            <span class="hljs-attr">dist</span> : { ... }
        },
        <span class="hljs-attr">watch</span> : {
            <span class="hljs-comment">// ラベルは"less"にしてみます</span>
            <span class="hljs-attr">less</span> : {
                <span class="hljs-comment">// "files"セクションで監視するファイルの条件を指定</span>
                <span class="hljs-attr">files</span> : &#91;
                    <span class="hljs-string">"./assets/less/*.less"</span>
                ],
                <span class="hljs-comment">// "tasks"セクションで実行するタスクを指定</span>
                <span class="hljs-attr">tasks</span> : &#91;
                    <span class="hljs-string">"less:dist"</span>
                ]
            }
        }
    });
};
</code></span></pre>


<p><em>&#8220;files&#8221;</em>セクションで監視するファイルの条件を設定します。 そして<em>&#8220;tasks&#8221;</em>セクションで、<em>&#8220;files&#8221;</em>のファイルが更新された場合に実行するタスクを設定します。</p>



<p><em>&#8220;files&#8221;</em>の値には、直接ファイル名を指定する他にパターンマッチングを使用する事ができ、特定ファイルの除外などの細かい設定が可能です。 詳細は公式ドキュメントにあるのでご参照ください。</p>



<p>cf) <a href="http://gruntjs.com/configuring-tasks#globbing-patterns">Globbing patterns | Configuring tasks &#8211; Grunt: The JavaScript Task Runner</a></p>



<h3 class="wp-block-heading">実行</h3>



<p>監視を開始するとターミナルは待機状態になります。</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">$ grunt watch:less
Running <span class="hljs-string">"watch:less"</span> task
Waiting...
</code></span></pre>


<p>&#8220;./assets/less/&#8221; にあるLessファイルを更新すると、自動的に &#8220;less:dist&#8221; タスクを実行する様になります。</p>



<h3 class="wp-block-heading">livereload もある？</h3>



<p>私は使ったことがありませんが、livereload（ファイル更新を検知してブラウザをリロードする）を実現するプラグインもあるようです。</p>



<ul class="wp-block-list">
<li><a href="https://github.com/gruntjs/grunt-contrib-livereload">gruntjs/grunt-contrib-livereload · GitHub</a></li>
</ul>



<p>ここでは説明を省きますが、このプラグインは上で挙げた <em>&#8220;grunt-contrib-watch&#8221;</em> ではなく <a href="https://github.com/yeoman/grunt-regarde">&#8220;grunt-regarde&#8221;</a> の使用が推奨されるようです。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Note that grunt-contrib-livereload is designed to use grunt-regarde instead grunt-contrib-watch (mainly due to shortcomings in the watch task which doesn&#8217;t give access to changed files because it spawns tasks in subprocesses.)</p>
</blockquote>



<h2 class="wp-block-heading">GUIコンパイラでいいんじゃないの？</h2>



<p>非常にもっともなご意見です。</p>



<p>本記事で挙げたCSSプリコンパイラの事例に限って言えば、 <a href="http://compass.handlino.com/">Compass</a>、<a href="http://incident57.com/codekit/">CodeKit</a>や<a href="http://incident57.com/less/">Less.app</a>等、優秀なGUIコンパイラが数多くリリースされているので、 「黒い画面」が苦手な方は無理して導入する事もないと思います。</p>



<p>ただ、GUIコンパイラに対するGruntの優位点を挙げるとすれば、 環境を跨いでの設定の共有が極めて容易な点かな、と思っています。 nodeが動く環境ならばOS問わずどこでも同じ設定ファイルで、コマンド一発で同じ事が出来るわけです。</p>



<p><a href="/2013/03/gruntjs.html">前編</a>で触れたGruntの魅力に興味を持たれたならば、 これを機に「黒い画面」とのお付き合いを始めてみても良いのではないでしょうか。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p>そんなわけで、最近ではGitとGruntとBowerのセットで制作を進めています。</p>



<p>これらの優秀なソフトウェアの開発者には足をむけて寝られませんが、 オープンソース故に多方面にいらっしゃるわけで、逆立ちして寝るしかなさそうです。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>LessにおけるMixin活用法</title>
		<link>https://blog.mach3.jp/2011/12/15/how-to-use-mixin-of-less.html</link>
		
		<dc:creator><![CDATA[mach3]]></dc:creator>
		<pubDate>Thu, 15 Dec 2011 01:08:54 +0000</pubDate>
				<category><![CDATA[Laboratory]]></category>
		<category><![CDATA[Advent Calendar 2011]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[LESS]]></category>
		<category><![CDATA[Sass]]></category>
		<guid isPermaLink="false">http://blog.mach3.jp/?p=2174</guid>

					<description><![CDATA[Less &#38; Sass Advent calendar 2011 の15日目です！ 今日はLessにおけるMixInの活用法について書いてみようと思います。 LessのMixinについて LessにおけるMixi [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><a href="http://atnd.org/events/21919">Less &amp; Sass Advent calendar 2011</a> の15日目です！<br />
今日はLessにおけるMixInの活用法について書いてみようと思います。</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh4.googleusercontent.com/-sCFbH4aDvWI/TubnZSOUsZI/AAAAAAAABa4/4EOC8L9YsbY/s400/20111215.png" alt="LessにおけるMixin活用法"/></figure>



<p></p>



<span id="more-2174"></span>



<h2 class="wp-block-heading">LessのMixinについて</h2>



<p>LessにおけるMixinというのは、簡単に言うと関数のようなもので、<br />
どこかで宣言しておけば、セレクタの中で呼び出すことが出来、<br />
結果としてその中身がセレクタ内に展開されます。</p>



<p>ごちゃごちゃとわかりづらいので、公式の例を拝借します。</p>


<pre class="wp-block-code"><span><code class="hljs">.rounded-corners (@radius: 5px) {
    border-radius: @radius;
    -webkit-border-radius: @radius;
    -moz-border-radius: @radius;
}
</code></span></pre>


<p>こんな感じに宣言しておきます。()の中身は引数と初期値です。<br />
セレクタ内で呼び出すときは…</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#header {</span>
    .rounded-corners;
}
<span class="hljs-comment">#footer {</span>
    .rounded-corners( <span class="hljs-number">10</span>px );
}
</code></span></pre>


<p>こんな感じで呼び出すと、それぞれ展開されて下のコードのようになります。</p>



<p>&#8220;#header&#8221;の方は引数を渡していないので、初期値の5pxが適用されています。</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-comment">/* 出力結果 */</span>
<span class="hljs-selector-id">#header</span> {
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">-moz-border-radius</span>: <span class="hljs-number">5px</span>;
}
<span class="hljs-selector-id">#footer</span> {
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>: <span class="hljs-number">10px</span>;
    <span class="hljs-attribute">-moz-border-radius</span>: <span class="hljs-number">10px</span>;
}
</code></span></pre>


<h2 class="wp-block-heading">SassのMixinとの違い</h2>



<p>SassとLessの記法は非常に良く似ていますが、<br />
大きく異なる点として、変数の記号（@ではなく$）と、<br />
Mixinの書き方があげられます。</p>



<p>前述の例をSCSSで書くと、こんな感じになると思います。</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@mixin rounded-corners( $radius:<span class="hljs-number">5</span>px ){
    border-radius: $radius;
    -webkit-border-radius: $radius;
    -moz-border-radius: $radius;
    border-radius:$radius;
}

<span class="hljs-comment">#header {</span>
    @<span class="hljs-keyword">include</span> rounded-corners;
}
<span class="hljs-comment">#footer {</span>
    @<span class="hljs-keyword">include</span> rounded-corners( <span class="hljs-number">10</span>px );
}
</code></span></pre>


<p>Lessが通常のCSSクラスの記法と酷似しているのに対し、<br />
Sassの記法は、一発でMixinとわかるような書き方になっていますね。<br />
可読性の点ではSassの方に軍配があがりそうですが、<br />
この記法はLessの長所の一つでもあります。</p>



<h2 class="wp-block-heading">CSSクラスもMixinとして使える</h2>



<p>LessのMixinはCSSクラスと似ているだけではなく、<br />
普通のCSSクラスもMixinとして使用する事ができます。</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">// 普通のCSSクラス</span>
.hoge {
    background-color:<span class="hljs-comment">#333;</span>
    color:<span class="hljs-comment">#fff;</span>
}
.fuga {
    .hoge;
    font-weight:bold;
}
</code></span></pre>


<h3 class="wp-block-heading">出力結果</h3>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-class">.hoge</span> {
    <span class="hljs-attribute">background-color</span>:<span class="hljs-number">#333</span>;
    <span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>;
}

<span class="hljs-selector-class">.fuga</span> {
    <span class="hljs-attribute">background-color</span>:<span class="hljs-number">#333</span>;
    <span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>;
    <span class="hljs-attribute">font-weight</span>:bold;
}
</code></span></pre>


<p>特別な記述をしなくても、簡単にプロパティを継承出来てしまうわけですね。<br />
これは楽です。</p>



<h2 class="wp-block-heading">Mixinで多重継承する</h2>



<p>この仕組みは、OOCSSの様な作り方をCSSだけで行いたい場合に役立ちます。</p>



<h3 class="wp-block-heading">従来の書き方</h3>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-class">.box</span> {
    <span class="hljs-attribute">padding</span>:<span class="hljs-number">10px</span>;
    <span class="hljs-attribute">margin</span>:<span class="hljs-number">10px</span>;
}
<span class="hljs-selector-class">.dark</span> {
    <span class="hljs-attribute">background-color</span>:<span class="hljs-number">#111</span>;
    <span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>;
}
<span class="hljs-selector-class">.rounded-corners</span> {
    <span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">5px</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">5px</span>;
    <span class="hljs-attribute">border-radius</span>:<span class="hljs-number">5px</span>;
}
</code></span></pre>


<p>例えばこのコードで黒くて角丸なボックスを要素に適用したい場合、<br />
次のようなHTMLになると思います。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box dark rounded-corners"</span>&gt;</span>
    ...
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></span></pre>


<h3 class="wp-block-heading">Mixinによる継承</h3>



<p>ここでLessのMixinを使用して、一つ記述を書き足してみます。</p>


<pre class="wp-block-code"><span><code class="hljs">.mybox {
    .box;
    .dark;
    .roudned-corners;
}
</code></span></pre>


<p>こうすることにより、divにあてがうクラスはよりシンプルになります。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mybox"</span>&gt;</span>
    ...
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></span></pre>


<p>オブジェクト指向的なCSSを書きたいけど、HTMLはスッキリさせたい。<br />
そんなケースによさそうです。</p>



<h3 class="wp-block-heading">デメリット</h3>



<p>HTMLはスッキリしますが、出力されるCSSはそうはいきません。<br />
（誰が見るわけでもないかもしれませんが）</p>



<p>呼び出される都度、何度でも展開されるわけなので、<br />
同じ事を繰り返して記述する、いささかしつこいCSSになるでしょう。<br />
些細な差かもしれませんが、ファイルサイズもかさみますね。</p>



<h2 class="wp-block-heading">extendをLESSに実装した例</h2>



<p>昨日、AdventCalender14日目の@hokacchaさんが、<br />
よりスマートなソリューションについて記事にされていたので、是非。</p>



<p><a href="http://d.hatena.ne.jp/hokaccha/20111214/1323821463">LESSにextendを実装してみた &#8211; hokaccha.hamalog v2</a></p>



<p>出力されるCSSが、大変いい感じになりますね。<br />
extend、よさげです。</p>



<p class="caution">
    LESS の1.4系より、extendが公式に導入されています。<br />
    <a href="https://github.com/less/less.js/blob/master/CHANGELOG.md#140-beta-1--2">less.js/CHANGELOG.md at master · less/less.js</a>
</p>



<h2 class="wp-block-heading">オマケ：無限ループの罠</h2>



<p>ちなみに、互いにそれぞれを呼び出すMixinを作ると、<br />
当たり前ですが無限ループに陥ってコンパイラが落ちる事もあると思いますので、<br />
ご注意ください。</p>



<p><em>良い子は真似しちゃだめ！</em></p>


<pre class="wp-block-code"><span><code class="hljs language-php">.hoge {
    color:<span class="hljs-comment">#fff;</span>
    .fuga;
}
.fuga {
    color:<span class="hljs-comment">#ccc;</span>
    .hoge;
}
</code></span></pre>


<h2 class="wp-block-heading">おわり</h2>



<p>それでは引き続き<a href="http://atnd.org/events/21919">Less &amp; Sass Advent calendar 2011</a>をお楽しみくださいませ。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
