-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
189 lines (154 loc) · 4.22 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
const prefixer = require('autoprefixer')
const sync = require('browser-sync')
const cssnano = require('cssnano')
const del = require('del')
const fs = require('fs')
const gulp = require('gulp')
const changed = require('gulp-changed')
const include = require('gulp-file-include')
const htmlmin = require('gulp-htmlmin')
const imagemin = require('gulp-imagemin')
const plumber = require('gulp-plumber')
const postcss = require('gulp-postcss')
const sass = require('gulp-sass')
const maps = require('gulp-sourcemaps')
const notifier = require('node-notifier')
const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const commonjs = require('rollup-plugin-commonjs')
const resolve = require('rollup-plugin-node-resolve')
const uglify = require('rollup-plugin-uglify')
// error handler
const onError = function(error) {
notifier.notify({
'title': 'Error',
'message': 'Compilation failure.'
})
console.log(error)
this.emit('end')
}
// clean
gulp.task('clean', function() {del('dist')})
// html
gulp.task('html', ['images'], function() {
return gulp.src('src/html/**/*.html')
.pipe(plumber({ errorHandler: onError }))
.pipe(include({ prefix: '@', basepath: 'dist/images/' }))
.pipe(htmlmin({ collapseWhitespace: true, removeComments: true }))
.pipe(gulp.dest('dist'))
})
// sass
const processors = [
prefixer({ browsers: 'last 2 versions' }),
cssnano({ safe: true })
]
gulp.task('sass', function() {
return gulp.src('src/sass/style.scss')
.pipe(plumber({ errorHandler: onError }))
.pipe(maps.init())
.pipe(sass())
.pipe(postcss(processors))
.pipe(maps.write('./maps', { addComment: false }))
.pipe(gulp.dest('dist'))
})
// js
const read = {
entry: 'src/js/main.js',
sourceMap: true,
plugins: [
resolve({ jsnext: true, main: true }),
commonjs(),
babel({ exclude: 'node_modules/**' }),
uglify()
]
}
const write = {
format: 'iife',
sourceMap: true
}
gulp.task('js', function() {
return rollup
.rollup(read)
.then(function(bundle) {
// generate the bundle
const files = bundle.generate(write)
// write the files to dist
fs.writeFileSync('dist/bundle.js', files.code)
fs.writeFileSync('dist/maps/bundle.js.map', files.map.toString())
})
})
// images
gulp.task('images', function() {
return gulp.src('src/images/**/*.{gif,jpg,png,svg}')
.pipe(plumber({ errorHandler: onError }))
.pipe(changed('dist/images'))
.pipe(imagemin({ progressive: true, interlaced: true }))
.pipe(gulp.dest('dist/images'))
})
// fonts, videos, favicon
const others = [
{
name: 'fonts',
src: '/fonts/**/*.{woff,woff2}',
dest: '/fonts'
}, {
name: 'videos',
src: '/videos/**/*',
dest: '/videos'
}, {
name: 'favicon',
src: '/favicon.ico',
dest: ''
}
]
others.forEach(function(object) {
gulp.task(object.name, function() {
return gulp.src('src' + object.src)
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest('dist' + object.dest))
})
})
// server
const server = sync.create()
const reload = sync.reload
const sendMaps = function(req, res, next) {
const filename = req.url.split('/').pop()
const extension = filename.split('.').pop()
if(extension === 'css' || extension === 'js') {
res.setHeader('X-SourceMap', '/maps/' + filename + '.map')
}
return next()
}
const options = {
notify: false,
proxy: 'django:8080',
watchOptions: {
ignored: '*.map'
},
port: 8080
}
gulp.task('server', function() {sync(options)})
// watch
gulp.task('watch', function() {
gulp.watch('src/html/**/*.html', ['html', reload])
gulp.watch('src/sass/**/*.scss', ['sass', reload])
gulp.watch('src/js/**/*.js', ['js', reload])
gulp.watch('src/images/**/*.{gif,jpg,png,svg}', ['images', reload])
})
// build and default tasks
gulp.task('build', ['clean'], function() {
try {
// create dist directories
fs.mkdirSync('dist')
} catch (err) {
// whatever.
}
try {
fs.mkdirSync('dist/maps')
} catch (err) {
// whatever.
}
// run the tasks
gulp.start('html', 'sass', 'js', 'images', 'fonts', 'videos', 'favicon')
})
gulp.task('default', ['build', 'server', 'watch'])