-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
154 lines (130 loc) · 4.63 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
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
'use strict';
const path = require('path');
const fs = require('fs')
const gulp = require('gulp');
const less = require('gulp-less');
const minify = require('gulp-clean-css');
const uglify = require('gulp-uglify-es').default;
const image = require('gulp-image');
const rename = require('gulp-rename');
const newer = require('gulp-newer');
const nodemon = require('gulp-nodemon');
const clean = require('gulp-clean');
const browserify = require('browserify');
const browserSync = require('browser-sync');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const glob = require('glob')
const merge = require('merge-stream');
const base_path = './public/src/';
const dist_path = './public/dist/';
const reload = () => {
browserSync.reload();
return;
};
const nodemon_watched = ['./bin/www','./server.js','./routes/*.js'];
const compiled_assets = {
less: './public/src/stylesheets/*.less',
less_entry: './public/src/stylesheets/style.less',
js: './public/src/scripts/**/*.js',
pug: './views/*.pug',
image: './public/src/images/**/*'
};
const moved_assets = './public/src/config/**/*';
const _move = () => {
return gulp.src(moved_assets, { base: base_path })
.pipe(newer(dist_path))
.pipe(gulp.dest(dist_path))
};
const _js = () => {
var files = glob.sync(compiled_assets.js);
var tasks = files.map(function (entry) {
var dir = path.relative(base_path, entry);
dir = path.dirname(dir);
return browserify({ entries: [entry] })
.bundle().on('error', (error) => console.error(error))
.pipe(source(entry))
.pipe(buffer())
// .pipe(uglify())
.on('error', function (err) { console.log(err) })
.pipe(rename({ dirname: dir }))
.pipe(gulp.dest(dist_path))
});
return merge(tasks).on('end', () => {
browserSync.reload();
});
};
const _js_watch = (entry) => {
entry = './' + entry;
entry = entry.replace(/\\/g, '/');
console.log(entry);
var dir = path.relative(base_path, entry);
dir = path.dirname(dir);
browserify({ entries: entry })
.bundle().on('error', (error) => console.error(error))
.pipe(source(entry))
//.pipe(buffer())
//.pipe(uglify())
.pipe(rename({ dirname: dir }))
.pipe(gulp.dest(dist_path))
.pipe(browserSync.reload({ stream: true }));
};
const _less = () => {
return gulp.src(compiled_assets.less_entry, { base: base_path })
.pipe(less()).on('error', (error) => console.error(error)) // compile to css
.pipe(minify()).on('error', (error) => console.error(error)) // minify
.pipe(gulp.dest(dist_path)) // output
.pipe(browserSync.reload({ stream: true })); // reload on change
};
const _image = () => {
return gulp.src(compiled_assets.image, { base: base_path })
.pipe(newer(dist_path))
.pipe(image()).on('error', (error) => console.error(error))
.pipe(gulp.dest(dist_path))
.pipe(browserSync.reload({ stream: true }));
};
const _watch = () => {
gulp.watch("./public/src/**/*").on('unlink', (file) => {
var filePathFromSrc = path.relative(path.resolve('src'), file);
var destFilePath = path.resolve('src', filePathFromSrc);
destFilePath = destFilePath.replace('src', 'dist');
console.log("Deleting: " + destFilePath);
if (fs.existsSync(destFilePath)) {
fs.unlinkSync(destFilePath);
}
});
gulp.watch(compiled_assets.js).on('change', _js_watch);
gulp.watch(compiled_assets.less, gulp.series(_less));
gulp.watch(compiled_assets.image, gulp.series(_image));
gulp.watch(moved_assets, gulp.series(_move));
gulp.watch(compiled_assets.pug).on('change', reload);
};
const _server = (cb) => {
var started = false;
return nodemon({
script: './bin/www',
watch: nodemon_watched,
env: { 'NODE_ENV': 'development' }
}).on('start', () => {
if (!started) {
cb();
started = true;
browserSync.init(null, {
proxy: "http://localhost:3000",
port: 3001,
notify: true,
debug: true
});
}
});
};
const _clean = () => {
return gulp.src(dist_path, { read: false, allowEmpty: true })
.pipe(clean());
};
gulp.task('clean', gulp.series(_clean));
gulp.task('build', gulp.parallel(_js, _less, _image, _move));
gulp.task('watch', _watch)
gulp.task('sync', gulp.series('build', gulp.parallel(_server, 'watch')));
gulp.task('serveWatch', gulp.parallel(_server, 'watch'));
gulp.task('serve', gulp.series(_server));