-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
126 lines (108 loc) · 2.71 KB
/
gulpfile.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
'use strict';
const path = require('path');
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const $ = require('gulp-load-plugins')();
const vinylify = require('factor-vinylify');
const browserify = require('browserify');
// styles dependencies
const nib = require('nib');
const cssimport = require('postcss-import');
const cssnano = require('cssnano');
// config
const config = {
views: {
src: ['views/**/*'],
},
styles: {
src: ['static/src/styles/**/*.styl'],
dest: 'static/dist/styles',
},
scripts: {
srcPath: 'static/src/scripts',
dest: 'static/dist/scripts',
entries: [
'bundle.js',
],
common: 'common.js',
},
fonts: {
src: [],
dest: 'static/dist/fonts',
},
};
config.scripts.src = path.join(config.scripts.srcPath, '/**/*.js');
// fonts
gulp.task('fonts', () => {
gulp.src(config.fonts.src)
.pipe($.changed(config.fonts.dest))
.pipe(gulp.dest(config.fonts.dest))
.pipe(browserSync.stream())
});
gulp.task('watch-fonts', () => {
gulp.watch([config.fonts.src], ['fonts']);
});
// styles
gulp.task('styles', () => {
const processors = [cssimport, cssnano];
gulp.src(config.styles.src)
.pipe($.stylus({ use: [nib()] }))
.on('error', $.util.log)
.pipe($.postcss(processors))
.on('error', $.util.log)
.pipe(gulp.dest(config.styles.dest))
.pipe(browserSync.stream())
});
gulp.task('watch-styles', () => {
gulp.watch([config.styles.src], ['styles']);
});
// javascript
gulp.task('scripts', () => {
browserify({
entries: config.scripts.entries,
basedir: config.scripts.srcPath
}).plugin(vinylify, {
outputs: config.scripts.entries,
common: config.scripts.common,
})
.bundle()
.on('error', function (err) {
$.util.log(err.toString());
this.emit("end");
})
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream())
});
gulp.task('watch-scripts', () => {
gulp.watch([config.scripts.src], ['scripts']);
});
// tasks
gulp.task('build', ['fonts', 'styles', 'scripts']);
gulp.task('watch', ['watch-fonts', 'watch-styles', 'watch-scripts']);
gulp.task('nodemon', (cb) => {
let started = false;
return $.nodemon({
script: 'server.js',
ext: 'js',
ignore: ['static/**/*'],
}).on('start', () => {
if (!started) {
cb();
started = true;
}
}).on('restart', () => {
setTimeout(() => browserSync.reload(), 1000);
});
});
gulp.task('dev', ['build', 'watch', 'nodemon'], () => {
let options = {
proxy: 'localhost:3000',
files: config.views.src,
port: 5000,
open: false,
};
if (process.env.VIRTUAL_HOST)
options.socket = { domain: process.env.VIRTUAL_HOST };
browserSync.init(options);
});
gulp.task('default', ['build']);