1 | const gulp = require('gulp')
|
2 | const browserSync = require('browser-sync')
|
3 | const reload = browserSync.reload
|
4 | const rollup = require('rollup')
|
5 | const babel = require('rollup-plugin-babel')
|
6 | const uglify = require('rollup-plugin-uglify')
|
7 | const resolve = require('rollup-plugin-node-resolve')
|
8 | const commonjs = require('rollup-plugin-commonjs')
|
9 | const gzip = require('gulp-gzip')
|
10 | const sourcemaps = require('gulp-sourcemaps');
|
11 | const cssnano = require('gulp-cssnano');
|
12 |
|
13 |
|
14 | gulp.task('serve', ['build'], function () {
|
15 | browserSync({
|
16 | port: 4040,
|
17 | server: {
|
18 | open: false,
|
19 | baseDir: './'
|
20 | }
|
21 | }).reload
|
22 | })
|
23 |
|
24 | gulp.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 |
|
31 | gulp.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 | entry: './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 | dest: './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 |
|
78 | gulp.task('default', ['serve', 'watch'])
|