LoginSignup
11
10

More than 5 years have passed since last update.

gulp-convert-encodingで文字コードを指定

Posted at

gulp-slimやgulp-jadeでhtmlを書いてコンパイルして、ふと日本語を混ぜた時に困った。Macなら問題ないんだけど、Windowsでは文字コードがSJISになってしまう…

エンコードをコンパイル後に直してくれるものがあると…ってありました。gulp-convert-encodingです!

使い方

では、gulp-slimでコンパイルして、その後エンコードをUTF-8にするという例を書きましょう。

gulpfile.js
var gulp = require('gulp'),
    plumber = require('gulp-plumber'),
    slim = require('gulp-slim'),
    convertEncoding = require('gulp-convert-encoding');

gulp.task('slim', function () {
  gulp.src(["src/**/*.slim"])
    .pipe(plumber())
    .pipe(slim({
      pretty: true
    }))
    .pipe(convertEncoding({to: "utf-8"}))
    .pipe(gulp.dest('./dist'));
});

必要なものは、npm installなどでインストールして、最初に読み込みます。slim用のタスクでpipe(slim())の部分でコンパイルして、次のpipe(convertEncoding())の部分で文字コードを変更します。UTF-8にしたいので、{to: "utf-8"}というオプションを書く。こうしたら、日本語が含まれていても文字化けせずに済むでしょう。

元のslimファイルもUTF-8でないと、コンパイル自体ができないので、ご注意ください。
(コンパイル前にもエンコードを変えるとかして対処可能ではあるのだろうけど…)

11
10
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
11
10