dayjournal memo

Total 974 articles!!

gulp #002 – gulpでファイルコピー

Yasunori Kirimoto's avatar

今回は、「gulp」でファイルをコピーする方法を試したいと思います。

node.js_016_02


まず、サンプルとしてsampleフォルダに「index.html」を用意します。 node.js_017_01


index.html


<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <title>sample</title>
</head>

<body>

    <p>サンプル!</p>

</body>

</html>

「gulpfile.js」に下記コードを記述します。

gulpfile.js


var gulp = require('gulp');

gulp.task('sample', function() {
    gulp.src('./sample/index.html')
        .pipe(gulp.dest('./dist'));
});

gulp.task('default', ['sample'])

作業フォルダでgulpを実行すると「dist」フォルダが作成され「index.html」がコピーされます。


gulp

node.js_017_03


node.js_017_02


コピーするファイルを指定:


gulp.src('sample/index.html')

ファイルのコピー先を指定:


.pipe(gulp.dest('./dist'))


ワイルドカードも利用できるため一括指定や任意指定も可能です。

全てのファイルを指定:


gulp.src('./sample/*')

任意の拡張子ファイルを指定:


gulp.src('./sample/*.html')

任意のフォルダのファイルを指定:


gulp.src('./**/*.html')

任意のファイルのコピーを除外:


gulp.src(['./sample/*.html', '!./sample/sample.html'])

必要なファイルだけを自動コピーして環境を構築できるため便利です。



book

Q&A