-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
248 lines (198 loc) · 6.23 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// DEPS
// ============================================================
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const uglifyES = require('uglify-es');
const composer = require('gulp-uglify/composer');
const uglify = composer(uglifyES, console);
const watchify = require('watchify');
const babel = require('babelify');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const svgo = require('gulp-svgo');
const sass = require('gulp-sass');
const livereload = require('gulp-livereload');
const htmlmin = require('gulp-htmlmin');
const swPrecache = require('sw-precache');
const image = require('gulp-image');
// CONFIG
// ============================================================
const browserslist = require('./package.json').browserslist;
const opts = {
builtins: false,
entries: ['src/js/index.js'],
debug: true,
insertGlobalVars: {
global: glob
}
};
const prepackConfig = {};
const uglifyConfig = {};
const htmlminConfig = {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true
};
const imageConfig = {
pngquant: true,
svgo: false,
concurrent: 10,
jpegoptim: true
};
// TASKS
// ============================================================
function compile(watch) {
const bundler = watchify(browserify(opts).transform(babel.configure({
presets: [
['env', {
targets: {
browsers: browserslist
}
}]
]
})));
function rebundle() {
return bundler.bundle()
.on('error', err => {
console.error(err);
this.emit('end');
})
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify(uglifyConfig))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/dist/js'));
}
if (watch) {
bundler.on('update', () => {
console.log('-> bundling...');
rebundle();
livereload();
console.log('done bundling.');
});
}
return rebundle();
}
function watch() {
return compile(true);
}
// CSS
// ============================================================
gulp.task('css', () => {
const plugins = [
autoprefixer({browsers: browserslist}),
cssnano()
];
return gulp.src('./src/css/style.scss')
.pipe(sass.sync().on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(gulp.dest('./public/dist/css'))
.pipe(livereload());
});
const cssWatcher = gulp.watch('src/css/**/*.scss', ['css']);
cssWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// OTHER JS
// ============================================================
gulp.task('js', () => {
return gulp.src(['./src/js/*.js', '!./src/js/index.js'])
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify(uglifyConfig))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/dist/js'));
});
const jsWatcher = gulp.watch('./src/js/loadJS.js', ['js']);
jsWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// IMG
// ============================================================
gulp.task('img-icons', () => {
return gulp.src('./src/*.svg')
.pipe(svgo())
.pipe(gulp.dest('./public/'));
});
gulp.task('img-images', () => {
return gulp.src('./src/img/*.{svg,png,jpg}')
.pipe(svgo())
.pipe(image(imageConfig))
.pipe(gulp.dest('./public/dist/img'));
});
gulp.task('img', ['img-icons', 'img-images']);
const imgWatcher = gulp.watch('src/**/*.{svg,png}', ['img']);
imgWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// HTML
// ============================================================
gulp.task('html', () => {
return gulp.src('./src/*.html')
.pipe(htmlmin(htmlminConfig))
.pipe(gulp.dest('./public/')); // Output goes to root of /public, as per firebase hosting
});
const htmlWatcher = gulp.watch('src/*.html', ['html']);
htmlWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// SERVICE WORKER
// ============================================================
const rootDir = './public';
gulp.task('generate-service-worker', callback => {
swPrecache.write(`./src/service-worker.js`, {
staticFileGlobs: [
`${rootDir}/dist/**/*.{js,css,png,jpg,gif,svg,eot,ttf,woff}`,
`${rootDir}/launcher-icon-*.{png,svg}`,
`${rootDir}/index.html`
],
stripPrefix: rootDir
}, callback);
});
gulp.task('optimize-service-worker', ['generate-service-worker'], () => {
return gulp.src(`./src/service-worker.js`)
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify(uglifyConfig))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public'));
});
// Do all service-worker things
gulp.task('service-worker', ['generate-service-worker', 'optimize-service-worker']);
const swWatcher = gulp.watch([rootDir + '/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}'], ['service-worker']);
swWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// MANIFEST
// ============================================================
gulp.task('manifest', () => {
return gulp.src('./src/manifest.json')
.pipe(gulp.dest('./public/'));
});
const manifestWatcher = gulp.watch('src/manifest.json', ['manifest']);
manifestWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// BUILD
// ============================================================
gulp.task('build', ['css', 'js', 'img', 'html'], () => {
return compile();
});
gulp.task('watch', () => {
livereload.listen();
return watch();
});
function glob() {
return 'typeof self !== "undefined" ? self : ' + 'typeof window !== "undefined" ? window : {}'; // eslint-disable-line no-useless-concat
}
// gulp.task('default', ['build', 'manifest', 'service-worker', 'watch']);
gulp.task('default', ['build', 'manifest', 'service-worker', 'watch']);