-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
336 lines (289 loc) · 8.72 KB
/
gulpfile.babel.js
File metadata and controls
336 lines (289 loc) · 8.72 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// 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 uglify = require('gulp-uglify');
// const watchify = require('watchify');
// const babel = require('babelify');
// const babel = require('gulp-babel');
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');
// const prepack = require('gulp-prepack-js');
const rollup = require('rollup').rollup;
// import gulp from 'gulp';
const file = require('gulp-file');
// import { rollup } from 'rollup';
const rbabel = require('rollup-plugin-babel');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
import ruglify from 'rollup-plugin-uglify';
// CONFIG
// ============================================================
const browserslist = require('./package.json').browserslist;
// const opts = {
// builtins: false,
// entries: ['src/js/index.js'],
// debug: true,
// insertGlobalVars: {
// global: glob
// }
// };
// const prepackConfig = {
// compatibility: 'browser',
// // sourcemaps: true
// };
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(prepack(prepackConfig))
// .pipe(uglify(uglifyConfig))
// .pipe(sourcemaps.write('./'))
// .pipe(gulp.dest('./public/dist/js'));
// }
// if (watch) {
// bundler.on('update', () => {
// console.log('-> bundling...');
// rebundle();
// console.log('done bundling.');
// });
// }
// return rebundle();
// }
// function watch() {
// return compile(true);
// }
// gulp.task('build', () => {
// return rollup.rollup({
// entry: './src/js/index.js',
// plugins: [
// resolve({
// browser: true,
// extensions: ['.js'],
// modulesOnly: true
// }),
// // rbabel({
// // // presets: [
// // // [
// // // 'es2015', {
// // // modules: false
// // // }
// // // ]
// // // ],
// // babelrc: true,
// // // exclude: 'node_modules/**'
// // })
// ]
// })
// .then(bundle => {
// return bundle.generate({
// format: 'umd',
// moduleName: 'PracticeJavaScript'
// });
// })
// .then(gen => {
// return file('bundle.js', gen.code, {src: true})
// .pipe(gulp.dest('./public/dist/js/bundle.js'));
// });
// });
let cache;
gulp.task('build', () => {
return rollup({
entry: 'src/js/index.js',
cache: cache,
sourceMap: true,
plugins: [
resolve({
jsnext: true,
preferBuiltins: false,
browser: true
}),
commonjs({
namedExports: {
'./node_modules/chai/index.js': ['assert']
}
}),
rbabel({
babelrc: false,
plugins: ['external-helpers'],
runtimeHelpers: true,
presets: [
[
'latest', {
es2015: {
modules: false
}
}
]
],
exclude: 'node_modules/**'
}),
// ruglify()
]
})
.then(bundle => {
cache = bundle;
return bundle.write({
moduleName: 'PracticeJavaScript',
format: 'iife',
dest: 'public/dist/js/bundle.js'
});
});
});
const buildWatcher = gulp.watch(['./src/js/index.js', './src/problems/**/*.js'], ['build']);
buildWatcher.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/problems/**/*.js', '!./src/js/index.js', '!./src/js/bundle.js'])
// .pipe(sourcemaps.init({
// loadMaps: true
// }))
// .pipe(uglify(uglifyConfig))
// .pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/dist/js'));
});
const jsWatcher = gulp.watch(['./src/js/*.js', '!./src/js/index.js', '!./src/js/bundle.js'], ['js']);
jsWatcher.on('change', event => {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// 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...');
});
// 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']);