LoginSignup
5
4

More than 5 years have passed since last update.

gulpでES6をコンパイルする時にNODE_ENVを埋める

Last updated at Posted at 2016-02-27

gulp-babel 6.1.2で以下のようにES6をコンパイルしているとする。

var gulp  = require('gulp');
var babel = require('gulp-babel');

gulp.task(
  'compile-es6',
  function () {
    return gulp.src('src/**/*.js')
      .pipe(babel({ presets: ['es2015'] }))
      .pipe(gulp.dest('app/'));
  }
);

babel-plugin-transform-inline-environment-variablesをインストールして以下のようにプラグインを指定し、

gulp.task(
  'compile-es6',
  function () {
    return gulp.src('src/**/*.js')
      .pipe(babel({ presets: ['es2015'], plugins: ['transform-inline-environment-variables'] }))
      .pipe(gulp.dest('app/'));
  }
);

NODE_ENV=production gulp compileとかすれば、JavaScript中のprocess.env.NODE_ENVがインラインで'production'に置き換わってくれる。Electronのパッケージングとかでコンパイル時のNODE_ENVにより処理を分岐したい時とかに便利。

5
4
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
5
4