-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
171 lines (149 loc) · 4.34 KB
/
gulpfile.babel.js
File metadata and controls
171 lines (149 loc) · 4.34 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import gulp from 'gulp';
// js
import rollupStream from '@rollup/stream';
import babel from 'rollup-plugin-babel';
import terser from 'gulp-terser';
import rename from 'gulp-rename';
import sourcemaps from 'gulp-sourcemaps';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import inject from '@rollup/plugin-inject';
import alias from '@rollup/plugin-alias';
import merge from 'merge-stream';
// sass
import postcss from 'gulp-postcss';
import sass from 'gulp-dart-sass';
import cssnano from 'cssnano';
import autoprefixer from 'autoprefixer';
import atImport from 'postcss-import';
import pixrem from 'pixrem';
// other
// import imagemin from 'gulp-imagemin';
import { deleteAsync as del } from 'del';
import browserSync from 'browser-sync';
import booking from './booking/static/booking/config.gulp.js';
import dbase from './dbase/static/dbase/config.gulp.js';
import camera from './camera/static/camera/config.gulp.js';
import catalog from './catalog/static/catalog/config.gulp.js';
const configs = [booking, dbase, camera, catalog];
const aliases = configs.filter(config => config.alias).map(config => config.alias);
const server = browserSync.create();
function serve(done) {
server.init({
notify: false,
proxy: '127.0.0.1:8000',
reloadDelay: 300,
reloadDebounce: 500,
});
done();
}
function reload(done) {
server.reload();
done();
}
// Flatten an array with potential subarrays
const flatten = (arr) => arr.reduce((flat, next) => flat.concat(next), []);
function extract_tasks_from_configs(task_type) {
// Get the configs that have a task with the specified type and return the tasks therefore
const tasks_per_config = configs.filter(config => config[task_type]).map(config => config[task_type]);
// Flatten the tasks as some tasks may be arrays of tasks
return flatten(tasks_per_config);
}
function scripts() {
const tasks = extract_tasks_from_configs('scripts').map(config => {
return rollupStream({
input: config.srcDir + '/' + config.entry,
output: {
sourcemap: true,
format: 'umd',
},
context: 'window',
plugins: [
inject(
{
$: 'jquery',
jQuery: 'jquery'
}
),
alias({
entries: aliases
}),
babel({
exclude: 'node_modules/**'
}),
resolve(), commonjs()
],
}).pipe(source(config.entry, config.srcDir))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(gulp.dest(config.dest)) // save .js
.pipe(terser())
.pipe(rename({ suffix: '.min' }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.dest)); // save .min.js
});
return merge(tasks);
}
function styles() {
const processors = [
atImport,
pixrem,
autoprefixer,
cssnano
];
const tasks = extract_tasks_from_configs('styles').map(config => {
return gulp.src(config.src)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: ['node_modules']
}).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(rename({ suffix: '.min' }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.dest))
.pipe(server.stream());
});
return merge(tasks);
}
function images() {
const tasks = extract_tasks_from_configs('images').map(config => {
return gulp.src(config.src)
// .pipe(imagemin())
.pipe(gulp.dest(config.dest));
});
return merge([gulp.src('.', { allowEmpty: true }), ...tasks]);
}
function staticFiles() {
let tasks = [];
configs.filter(config => config.staticFiles).map(config => config.staticFiles).map(config => {
config.map(files => {
tasks.push(gulp.src(files.src)
.pipe(gulp.dest(files.dest)));
});
});
return merge(tasks);
}
function watch() {
// Scripts
gulp.watch(
['**/static/**/*.js'],
gulp.series(scripts, reload)
);
// Styles
gulp.watch(
['**/static/**/*.css', '**/static/**/*.scss'],
styles
);
// Pages
gulp.watch(
['**/*.html'],
reload
);
}
const clean = () => del(['build']);
const build = gulp.parallel(scripts, styles, images, staticFiles);
const dev = gulp.series(serve, watch);
export { scripts, styles, images, staticFiles, clean, build, dev };
export default build;