-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
157 lines (137 loc) · 4.64 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
var gulp = require('gulp');
var webp = require('gulp-webp');
var browserify = require('browserify');
var babelify = require('babelify');
var sourcemaps = require('gulp-sourcemaps');
var cleanCSS = require('gulp-clean-css');
var autoprefixer = require('gulp-autoprefixer');
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var gulpSequence = require('gulp-sequence');
var htmlmin = require('gulp-htmlmin');
var clean = require('gulp-clean');
var minifyInline = require('gulp-minify-inline');
var gzip = require('gulp-gzip');
var gzipStatic = require('connect-gzip-static');
var connect = require('gulp-connect');
var jsSrcMainList = ['js/dbhelper.js', 'js/main.js'];
var jsSrcRestaurantList = ['js/dbhelper.js', 'js/restaurant_info.js'];
// ===================== Default Task =====================
gulp.task('default', ['prod:serve']);
// ===================== Build & Serve Production Build =====================
gulp.task('prod:serve', gulpSequence('build', 'serve'));
// ===================== Production Build =====================
gulp.task('build', gulpSequence('clean', 'scripts:prod', 'html:prod', 'styles:prod', 'copy:prod', 'webp:prod', 'gzip:prod'));
// Copy app contents to dist directory
gulp.task('copy:prod', function () {
return gulp.src(['!node_modules/**', '!screenshots/**', '**/*.{png,jpg}', 'service-worker.js', 'manifest.json', '!gulpfile.js'])
.pipe(gulp.dest('./dist'));
});
// ===================== Clean Build =====================
gulp.task('clean', function () {
return gulp.src('./dist', {
read: false
})
.pipe(clean());
});
// ===================== Minify HTML =====================
gulp.task('html:prod', function () {
return gulp.src(['!node_modules/**', '**/*.html'])
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(minifyInline())
.pipe(gulp.dest('./dist'));
});
// ===================== WebP Image Conversion =====================
gulp.task('webp:prod', function () {
gulp.src('img/*.jpg')
.pipe(webp({
method: 6
}))
.pipe(gulp.dest('./dist/img/webp'));
});
// ===================== Styles =====================
gulp.task('styles:prod', function () {
gulp.src('css/styles.css')
.pipe(cleanCSS({
compatibility: 'ie8'
}))
.pipe(autoprefixer({
browsers: ['last 2 versions']
}))
.pipe(rename('styles.min.css'))
.pipe(gulp.dest('./dist/css'));
});
// ===================== Scripts =====================
gulp.task('scripts:prod', gulpSequence('script:index', 'script:restaurant'));
gulp.task('script:index', function () {
jsSrcMainList.map(function (jsFile) {
return browserify({
entries: [jsFile]
})
.transform(babelify.configure({
presets: ['env']
}))
.bundle()
.pipe(source('index.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/js'));
});
});
gulp.task('script:restaurant', function () {
jsSrcRestaurantList.map(function (jsFile) {
return browserify({
entries: [jsFile]
})
.transform(babelify.configure({
presets: ['env']
}))
.bundle()
.pipe(source('restaurant.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/js'));
});
});
// ===================== Gzip Build =====================
gulp.task('gzip:prod', gulpSequence('gzip-html', 'gzip-css', 'gzip-js'));
gulp.task('gzip-html', function () {
gulp.src('./dist/**/*.html')
.pipe(gzip())
.pipe(gulp.dest('./dist'));
});
gulp.task('gzip-css', function () {
gulp.src('./dist/css/**/*.min.css')
.pipe(gzip())
.pipe(gulp.dest('./dist/css'));
});
gulp.task('gzip-js', function () {
gulp.src('./dist/js/**/*.js')
.pipe(gzip())
.pipe(gulp.dest('./dist/js'));
});
// ===================== Serve Build =====================
gulp.task('serve', function () {
connect.server({
root: "dist/index.html",
port: 9000,
middleware: function () {
return [
gzipStatic(__dirname, {
maxAge: 31536000000
})
]
}
});
});