Skip to content

Instantly share code, notes, and snippets.

@musichook
Last active November 27, 2021 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musichook/0f41f8232a484440e4baa823788c1195 to your computer and use it in GitHub Desktop.
Save musichook/0f41f8232a484440e4baa823788c1195 to your computer and use it in GitHub Desktop.
Using environments with Gulp example

Gulp

Environment (.env file)

Configuration

Using

var gulp = require('gulp');
var _if = require('gulp-if');
// опущу подключение остальных плагинов

var production = process.env.NODE_ENV === 'production';

gulp.task('less', function () {
    gulp.src('src/less/**/*.less')
        .pipe(plumber()) // не даем ошибкам всплывать
        .pipe(_if(!production, sourcemaps.init())) // для сорсмепов в дев-режиме
        .pipe(less()) // компилируем less
        .pipe(autoprefixer()) // расставляем префиксы для браузеров
        .pipe(_if(production, csso())) // в продакшене жмем
        .pipe(_if(!production, sourcemaps.write())) // пишем сормепы в дев-режиме
        .pipe(gulp.dest('public/css')) // пишем на диск
        .pipe(_if(production, gzip())) // если продакшен - жмем gzip
        .pipe(_if(production, gulp.dest('public/css'))) // и тоже записываем на диск
});

gulp.task('default', ['less'], function () {
    gulp.watch('src/less/**/*.less', ['less']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment