-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
143 lines (129 loc) · 3.17 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
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
'use strict';
import config from './config';
import gulp from 'gulp';
import nunjucks from 'gulp-nunjucks';
import sass from 'gulp-sass';
import autoprefixer from 'autoprefixer';
import sourcemaps from 'gulp-sourcemaps';
import postcss from 'gulp-postcss';
import cleanCSS from 'gulp-clean-css';
import webpack from 'webpack';
import browsersync from 'browser-sync';
import runSequence from 'run-sequence';
import watch from 'gulp-watch';
import del from 'del';
import rename from 'gulp-rename';
import webpackConfig from './webpack.config';
/**
* Task: delete dist
*/
gulp.task('del', () => del.sync(config.dist));
/**
* Task: nunjucks to html
*/
gulp.task('html', () =>
gulp.src(config.html.src)
.pipe(nunjucks.compile({ baseUrl: config.baseUrl, saveUrl: config.saveUrl }))
.pipe(rename('index.html'))
.pipe(gulp.dest(config.html.dist))
.pipe(browsersync.stream()));
/**
* Task: scss to css
*/
gulp.task('css', () =>
gulp.src(config.css.src)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss([autoprefixer()]))
.pipe(cleanCSS())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.css.dist))
.pipe(browsersync.stream())
);
/**
* Task: bundle js
*/
gulp.task('js:bundle', cb => {
let setup = false;
webpack(webpackConfig, (err, stats) => {
if(err) return console.log(err);
browsersync.reload();
if (!setup) {
cb();
setup = true;
}
});
});
/**
* Task: copy vendor js
*/
gulp.task('js:vendor', () =>
gulp.src(config.js.src.vendor)
.pipe(gulp.dest(config.js.dist))
);
/**
* Task: copy model files
*/
gulp.task('models', () =>
gulp.src(config.models.src)
.pipe(gulp.dest(config.models.dist))
.pipe(browsersync.stream())
);
/**
* Task: copy images
*/
gulp.task('images', () =>
gulp.src(config.images.src)
.pipe(gulp.dest(config.images.dist))
.pipe(browsersync.stream())
);
/**
* Task: copy audio
*/
gulp.task('audio', () =>
gulp.src(config.audio.src)
.pipe(gulp.dest(config.audio.dist))
.pipe(browsersync.stream())
);
/**
* Task: watch
*/
gulp.task('watch', cb => {
watch(config.css.watch, () => gulp.start('css'));
watch(config.html.watch, () => gulp.start('html'));
watch(config.js.src.vendor, () => gulp.start('js:vendor'));
watch(config.models.src, () => gulp.start('models'));
watch(config.audio.src, () => gulp.start('audio'));
watch(config.images.src, () => gulp.start('images'));
cb();
});
/**
* Task: start browsersync
*/
gulp.task('browsersync', () =>{
browsersync.init({
server: {
baseDir: config.dist
},
ui: false,
open: false
});
});
gulp.task('dist', cb => {
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({ sourceMap: true }));
runSequence(
'del',
['html', 'css', 'js:bundle', 'js:vendor', 'models', 'images', 'audio'],
cb
);
});
gulp.task('dev', cb => {
webpackConfig.watch = true;
runSequence(
'del',
['html', 'css', 'js:bundle', 'js:vendor', 'models', 'images', 'audio'],
'watch',
'browsersync',
cb
);
});