UNPKG

1.91 kBJavaScriptView Raw
1const gulp = require('gulp')
2const browserSync = require('browser-sync')
3const reload = browserSync.reload
4const rollup = require('rollup')
5const babel = require('rollup-plugin-babel')
6const uglify = require('rollup-plugin-uglify')
7const resolve = require('rollup-plugin-node-resolve')
8const commonjs = require('rollup-plugin-commonjs')
9const gzip = require('gulp-gzip')
10const sourcemaps = require('gulp-sourcemaps');
11const cssnano = require('gulp-cssnano');
12
13// Static Server & watching files:
14gulp.task('serve', ['build'], function () {
15 browserSync({
16 port: 4040,
17 server: {
18 open: false,
19 baseDir: './'
20 }
21 }).reload
22})
23
24gulp.task('watch', function() {
25 gulp.watch('./index.html').on('change', reload)
26 gulp.watch(['./dev/app.js', 'dev/**/*.js'], ['build', reload])
27 gulp.watch('./js/app.js').on('change', reload)
28 gulp.watch('./dev/css/*.css', ['build', reload])
29})
30
31gulp.task('build', function () {
32
33 gulp.src('./dev/css/styles.css')
34 .pipe(sourcemaps.init())
35 .pipe(cssnano({advanced: true, aggressiveMerging: true}))
36 .pipe(sourcemaps.write('.'))
37 .pipe(gulp.dest('./css'))
38
39
40 return rollup.rollup({
41 input: './dev/app.js',
42 plugins: [
43 babel(),
44 resolve({
45 jsnext: true,
46 main: true,
47 browser: true
48 }),
49 commonjs(),
50 uglify({
51 compress: {
52 collapse_vars: true
53 }
54 })
55 ]
56 })
57 .then((bundle) => {
58 return bundle.write({
59 format: 'iife',
60 name: 'app',
61 file: './js/app.js',
62 sourcemap: true
63 })
64 })
65 .then((bundle) => {
66 return gulp.src('./js/app.js')
67 .pipe(gzip({ extension: 'gzip' }))
68 .pipe(gulp.dest('./js'))
69 })
70 .then((bundle) => {
71 gulp.src('./css/styles.css')
72 .pipe(gzip({ extension: 'gzip' }))
73 .pipe(gulp.dest('./css'))
74 })
75})
76
77// Process app.js and load page in browser:
78gulp.task('default', ['serve', 'watch'])