-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
231 lines (211 loc) · 6.49 KB
/
gulpfile.babel.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
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
'use strict';
// Core Node
import * as fs from 'fs';
// Core Gulp
import gulp from 'gulp';
// Other Tools
import notifier from 'node-notifier';
import newer from 'gulp-newer';
import {create as bsCreate} from 'browser-sync';
const browserSync = bsCreate();
// Style Processing
import sass from 'gulp-sass';
import sassGlob from 'gulp-sass-bulk-import';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import normalize from 'postcss-normalize';
// Javascript Processing
import browserify from 'browserify';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import es from 'event-stream';
import rename from 'gulp-rename';
import uglify from 'gulp-uglify';
// Image Processing
import imagemin from 'gulp-imagemin';
import imageminJpegoptim from 'imagemin-jpegoptim';
// Template Processing
import nunjucksRender from 'gulp-nunjucks-render';
import htmlmin from 'gulp-htmlmin';
const packageJson = JSON.parse(fs.readFileSync('./package.json'));
const targets = packageJson.browserslist;
const env = process.env.NODE_ENV; // eslint-disable-line
const paths = {
'src': {
'styles': 'src/assets/sass',
'scripts': 'src/assets/js',
'images': 'src/assets/images',
'static': 'src/static',
'template': 'src/templates'
},
'dest': {
'styles': 'public/assets/css',
'scripts': 'public/assets/js',
'images': 'public/assets/images',
'static': 'public/static',
'template': 'public/'
}
};
function emitLog(stage, err) {
if (err) {
notifier.notify({
'title': `ERROR: ${stage}`,
'message': `There was an error within ${stage}`
});
console.log(err); // eslint-disable-line
}
}
// Styles Task
gulp.task('styles', () => {
let processors = [
autoprefixer(),
normalize()
];
if (env !== 'development') {
processors.push(cssnano);
}
return gulp.src(`${paths.src.styles}/**/*.scss`)
.pipe(sassGlob())
.pipe(
sass()
.on('error', (err) => {
emitLog('styles', err);
this.emit('end');
})
)
.pipe(postcss(processors))
.pipe(gulp.dest(paths.dest.styles));
});
// Scripts Task
gulp.task('scripts', () => {
return gulp.src(`${paths.src.scripts}/**/*.js`, (err, files) => {
let tasks = files.map((entry) => {
return browserify({entries: [entry]})
.transform(babelify, {
presets: [[
'env', {
'targets': targets[env]
}]]
})
.bundle()
.pipe(source(entry))
.pipe(buffer())
.pipe(env !== 'development' ? uglify() : buffer())
.pipe(rename((fileObj) => {
fileObj.dirname = '';
}))
.pipe(gulp.dest(paths.dest.scripts));
});
es.merge(tasks);
})
.on('end', (err) => {
emitLog('scripts', err);
});
});
// Images Task
gulp.task('images', () => {
if (env === 'development') {
return gulp.src(`${paths.src.images}/**/*.+(jpg|jpeg|gif|png|svg)`)
.pipe(newer(paths.dest.images))
.pipe(gulp.dest(paths.dest.images))
.on('end', (err) => {
emitLog('images', err);
});
}
return gulp.src(`${paths.src.images}/**/*.+(jpg|jpeg|gif|png|svg)`)
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.optipng({optimzationLevel: 5}),
imagemin.svgo({
plugins: [
{
cleanupIDs: false,
removeEmptyAttrs: false,
removeViewBox: false
}
]
}),
imageminJpegoptim({
max: 85,
progressive: true
})
]))
.pipe(gulp.dest(paths.dest.images))
.on('end', (err) => {
emitLog('images', err);
});
});
// Templates Task
gulp.task('templates',['styles'], () => {
let extension = '.php';
if (env === 'development') {
extension = '.html';
}
const styleData = fs.readFileSync('./public/assets/css/main.css', 'utf-8');
return gulp.src(`${paths.src.template}/**/*.njk`)
.pipe(nunjucksRender({
path: [paths.src.template],
data: {
styleData: styleData,
stylePath: './assets/css',
scriptPath: './assets/js',
imagePath: './assets/images',
staticPath: './static',
composerPath: './app',
env: env
},
envOptions: {
autoescape: false,
trimBlocks: true,
lstripBlocks: true
},
ext: extension
}))
.pipe(env !== 'development' ? htmlmin({collapseWhitespace: true, minifyJS: true, minifyCSS: true}) : buffer())
.pipe(gulp.dest(paths.dest.template))
.on('end', (err) => {
emitLog('template', err);
});
});
gulp.task('script-watch', ['scripts'], (done) => {
browserSync.reload();
done();
});
gulp.task('template-watch', ['templates'], (done) => {
browserSync.reload();
done();
});
gulp.task('browserSync', () => {
browserSync.init({
server: {
baseDir: './public/',
serveStaticOptions: {
extensions: ['html']
},
options: {
open: false
}
}
});
})
// Static Files
gulp.task('static', () => {
return gulp.src(`${paths.src.static}/**/*`, {dot: true})
.pipe(newer(paths.dest.static))
.pipe(gulp.dest(paths.dest.static))
.on('end', (err) => {
emitLog('static', err);
});
});
// Watch Task
gulp.task('watch', ['browserSync', 'default'], () => {
gulp.watch(`${paths.src.static}/**/*`, ['static']);
gulp.watch(`${paths.src.styles}/**/*.scss`,['template-watch']);
gulp.watch(`${paths.src.scripts}/**/*.js`,['script-watch']);
gulp.watch(`${paths.src.template}/**/*`,['template-watch']);
gulp.watch(`${paths.src.images}/**/*.+(jpg|jpeg|gif|png|svg)`,['images']);
});
// Compilation Task
gulp.task('default', ['styles', 'scripts', 'images', 'templates', 'static']);