/
gulpfile.js
201 lines (173 loc) · 4.52 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
// GULP
const gulp = require('gulp');
// PLUGINS
const autoprefixer = require("autoprefixer");
const babel = require('gulp-babel');
const browserSync = require("browser-sync").create();
const concat = require('gulp-concat');
const cssnano = require("cssnano");
const del = require("del");
const htmlValidator = require('gulp-w3c-html-validator');
const imagemin = require('gulp-imagemin');
const jshint = require('gulp-jshint');
const newer = require("gulp-newer");
const postcss = require("gulp-postcss");
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const terser = require('gulp-terser');
const uglify = require('gulp-uglify');
// FILE PATHS
const paths = {
html: {
src: './app/*.html',
tmp: 'tmp/',
dist: './dist/'
},
styles: {
src: './app/scss/**/*.scss',
tmp: './tmp/css/',
dist: './dist/css/'
},
scripts: {
src: './app/scripts/**/*.js',
tmp: './tmp/js/',
dist: 'dist/js/'
},
assets: {
src: './app/assets/**/*',
tmp: './tmp/assets/',
dist: './dist/assets/'
}
};
// CLEAN ASSETS
function clean() {
return del([
paths.styles.tmp,
paths.scripts.tmp,
paths.assets.tmp,
paths.styles.dist,
paths.scripts.dist,
paths.assets.dist
]);
}
// HTML
function html() {
return (
gulp
.src(paths.html.src)
.pipe(htmlValidator())
.pipe(htmlValidator.reporter())
.pipe(gulp.dest(paths.html.tmp))
.pipe(gulp.dest(paths.html.dist))
// RELOAD SERVER
.pipe(browserSync.stream())
);
}
// OPTIMIZE IMAGES
function images() {
return gulp
.src(paths.assets.src)
.pipe(newer(paths.assets.tmp))
.pipe(imagemin())
.pipe(gulp.dest(paths.assets.tmp))
.pipe(gulp.dest(paths.assets.dist))
}
// PROCESS SCSS TO CSS
function style() {
return (
gulp
// GET STYLES
.src(paths.styles.src)
// INITIALIZE SOURCE MAP
.pipe(sourcemaps.init())
// PROCESS SOURCE FILES INTO CSS
.pipe(sass())
.on("error", sass.logError)
// WRITE SOURCE MAP
.pipe(sourcemaps.write())
// ADD CSS FILE TO TMP FOLDER
.pipe(gulp.dest(paths.styles.tmp))
// ADD MINIMIZED FILE TO DIST
.pipe(rename({ suffix: ".min" }))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(gulp.dest(paths.styles.tmp))
.pipe(gulp.dest(paths.styles.dist))
// RELOAD SERVER
.pipe(browserSync.stream())
);
}
// CONCAT JAVASCRIPT FILES
function scripts() {
return (
gulp
// GET FILES
.src([
'./app/scripts/main.js',
'./app/scripts/utils.js',
'./app/scripts/offscreen-canvas.js',
'./app/scripts/orb-canvas.js',
'./app/scripts/orb.js',
'./app/scripts/tone-block.js',
'./app/scripts/interaction.js',
'./app/scripts/wavetables/*.js',
'./app/scripts/audio.js'
])
// INITIALIZE SOURCEMAPS
.pipe(sourcemaps.init())
// CONCATENATE
.pipe(babel({presets: ["@babel/preset-env"]}))
.pipe(concat('main.js'))
// WRITE SOURCEMAP
.pipe(sourcemaps.write())
// ADD TO TMP FOLDER
.pipe(gulp.dest(paths.scripts.tmp))
// MINIMIZE AND ADD TO DIST
.pipe(uglify())
.pipe(gulp.dest(paths.scripts.dist))
// RELOAD SERVER
.pipe(browserSync.stream())
);
}
// JS LINTER
function scriptsLint() {
return (
gulp
// GET FILES
.src(paths.scripts.src)
// LINT
.pipe(jshint())
.pipe(jshint.reporter('default'))
);
}
// WATCH FOR CHANGES
function watch() {
// INITIALIZE BROWSER
browserSync.init({
// You can tell browserSync to use this directory and serve it as a mini-server
server: {
baseDir: './tmp'
}
// If you are already serving your website locally using something like apache
// You can use the proxy setting to proxy that instead
// proxy: "yourlocal.dev"
});
// WATCH TASKS
gulp.watch(paths.html.src, html)
gulp.watch(paths.styles.src, style)
gulp.watch(paths.scripts.src, scripts)
gulp.watch(paths.assets.src, images)
}
// COMPLEX TASKS
const build = gulp.series(clean, gulp.parallel(html, style, scripts, images));
gulp.task('build', build);
const develop = gulp.series(build, watch);
gulp.task('develop', develop);
// Don't forget to expose the task!
// EXPOSED TASKS
exports.style = style;
exports.jslint = scriptsLint;
exports.watch = watch;
exports.build = build;
exports.develop = develop;