Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab802aa

Browse files
committed
feat(gulp): 書き方の例を追加
1 parent4d9e265 commitab802aa

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

‎SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
*[jQuery](ja/jQuery/README.md)
66
*[ESLint](ja/ESLint/README.md)
77
*[Connect](ja/connect/README.md)
8+
*[gulp](ja/gulp/README.md)
89

‎ja/gulp/README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,70 @@
1111
処理をStreamでつなげる(`pipe`)することができ、複数の処理を一時ファイルなしでできるようになっています。
1212

1313
それぞれの処理はgulpのプラグインという形でモジュール化されているため、
14-
利用者はモジュールを読み込み、`pipe()`することでタスクを定義するという使い方ができるツールです
14+
利用者はモジュールを読み込み、`pipe()`で繋ぐだけでタスクの定義ができるツールです
1515

1616
##どう書ける?
1717

18+
例えば、[Sass](http://sass-lang.com/"Sass")で書いたファイルを次のように処理したいとします。
19+
20+
1.`sass/*.scss`のファイルを読み込む
21+
2. 読み込んだsassファイルを`sass`でコンパイル
22+
3. CSSとなったファイルに`autoprefixture`で接頭辞をつける
23+
4. CSSファイルをそれぞれ`minify`で圧縮する
24+
5. 圧縮したCSSファイルをそれぞれ`css`ディレクトリに出力する
25+
26+
この一連の処理は以下のようなタスクとして定義することができます。
27+
28+
```js
29+
importgulpfrom"gulp";
30+
importsassfrom"gulp-sass";
31+
importautoprefixerfrom"gulp-autoprefixer";
32+
importminifyfrom"gulp-minify-css";
33+
34+
gulp.task('sass',function() {
35+
returngulp.src('sass/*.scss')
36+
.pipe(sass())
37+
.pipe(autoprefixer())
38+
.pipe(minify())
39+
.pipe(gulp.dest('css'));
40+
});
41+
```
42+
43+
ここでは、gulpプラグインの仕組みについて扱うので、gulpの使い方については詳しくは以下を参照して下さい。
44+
45+
-[gulp/docs at master · gulpjs/gulp](https://github.com/gulpjs/gulp/tree/master/docs)
46+
-[現場で使えるgulp入門 - gulpとは何か | CodeGrid](https://app.codegrid.net/entry/gulp-1)
47+
-[gulp入門 (全12回) - プログラミングならドットインストール](http://dotinstall.com/lessons/basic_gulp)
48+
49+
##どういう仕組み?
50+
51+
実際にgulpプラグインを書きながら、どのような仕組みで処理同士が連携を取って動作しているのかを見ていきましょう。
52+
53+
先ほどのgulpのタスクの例では、既にモジュール化された処理を`pipe`で繋げただけであるため、
54+
それぞれの処理がどのように実装されているかはよく分かりませんでした。
55+
56+
ここでは`gulp-prefixer`という、それぞれのファイルに対して先頭に特定の文字列を追加するgulpプラグインを書いていきます。
57+
58+
同様の名前のプラグインが公式のドキュメントで「プラグインの書き方」の例として紹介されているので合わせてみると良いでしょう。
59+
60+
-[gulp/docs/writing-a-plugin at master · gulpjs/gulp](https://github.com/gulpjs/gulp/tree/master/docs/writing-a-plugin"gulp/docs/writing-a-plugin at master · gulpjs/gulp")
61+
-[gulp/dealing-with-streams.md at master · gulpjs/gulp](https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/dealing-with-streams.md"gulp/dealing-with-streams.md at master · gulpjs/gulp")
62+
63+
gulpプラグインは一般的にオプションを受け取り、NodeのStreamを返す関数として実装されます。
64+
65+
[import gulp-prefixer.js](../../src/gulp/gulp-prefixer.js)
66+
67+
ここで実装した`gulp-prefixer`は、gulpのタスクで次のように書くことで利用できます。
68+
69+
[import gulpfile.babel.js](../../src/gulp/gulpfile.babel.js)
70+
71+
この`default`タスクが実行されると次のような処理が行われます。
72+
73+
1.`./*.*`にマッチするファイルを取得(全てのファイル)
74+
2. 取得したファイルの先頭に"prefix text"という文字列を追加する
75+
3. 変更したされたファイルを`build/`ディレクトリに出力する
1876

1977

20-
-[ ] どういう仕組み?
2178
-[ ] どういう用途に向いている?
2279
-[ ] どういう用途に向いていない?
2380
-[ ] この仕組みを使っているもの

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp