-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
105 lines (93 loc) · 2.54 KB
/
gulpfile.babel.js
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
'use strict';
const { src, dest, watch, series } = require('gulp'),
babel = require('gulp-babel'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer'),
sass = require('gulp-sass'),
gnf = require('gulp-npm-files'),
sourcemaps = require('gulp-sourcemaps'),
pug = require('gulp-pug'),
browserSync = require('browser-sync').create();
/**
* Transpile ecmascript ES6 to ES5 with babel
*/
const javascript = () => {
return src('src/js/**/*.js', { sourcemaps: true })
.pipe(sourcemaps.init())
.pipe(babel({
presets: [
['@babel/env', {
modules: false
}]
]
}))
.pipe(rename({ extname: '.min.js' }))
.pipe(uglify().on('error', (e) => console.log(e)))
.pipe(sourcemaps.write('.'))
.pipe(dest('build/assets/js'));
}
/**
* Process scss files to css.min
*/
const scss = () => {
const plugins = [
autoprefixer({ overrideBrowserslist: ['last 10 version'] })
];
return src('./src/scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(rename({ extname: '.min.css' }))
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(sourcemaps.write('.'))
.pipe(dest('build/assets/css'))
.pipe(browserSync.stream());
};
/**
* Process PUG files to html
*/
const views = () => {
return src('./src/pug/**/*.pug')
.pipe(pug({
doctype: 'html',
pretty: ' '
}))
.pipe(dest('build'));
};
/**
* Copy production node modules to build
*/
const copynm = () => {
return src(gnf(null, './package.json'), { base: './' })
.pipe(dest('build/assets'));
};
/**
* Watch for all changes in SRC
*/
const live = () => {
browserSync.init({
server: {
baseDir: "./build/"
},
port: 2018,
notify: false
});
watch(['scss/**/*.scss'], { cwd: './src/' }, scss);
watch(['js/**/*.js'], { cwd: './src/' }, javascript);
watch(['pug/**/*.pug'], { cwd: './src/' }, views);
const buildWatcher = watch(['build/**/*.html', 'build/assets/**/*.js']);
buildWatcher.on('all', (stats, file) => {
console.log(`Reloading server for ${stats} on ${file}`);
browserSync.stream();
browserSync.reload();
});
}
exports.javascript = javascript;
exports.scss = scss;
exports.copynm = copynm;
exports.views = views;
exports.default = series(views, scss, javascript, live);
exports.build = series(copynm, views, scss, javascript);