LoginSignup
8
8

More than 5 years have passed since last update.

gulpを使ってNODE_ENVで出力を分ける

Posted at

developmentの時だけ必要なものとか、productionの時だけ必要なものとかあったりするのはよくあること。

NODE_ENVの内容によって特定の場所の出力内容を変える、というのを試してみた。

準備

gulp-preprocessのインストール

gulp-preprocessを使うのでインストールする。

$ npm install --save-dev gulp-preprocess

gulpfileの編集

gulpfileでは、preprocessを適当なタイミングで読んであげるようにする。

gulpfile.coffee
preprocess = require 'gulp-preprocess'

gulp.task 'coffee', () ->
  gulp.src 'src/js/**/*.coffee'
    .pipe preprocess()
    .pipe coffee()
    .pipe gulp.dest('dist/js')

使う

コードにNODE_ENVごとの処理を書く

基本的に@if@endifで囲む。

@ifの後に分岐の判定を書くんだけど、==じゃなくて=なのが注意。

hello_world.coffee
console.log 'Hello World.'
# @if NODE_ENV='development'
console.log 'I am debug mode.'
# @endif

タスクの実行

あとはgulpでタスクを実行するときにNODE_ENVを指定すれば、その時々で条件ごとに出力内容が変わる。

$ NODE_ENV=production gulp coffee
8
8
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
8
8