Skip to content

Commit f6c72c7

Browse files
committed
update build system with gulp4
1 parent ac223d0 commit f6c72c7

File tree

8 files changed

+130
-108
lines changed

8 files changed

+130
-108
lines changed

build-system/build.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const $ = require('./util');
55
const webpack = require('webpack');
66
const webpackConfig = require('../webpack.config.js');
7-
const runSequence = require('run-sequence');
87
const env = process.env.NODE_ENV;
98

10-
gulp.task('build:app-js', 'Builds the app scripts.', () => {
9+
// Builds the app scripts.
10+
gulp.task('build:app-js', () => {
1111
return new Promise(resolve => webpack(webpackConfig[env === 'production' ? 'prod' : 'dev'], (err, stats) => {
1212
if (err) throw new $.util.PluginError('webpack', err);
1313
let errorStats = stats.toString('errors-only');
@@ -16,8 +16,9 @@ gulp.task('build:app-js', 'Builds the app scripts.', () => {
1616
}));
1717
});
1818

19-
gulp.task('build:app-css', 'Builds the app style.', cb => {
20-
gulp.src(['./styles/**/*.scss'], {buffer: true})
19+
// Builds the app style.
20+
gulp.task('build:app-css', cb => {
21+
gulp.src(['./styles/**/*.scss'], { buffer: true })
2122
.pipe($.sass({
2223
outputStyle: 'expanded',
2324
sourceMap: 'app.css.map',
@@ -35,30 +36,29 @@ gulp.task('build:app-css', 'Builds the app style.', cb => {
3536
});
3637
});
3738

38-
gulp.task('build:lib-js', 'Builds the lib scripts.', () => {
39+
// Builds the lib scripts.
40+
gulp.task('build:lib-js', () => {
3941
return gulp.src(require('../lib.config').js)
4042
.pipe($.concat('lib.js'))
4143
.pipe(gulp.dest('./app/js/'));
4244
});
4345

44-
gulp.task('build:lib-css', 'Builds the lib style.', () => {
46+
// Builds the lib style.
47+
gulp.task('build:lib-css', () => {
4548
return gulp.src(require('../lib.config').css)
4649
.pipe($.concat('lib.css'))
4750
.pipe($.replace('@import url(https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic&subset=latin);', ''))
4851
.pipe(gulp.dest('./app/css/'));
4952
});
5053

51-
gulp.task('build:images', 'Builds the app style.', () => {
54+
// Builds the app style.
55+
gulp.task('build:images', () => {
5256
return gulp.src('./assets/images/**/*.*')
5357
.pipe(gulp.dest('./app/images/'));
5458
});
5559

56-
gulp.task('build:fonts', 'Builds the app fonts.', () => {
57-
return gulp.src('./assets/fonts/**/*.*')
58-
.pipe(gulp.dest('./app/fonts/'));
59-
});
60-
61-
gulp.task('build:extra', 'Builds extra files.', () => {
60+
// Builds extra files.
61+
gulp.task('build:extra', () => {
6262
return Promise.all(require('../lib.config').extra.map(key => {
6363
const dest = Object.keys(key);
6464
const path = key[dest];
@@ -67,14 +67,5 @@ gulp.task('build:extra', 'Builds extra files.', () => {
6767
}));
6868
});
6969

70-
gulp.task('build', 'Builds the app.', cb => {
71-
runSequence([
72-
'build:extra',
73-
'build:fonts',
74-
'build:images',
75-
'build:app-js',
76-
'build:lib-css',
77-
'build:app-css',
78-
'build:lib-js'
79-
], cb);
80-
});
70+
// Builds the app.
71+
gulp.task('build', gulp.parallel('build:extra', 'build:images', 'build:app-js', 'build:lib-css', 'build:app-css', 'build:lib-js'));

build-system/clean.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const del = require('del');
5-
const runSequence = require('run-sequence');
65

7-
gulp.task('clean:dist', 'Cleans dist files.', () => {
8-
return del(['./dist/**'], {force: true});
6+
// Cleans dist files.
7+
gulp.task('clean:dist', () => {
8+
return del(['./dist/**'], { force: true });
99
});
1010

11-
gulp.task('clean', 'Cleans files.', cb => {
12-
runSequence('clean:dist', cb);
13-
});
11+
// Cleans files.
12+
gulp.task('clean', gulp.series('clean:dist'));

build-system/default.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const $ = require('./util');
5-
const runSequence = require('run-sequence');
65

7-
function watch(files, cb) {
8-
return gulp.watch(files, function (event) {
9-
$.util.log($.util.colors.bold('File ' + event.path + ' was ' + event.type + ', running tasks...'));
10-
cb();
6+
function watch(files, task) {
7+
const watcher = gulp.watch(files, task);
8+
['add', 'addDir', 'change', 'unlink', 'unlinkDir'].forEach(type => {
9+
watcher.on(type, (path, stats) => {
10+
$.util.log($.util.colors.bold('File ' + path + ' was ' + type + ', running tasks...'));
11+
});
1112
});
1213
}
1314

14-
gulp.task('watch', 'Watches for changes in files.',
15-
() => {
16-
watch(['lib.config.js'], () => runSequence('lint', 'build:extra', 'build:lib-js', 'build:lib-css'));
17-
watch(['styles/**/*.*'], () => runSequence('lint', 'build:app-css'));
18-
});
15+
// Watches for changes in files.
16+
gulp.task('watch', cb => {
17+
watch(['lib.config.js'], gulp.series('lint', 'build:extra', 'build:lib-js', 'build:lib-css'));
18+
watch(['styles/**/*.*'], gulp.series('lint', 'build:app-css'));
19+
cb();
20+
});

build-system/dist.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
11
'use strict';
2-
const gulp = require('gulp-help')(require('gulp'));
2+
3+
const gulp = require('gulp');
34
const $ = require('./util');
45
const pngquant = require('imagemin-pngquant');
5-
const runSequence = require('run-sequence');
66
const cachebust = $.cachebust();
77
const through2 = require('through2');
88

99
const distPath = './dist';
1010
const buildVersion = (new Date()).toISOString();
1111

12-
gulp.task('dist:all', 'Copy all to dist.', () => {
12+
// Copy all to dist.
13+
gulp.task('dist:all', () => {
1314
return gulp.src(['./app/**/**'])
1415
.pipe(gulp.dest(distPath))
15-
.pipe($.size({title: 'dist:all'}));
16+
.pipe($.size({ title: 'dist:all' }));
1617
});
1718

18-
gulp.task('dist:images', 'Compress images to dist.', () => {
19+
// Compress images to dist.
20+
gulp.task('dist:images', () => {
1921
return gulp.src(['./app/images/**/*'])
2022
.pipe($.imagemin({
2123
use: [pngquant()]
2224
}))
2325
.pipe(cachebust.resources())
2426
.pipe(gulp.dest(distPath + '/images/'))
25-
.pipe($.size({title: 'dist:images'}));
27+
.pipe($.size({ title: 'dist:images' }));
2628
});
2729

28-
gulp.task('dist:css', 'Compress css to dist.', () => {
30+
// Compress css to dist.
31+
gulp.task('dist:css', () => {
2932
return gulp.src('./app/css/**/*.css')
3033
.pipe(cachebust.references())
31-
.pipe($.csso({comments: false}))
34+
.pipe($.csso({ comments: false }))
3235
.pipe(cachebust.resources())
3336
.pipe(gulp.dest(distPath + '/css'))
34-
.pipe($.size({title: 'dist:css'}));
37+
.pipe($.size({ title: 'dist:css' }));
3538
});
3639

37-
gulp.task('dist:js', 'Compress js to dist.', () => {
40+
// Compress js to dist.
41+
gulp.task('dist:js', () => {
3842
return gulp.src(['./app/js/*.js'])
3943
.pipe(cachebust.references())
4044
.pipe($.minify())
4145
.pipe(cachebust.resources())
4246
.pipe(gulp.dest(distPath + '/js/'))
43-
.pipe($.size({title: 'dist:js'}));
47+
.pipe($.size({ title: 'dist:js' }));
4448
});
4549

46-
gulp.task('dist:html', 'Compress html to dist.', () => {
50+
// Compress html to dist.
51+
gulp.task('dist:html', () => {
4752
return gulp.src(['./app/*.html'])
4853
.pipe(cachebust.references())
49-
.pipe($.htmlmin({collapseWhitespace: true, minifyCSS: true, minifyJS: true}))
54+
.pipe($.htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true }))
5055
.pipe(gulp.dest(distPath))
51-
.pipe($.size({title: 'dist:html'}));
56+
.pipe($.size({ title: 'dist:html' }));
5257
});
5358

54-
5559
//generate service workers
56-
gulp.task('dist:serviceworkers', function (cb) {
60+
gulp.task('dist:serviceworkers', cb => {
5761
const swConfig = require('../lib.config').serviceWorker;
5862
const rootPath = __dirname.replace('build-system', '') + 'dist/';
5963
let resources = ['"./"'];
6064
gulp.src([distPath + '/**/*.*'])
6165
.pipe(through2.obj(function (file, enc, next) {
62-
!/sw\.js|\.html|\.map/.test(file.path) && this.push('"' + file.path.replace(rootPath,'') + '"');
66+
!/sw\.js|\.html|\.map/.test(file.path) && this.push('"' + file.path.replace(rootPath, '') + '"');
6367
next();
6468
}))
65-
.on('data', function (data) {
69+
.on('data', data => {
6670
resources.push(data)
6771
})
6872
.on('end', function () {
@@ -79,7 +83,5 @@ gulp.task('dist:serviceworkers', function (cb) {
7983
});
8084
});
8185

82-
83-
gulp.task('dist', 'Dist the app.', cb => {
84-
runSequence('clean:dist', 'dist:all', 'dist:images', 'dist:css', 'dist:js', 'dist:html', 'dist:serviceworkers', cb);
85-
});
86+
// Dist the app.
87+
gulp.task('dist', gulp.series('clean:dist', 'dist:all', 'dist:images', 'dist:css', 'dist:js', 'dist:html', 'dist:serviceworkers'));

build-system/lint.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const $ = require('./util');
55

6-
gulp.task('lint', 'Lint JS files.', () => {
6+
// Lint JS files.
7+
gulp.task('lint', () => {
78
return gulp.src(['gulpfile.js', 'build-system/**/*.js', 'src/**/*.js*', '!src/vendors/**/**.*', '!src/sw.js'])
89
.pipe($.eslint())
910
.pipe($.eslint.format());

build-system/server.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const browserSync = require('browser-sync').create();
55
const $ = require('./util');
66
const serveIndex = require('serve-index');
@@ -9,8 +9,8 @@ const webpackHotMiddleware = require('webpack-hot-middleware');
99
const webpack = require('webpack');
1010
const webpackConfig = require('../webpack.config.js');
1111

12-
// start server with browserSync
13-
gulp.task('server', 'Starts a HTTP(s) server for debug.', () => {
12+
// Starts a HTTP(s) server for debug.
13+
gulp.task('server', () => {
1414
return new Promise(resolve => {
1515
const compiler = webpack(webpackConfig.dev, () => {
1616
const config = {
@@ -31,7 +31,7 @@ gulp.task('server', 'Starts a HTTP(s) server for debug.', () => {
3131
serveIndex('.'),
3232
webpackDevMiddleware(compiler, {
3333
publicPath: webpackConfig.dev.output.publicPath,
34-
stats: {colors: true},
34+
stats: { colors: true },
3535
writeToDisk: true,
3636
logTime: true,
3737
logLevel: 'error'

gulpfile.js

+40-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
'use strict';
22

3-
const gulp = require('gulp-help')(require('gulp'));
3+
const gulp = require('gulp');
44
const $ = require('./build-system/util');
5-
const runSequence = require('run-sequence');
65
const requireDir = require('require-dir');
76
requireDir('./build-system');
87

8+
gulp.task('help', cb => {
9+
$.util.log(
10+
`
11+
Usage
12+
gulp [TASK] [OPTIONS...]
13+
14+
Available tasks
15+
build Builds the app.
16+
build:app-css Builds the app style.
17+
build:app-js Builds the app scripts.
18+
build:extra Builds extra files.
19+
build:images Builds the app style.
20+
build:lib-css Builds the lib style.
21+
build:lib-js Builds the lib scripts.
22+
clean Cleans files.
23+
clean:dist Cleans dist files.
24+
default
25+
dist Dist the app.
26+
dist:all Copy all to dist.
27+
dist:css Compress css to dist.
28+
dist:html Compress html to dist.
29+
dist:images Compress images to dist.
30+
dist:js Compress js to dist.
31+
help Display this help text.
32+
lint Lint JS files.
33+
server Starts a HTTP(s) server for debug.
34+
watch Watches for changes in files.
35+
`
36+
);
37+
cb();
38+
});
39+
940
// Run tasks: lint, build, docs, watch, server
10-
gulp.task('default', (cb) => {
41+
gulp.task('default', cb => {
1142
$.util.log(
1243
$.util.colors.green('Building and watching for changes ...')
1344
);
14-
runSequence(
45+
gulp.series(
1546
'lint',
16-
'build:fonts', 'build:extra', 'build:images', 'build:app-css', 'build:lib-js', 'build:lib-css',
17-
'watch', 'server', () => {
18-
cb();
19-
$.util.log(
20-
$.util.colors.green('Ready! Run "gulp help" for more build command usages.'), '\n'
21-
);
22-
});
47+
'build:extra', 'build:images', 'build:app-css', 'build:lib-js', 'build:lib-css', 'watch', 'server', () => {
48+
$.util.log(
49+
$.util.colors.green('Ready! Run "gulp help" for more build command usages.'), '\n'
50+
);
51+
})(cb);
2352
});

0 commit comments

Comments
 (0)