LoginSignup
14
13

More than 5 years have passed since last update.

gulp-ejsで拡張子がejsになってしまう問題(.htmlにしたい)

Last updated at Posted at 2016-07-28

どのバージョンからか知らないけど拡張子がejsになってしまう!

index.ejs なんてのをコンパイルすると index.ejsのままなんですねぇ。
はい。答えは簡単renameしてしまえば良いのです。

.pipe(rename({extname: '.html'}))

※上の方で rename = require('gulp-rename')するのを忘れないように。
  
こんな感じ。

gulp.task("ejs", function () {
    gulp.src(
        [
            assetsPath + '/ejs/*.ejs', //入力ソースパターン&path
            '!' + assetsPath + "/ejs/_*.ejs" //入力除外ソースパターン&path
        ]
    )
        .pipe(ejs())
        .pipe(rename({extname: '.html'})) //出力ファイル名パターン
        .pipe(gulp.dest(publicPath + '/html')); //出力path
});

そうすると
index.html
が出来上がります。

応用

というわけで。お気づきの方もおられるかもしれませんが
何も .htmlにこだわる必要ないんですねぇ。別に拡張子jsでもphpでもなんでもござれ。

番外編

.pipe(rename({extname: '.html'})) //出力ファイル名パターン

上でこんなのやってますが、 extnameの部分をsuffixにしてminとかつけると。。。

gulp.task("ejs", function () {
    gulp.src(
        [
            assetsPath + '/ejs/*.ejs', //入力ソースパターン&path
            '!' + assetsPath + "/ejs/_*.ejs" //入力除外ソースパターン&path
        ]
    )
        .pipe(ejs())
        .pipe(rename({suffix: '.min'}))
        .pipe(rename({extname: '.html'})) //出力ファイル名パターン
        .pipe(gulp.dest(publicPath + '/html')); //出力path
});

index.min.html
みたいになります。(やる人いるのかわからないけど。)

因みに.jsも読み込むこと出来ます。

.ejsファイルの中で

<script><% include ../js/min/index.min.js %></script>

こんな感じ。
そうすると、圧縮したjsの中身だけを吸い取ってhtmlに表示したりとかもできますね。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13