gulpfile.js

스크립트 minification 을 위해 자주 사용하는 gulpfile.js 템플릿 코드

const gulp = require('gulp')
    , uglify = require('gulp-uglify')
    , uglifycss = require('gulp-uglifycss')
    , rename = require('gulp-rename')
    , sourcemaps = require('gulp-sourcemaps')
    , clean = require('gulp-clean')
;

gulp.task('min-js', function () {
    return gulp
        .src([
            'assets/js/**/*.js',
            '!assets/js/**/*.min.js',
            '!assets/js/vendor/*.js',
        ])
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(rename(function (path) {
            path.extname = '.min.js';
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('assets/js'));
});

gulp.task('min-css', function () {
    return gulp
        .src([
            'assets/css/**/*.css',
            '!assets/css/**/*.min.css',
            '!assets/css/vendor/*.css',
        ])
        .pipe(uglifycss())
        .pipe(rename(function (path) {
            path.extname = '.min.css';
        }))
        .pipe(gulp.dest('assets/css'));
});

gulp.task('clean', function () {
    return gulp
        .src([
            'assets/**/*.min.{js,css}',
            '!assets/css/vendor/*.css',
            '!assets/js/vendor/*.js',
        ], {read: false})
        .pipe(clean())
});


gulp.task('watch', function () {
    gulp.watch([
        'assets/js/**/*.js',
        '!assets/js/**/*.min.js',
        '!assets/js/vendor/*.js',
    ], gulp.parallel(['min-js']));

    gulp.watch([
        'assets/css/**/*.css',
        '!assets/css/**/*.min.css',
        '!assets/css/vendor/*.css',
    ], gulp.parallel(['min-css']));
});

gulp.task('default', gulp.series('clean', 'min-js', 'min-css', 'watch'));
gulp.task('build', gulp.series('clean', 'min-js', 'min-css'));