-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
269 lines (246 loc) · 6.61 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
cleanCSS = require('gulp-clean-css'),
pug = require('gulp-pug'),
htmlreplace = require('gulp-html-replace'),
browserSync = require('browser-sync'),
browserify = require('browserify'),
babelify = require('babelify'),
eslint = require('gulp-eslint'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
runSequence = require('run-sequence').use(gulp),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
merge = require('gulp-merge'),
del = require('del'),
cache = require('gulp-cache');
let allInDir = '/**/*',
srcDirs = {
js: {
app: 'app/src/js/',
vendor: 'app/src/js/vendor/',
},
view: ['app/src/views/*.pug', '!app/src/views/_*.pug'],
scss: 'app/src/scss/',
fonts: 'app/src/fonts/'
},
srcFileNames = {
js: 'main.js',
},
buildDirs = {
js: 'app/build/js',
css: 'app/build/css',
img: 'app/build/img',
html: 'app/build'
},
buildFileNames = {
js: 'bundle.js',
vendorJs: 'vendor.js'
},
distFileNames = {
css: 'styles.min.css',
js: 'app.min.js',
},
pathReplacements = {
css: 'css/styles.min.css',
js: 'js/app.min.js'
},
distDirs = {
root: 'dist',
css: './dist/css',
js: 'dist/js',
img: 'dist/img/',
fonts: 'dist/fonts',
},
vendorMergeFiles = [ // IN ORDER!
'app/src/js/vendor/jquery-3.3.1.min.js',
'app/src/js/vendor/materialize.js'
],
jsFILES = [buildFileNames.js, buildFileNames.vendorJs];
/**
* HTML Tasks
*/
gulp.task('views', function buildHTML() {
return gulp.src(srcDirs.view)
.pipe(pug({
pretty: true,
}))
.on('error', console.error.bind(console))
.pipe(gulp.dest(buildDirs.html))
.pipe(browserSync.reload({
stream: true
}))
});
// Corrects file paths in HTML files for production.
gulp.task('html-replace', () => {
return gulp.src(buildDirs.html + allInDir + '.html')
.pipe(htmlreplace({
'css': pathReplacements.css,
'js': pathReplacements.js,
}))
.pipe(gulp.dest(distDirs.root))
})
/**
* Style Tasks
*/
gulp.task('sass', function () {
return gulp.src(srcDirs.scss + allInDir + '.scss') //Source all files ending with.scss in scss directory and its subdirectories
.pipe(sass())
.on('error', console.error.bind(console))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest(buildDirs.css))
.pipe(browserSync.reload({
stream: true
}))
});
gulp.task('css', () => {
return gulp.src(buildDirs.css + allInDir + '.css')
.pipe(sourcemaps.init())
.pipe(cleanCSS({ debug: true }, (details) => {
console.log(`${details.name} Reduced: ${details.stats.minifiedSize / details.stats.originalSize * 100}%`);
}))
.pipe(concat(distFileNames.css))
.pipe(sourcemaps.write())
.pipe(gulp.dest(distDirs.css))
});
/**
* JavaScript Tasks
*/
// Converts ES6 to browser-ready JS w/ ENV Preset.
gulp.task('babel', () => { // Converts es6 to browser ready
return browserify({
entries: [srcDirs.js.app + srcFileNames.js],
debug: true,
})
.transform(babelify, { presets: ['env'] }) // babelify
.bundle() // bundle
.pipe(source(buildFileNames.js)) // grab viny stream
.pipe(buffer()) // buffer the stream
.pipe(gulp.dest(buildDirs.js)) // pipe to app/build/js
});
// Merges jQuery and Materialize JS libs in correct order
gulp.task('vendor', () => {
return merge(
gulp.src(vendorMergeFiles)
.pipe(concat(buildFileNames.vendorJs))
.pipe(gulp.dest(buildDirs.js)) // Drops in build DIR
);
})
// Lints non-vendor JS w/ AirBnB Preset
gulp.task('lint', () => {
return gulp.src([
srcDirs.js.app + allInDir + '.js',
'!' + srcDirs.js.vendor + allInDir + '.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// JS Bundle Task
gulp.task('bundle', ['babel'], () => {
return gulp.src([
buildDirs.js + buildFileNames.vendorJs,
buildDirs.js + buildFileNames.js
])
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(concat(distFileNames.js))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(distDirs.js));
});
// JS Build Task
gulp.task('js', () => {
jsFILES.map((entry) => {
return browserify({ entries: [buildDirs.js + '/' + entry] }) // Browserfy start
.transform(babelify, { presets: ['env'] }) // babelify
.bundle() // bundle
.pipe(source(entry)) // grab the source?
.pipe(rename({ extname: '.min.js' })) // rename it
.pipe(buffer()) // must grab a buffer ??
.pipe(sourcemaps.init({ loadMaps: true })) // init sourcemapping
.pipe(uglify()) // uglify
.pipe(sourcemaps.write('./')) // write sourcemap in current dir
.pipe(gulp.dest(distDirs.js)) // pipe to dest
});
})
/**
* Image Tasks
*/
gulp.task('imagemin', function () {
return gulp.src('app/src/img/**/*.+(png|jpg|gif|swg|svg)')
.pipe(cache(imagemin({
gif: {
interlaced: true
}
})))
.pipe(gulp.dest(buildDirs.img))
});
gulp.task('images', () => {
return gulp.src(buildDirs.img + allInDir)
.pipe(gulp.dest(distDirs.img));
});
/**
* Font Tasks
*/
gulp.task('fonts', function () { // TODO: Test dest filepaths
return gulp.src(srcDirs.fonts + allInDir)
.pipe(gulp.dest(distDirs.fonts))
});
/**
* Cleanup Tasks
*/
gulp.task('clean:dist', function () {
return del.sync([distDirs.root + allInDir]);
});
gulp.task('clean:app', () => {
return del.sync(['app/**/*', '!app/src', '!app/src/**/*']); // Leave as is. No vars.
});
/**
* Default & Watch Tasks
*/
gulp.task('default', function (callback) {
runSequence('watch',
['sass', 'vendor', 'babel', 'views', 'browserSync', 'imagemin'],
callback
);
});
gulp.task('watch', function () {
gulp.watch('app/src/scss/**/*.scss', ['sass']); // Watch SCSS Src files and build
gulp.watch('app/src/views/**/*.pug', ['views']); // Watch Pug Files and build
gulp.watch('app/src/js/**/*.js', ['lint', 'babel']); // Watch JS Src files and build
gulp.watch('app/src/img', ['imagemin', browserSync.reload]);
gulp.watch('app/build/**/*', browserSync.reload); // Reload when anything is changed in Build
});
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: 'app/build' // TODO: Find out if this breaks by changing to app/build
}
});
});
/**
* Build Tasks
*/
gulp.task('build', function (callback) {
runSequence('clean:dist',
'views', 'sass',
'css', 'js', 'html-replace', 'images', 'fonts',
'clean:app',
callback);
});
gulp.task('build:prod', function (callback) {
runSequence('clean:dist',
'views', 'sass', 'vendor', 'css', 'bundle',
'html-replace', 'imagemin', 'images', 'fonts',
'clean:app',
callback);
});