aboutsummaryrefslogtreecommitdiff
path: root/gulpfile.js
blob: 4aad2db7de8e60bd10d84571dfd376ae1bd6460a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';

/* paths to source files (src), to ready files (build), as well as to those whose changes need to be monitored (watch) */
var path = {
    build: {
        html: 'assets/build/',
        js: 'assets/build/js/',
        wasm: 'assets/build/js/',
        css: 'assets/build/css/',
        img: 'assets/build/img/',
        fonts: 'assets/build/fonts/'
    },
    src: {
        html: 'assets/src/*.html',
        js: 'assets/src/js/*.js',
        style: 'assets/src/style/main.scss',
        img: 'assets/src/img/**/*.*',
        fonts: 'assets/src/fonts/**/*.*',
        wasm: 'assets/src/js/*.*'
    },
    watch: {
        html: 'assets/src/**/*.html',
        js: 'assets/src/js/**/*.js',
        css: 'assets/src/style/**/*.scss',
        img: 'assets/src/img/**/*.*',
        fonts: 'assets/srs/fonts/**/*.*'
    },
    clean: './assets/build/*'
};

/* настройки сервера */
var config = {
    server: {
        baseDir: './assets/build'
    },
    notify: false
};

/* include gulp and plugins */
var gulp = require('gulp'),  // include Gulp
    webserver = require('browser-sync'), // server for work and automatic page updates
    plumber = require('gulp-plumber'), // bug tracking module
    rigger = require('gulp-rigger'), // a module to import the contents of one file into another
    sourcemaps = require('gulp-sourcemaps'), // module for generating a map of source files
    sass = require('gulp-sass'), // module for compiling SASS (SCSS) to CSS
    autoprefixer = require('gulp-autoprefixer'), // module for automatic installation of auto-prefixes
    cleanCSS = require('gulp-clean-css'), // CSS minification plugin
    uglify = require('gulp-uglify'), // JavaScript minification module
    cache = require('gulp-cache'), // module for caching
    imagemin = require('gulp-imagemin'), // plugin for compressing PNG, JPEG, GIF and SVG images
    jpegrecompress = require('imagemin-jpeg-recompress'), // jpeg compression plugin
    pngquant = require('imagemin-pngquant'), // png compression plugin
    del = require('del'), // plugin for deleting files and directories
    rename = require('gulp-rename');

/* tasks */

// start the server
gulp.task('webserver', function () {
    webserver(config);
});

// compile html
gulp.task('html:build', function () {
    return gulp.src(path.src.html) // selection of all html files in the specified path
        .pipe(plumber()) // error tracking
        .pipe(rigger()) // attachment import
        .pipe(gulp.dest(path.build.html)) // uploading ready files
        .pipe(webserver.reload({ stream: true })); // server reboot
});

// compile styles
gulp.task('css:build', function () {
    return gulp.src(path.src.style) // get main.scss
        .pipe(plumber()) // for bug tracking
        .pipe(sourcemaps.init()) // initialize sourcemap
        .pipe(sass()) // scss -> css
        .pipe(autoprefixer()) // add prefix
        .pipe(gulp.dest(path.build.css))
        .pipe(rename({ suffix: '.min' }))
        .pipe(cleanCSS()) // minimize CSS
        .pipe(sourcemaps.write('./')) // write sourcemap
        .pipe(gulp.dest(path.build.css)) // output to build
        .pipe(webserver.reload({ stream: true })); // server restart
});

// compile js
gulp.task('js:build', function () {
    return gulp.src(path.src.js) // get file main.js
        .pipe(plumber()) // for bug tracking
        .pipe(rigger()) // import all files to main.js
        .pipe(gulp.dest(path.build.js))
        .pipe(rename({ suffix: '.min' }))
        .pipe(sourcemaps.init()) //initialize sourcemap
        .pipe(uglify()) // minimize js
        .pipe(sourcemaps.write('./')) //  write sourcemap
        .pipe(gulp.dest(path.build.js)) // put ready file
        .pipe(webserver.reload({ stream: true })); // server restart
});

gulp.task('wasm:build', function () {
    return gulp.src(path.src.wasm)
    .pipe(gulp.dest(path.build.wasm))
    .pipe(webserver.reload({stream: true}))
});

// move fonts
gulp.task('fonts:build', function () {
    return gulp.src(path.src.fonts)
        .pipe(gulp.dest(path.build.fonts));
});

// image processing
gulp.task('image:build', function () {
    return gulp.src(path.src.img) // path to image source
        .pipe(cache(imagemin([ // image compression
            imagemin.gifsicle({ interlaced: true }),
            jpegrecompress({
                progressive: true,
                max: 90,
                min: 80
            }),
            pngquant(),
            imagemin.svgo({ plugins: [{ removeViewBox: false }] })
        ])))
        .pipe(gulp.dest(path.build.img)); // output ready files
});

// remove catalog build
gulp.task('clean:build', function () {
    return del(path.clean);
});

// clear cache
gulp.task('cache:clear', function () {
    cache.clearAll();
});

// assembly
gulp.task('build',
    gulp.series('clean:build',
        gulp.parallel(
            'html:build',
            'css:build',
            'js:build',
            'fonts:build',
            'image:build',
            'wasm:build'
        )
    )
);

// launching tasks when files change
gulp.task('watch', function () {
    gulp.watch(path.watch.html, gulp.series('html:build'));
    gulp.watch(path.watch.css, gulp.series('css:build'));
    gulp.watch(path.watch.js, gulp.series('js:build'));
    gulp.watch(path.watch.img, gulp.series('image:build'));
    gulp.watch(path.watch.fonts, gulp.series('fonts:build'));
});

// default tasks
gulp.task('default', gulp.series(
    'build',
    gulp.parallel('webserver','watch')
));