-
Notifications
You must be signed in to change notification settings - Fork 18
/
gulpfile.js
377 lines (330 loc) · 12.9 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// generated on 2016-12-01 using generator-webapp 2.1.0
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const browserSync = require('browser-sync');
const del = require('del');
const wiredep = require('wiredep').stream;
const historyApiFallback = require('connect-history-api-fallback');
const $ = gulpLoadPlugins();
const reload = browserSync.reload;
// Browserify bundle related
const babelify = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const gutil = require('gulp-util');
const livereload = require('gulp-livereload');
const merge = require('merge');
const rename = require('gulp-rename');
const source = require('vinyl-source-stream');
const sourceMaps = require('gulp-sourcemaps');
const watchify = require('watchify');
const uglify = require('gulp-uglify');
const zip = require('gulp-zip');
const runSequence = require('run-sequence');
const servePort = 9003;
gulp.task('styles', () => {
return gulp.src('app/styles/*.scss')
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.sass.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
}).on('error', $.sass.logError))
.pipe($.autoprefixer({browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']}))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/styles'))
.pipe(reload({stream: true}));
});
function lint(files, options) {
return gulp.src(files)
.pipe(reload({stream: true, once: true}))
.pipe($.eslint(options))
.pipe($.eslint.format())
.pipe($.if(!browserSync.active, $.eslint.failAfterError()));
}
gulp.task('lint', () => {
return lint('app/scripts/**/*.js', {
fix: true
})
.pipe(gulp.dest('app/scripts'));
});
gulp.task('lint:test', () => {
return lint('test/spec/**/*.js', {
fix: true,
env: {
mocha: true
}
})
.pipe(gulp.dest('test/spec/**/*.js'));
});
gulp.task('html', ['styles', 'bundleMin'], () => {
return gulp.src('app/*.html')
.pipe($.useref({searchPath: ['.tmp', 'app', '.']}))
.pipe($.if('*.css', $.cssnano({safe: true, autoprefixer: false})))
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe(gulp.dest('dist'));
});
gulp.task('views', () => {
return gulp.src('app/views/*.html')
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe(gulp.dest('dist/views'));
});
gulp.task('images', () => {
return gulp.src('app/images/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true,
// don't remove IDs from SVGs, they are often used
// as hooks for embedding and styling
svgoPlugins: [{cleanupIDs: false}]
})))
.pipe(gulp.dest('dist/images'));
});
gulp.task('images-lite', () => {
return gulp.src('app/images/**/*')
.pipe(gulp.dest('dist/images'));
});
gulp.task('fonts', () => {
return gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', function (err) {})
.concat('app/fonts/**/*'))
.pipe(gulp.dest('.tmp/fonts'))
.pipe(gulp.dest('dist/fonts'));
});
gulp.task('extras', () => {
return gulp.src([
'app/*.*',
'!app/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'));
});
gulp.task('clean', del.bind(null, ['.tmp', 'dist']));
gulp.task('serve', ['styles', 'bundle', 'fonts'], () => {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
middleware: [ historyApiFallback() ],
routes: {
'/bower_components': 'bower_components'
}
}
});
gulp.watch([
'app/*.html',
'app/images/**/*',
'.tmp/fonts/**/*'
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/styles/library/**/*.scss', ['compileLibrary']);
gulp.watch('app/scripts/**/*.js', ['bundleServe']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});
gulp.task('serve:dist', () => {
browserSync({
notify: false,
port: servePort,
server: {
baseDir: ['dist']
}
});
});
gulp.task('serve:test', ['bundle'], () => {
browserSync({
notify: false,
port: 9000,
ui: false,
server: {
baseDir: 'test',
routes: {
'/scripts': '.tmp/scripts',
'/bower_components': 'bower_components'
}
}
});
gulp.watch('app/scripts/**/*.js', ['bundle']);
gulp.watch('test/spec/**/*.js').on('change', reload);
gulp.watch('test/spec/**/*.js', ['lint:test']);
});
// inject bower components
gulp.task('wiredep', () => {
gulp.src('app/styles/*.scss')
.pipe(wiredep({
ignorePath: /^(\.\.\/)+/
}))
.pipe(gulp.dest('app/styles'));
gulp.src('app/*.html')
.pipe(wiredep({
ignorePath: /^(\.\.\/)*\.\./
}))
.pipe(gulp.dest('app'));
});
// // Removing lint from build task
// gulp.task('build', ['lint', 'html', 'images', 'fonts', 'extras'], () => {
// return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
// });
gulp.task('build', ['html', 'images', 'fonts', 'extras', 'views', 'buildLibrary'], () => {
return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
});
gulp.task('build-lite', ['html', 'images-lite', 'fonts', 'extras', 'views'], () => {
return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
});
gulp.task('default', ['clean'], () => {
gulp.start('build');
});
/////// Begin browserify bundling
var config = {
js: {
src: './app/scripts/app.js', // Entry point
outputDir: './app/scripts/', // Directory to save bundle to
mapDir: '../../maps/', // Subdirectory to save maps to
buildOutputDir: './dist/scripts', // Output to dist
outputFile: 'bundle.js' // Name to use for bundle
},
};
// Watch task: Bundle, kick off live reload server, nd rebundle/reload on file changes
gulp.task('watch', function () {
livereload.listen();
var args = merge(watchify.args, { debug : true});
var bundler = browserify(config.js.src, args)
.plugin(watchify, { ignoreWatch: ['**/node_modules'] })
.transform(babelify, { presets : [ 'es2015' ] });
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(sourceMaps.init({ loadMaps : true })) // Strip inline source maps
.on('error', gutil.log)
.pipe(sourceMaps.write(config.js.mapDir)) // Save source maps to their own directory
.pipe(gulp.dest(config.js.outputDir)) // Save 'bundle' to build/
.pipe(reload({stream: true}));
bundler.on('update', function () {
// Add options to add to "base" bundler passed as parameter
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(sourceMaps.init({ loadMaps : true })) // Strip inline source maps
.pipe(sourceMaps.write(config.js.mapDir)) // Save source maps to their own directory
.pipe(gulp.dest(config.js.outputDir)) // Save 'bundle' to build/
// .pipe(livereload()); // Reload browser if relevant
});
});
gulp.task('bundle', function () {
var bundler = browserify(config.js.src) // Pass browserify the entry point
.transform(babelify, { presets : [ 'es2015' ] }); // Then, babelify, with ES2015 preset
// Add options to add to "base" bundler passed as parameter
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(sourceMaps.init({ loadMaps : true })) // Strip inline source maps
.pipe(sourceMaps.write(config.js.mapDir)) // Save source maps to their own directory
.pipe(gulp.dest(config.js.outputDir)) // Save 'bundle' to build/
// .pipe(livereload()); // Reload browser if relevant
})
gulp.task('bundleReload', function () {
var args = merge(watchify.args, { debug : true});
var bundler = browserify(config.js.src, args)
.plugin(watchify, { ignoreWatch: ['**/node_modules'] })
.transform(babelify, { presets : [ 'es2015' ] });
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(sourceMaps.init({ loadMaps : true })) // Strip inline source maps
.on('error', gutil.log)
.pipe(sourceMaps.write(config.js.mapDir)) // Save source maps to their own directory
.pipe(gulp.dest(config.js.outputDir)) // Save 'bundle' to build/
.pipe(reload({stream: true}));
});
gulp.task('bundleServe', function () {
var args = merge(watchify.args, { debug : true});
var bundler = browserify(config.js.src, args)
.plugin(watchify, { ignoreWatch: ['**/node_modules'] })
.transform(babelify, { presets : [ 'es2015' ] });
// Add options to add to "base" bundler passed as parameter
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(sourceMaps.init({ loadMaps : true })) // Strip inline source maps
.pipe(sourceMaps.write(config.js.mapDir)) // Save source maps to their own directory
.pipe(gulp.dest(config.js.outputDir)) // Save 'bundle' to build/
.pipe(reload({stream: true}));
});
gulp.task('bundleMin', function () {
var bundler = browserify(config.js.src)
.transform(babelify, { presets : [ 'es2015' ], comments : true, compact: false });
bundler
.bundle() // Start bundle
.pipe(source(config.js.src)) // Entry point
.pipe(buffer()) // Convert to gulp pipeline
// .pipe(ngAnnotate()) // ng-annotate to enable uglification of services injected
// .pipe(uglify())
.pipe(rename(config.js.outputFile)) // Rename output from 'main.js' to 'bundle.js'
.pipe(gulp.dest(config.js.buildOutputDir)) // Save 'bundle' to build/
})
/////// End browserify bundling
/////////// Start manual build browserify task
gulp.task('buildbundle', function(){
var exec = require('child_process').exec;
// var cmd = 'browserify app/scripts/main.js > app/scripts/bundle.js';
// var cmd = 'browserify app/scripts/main.js --debug | exorcist app/scripts/bundle.map.js > app/scripts/bundle.js';
var cmd = 'watchify app/scripts/main.js -o app/scripts/bundle.js -v';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
});
});
/////////// End manual browserify task
////////////////////////////////////////////////////
// Sass Flexbox library build tasks //
////////////////////////////////////////////////////
// Clean public folder
gulp.task('cleanLibrary', del.bind(null, ['public/sass-flexbox/**/*']));
// Copy sass library to public folder
gulp.task('copyLibrary', () => {
return gulp.src('app/styles/library/**/*.scss')
.pipe($.plumber())
.pipe(gulp.dest('public/sass-flexbox/scss'))
});
/// process scss and compile library for public use
gulp.task('compileLibrary', () => {
return gulp.src('app/styles/library/*.scss')
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.sass.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
}).on('error', $.sass.logError))
.pipe($.autoprefixer({browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']}))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('public/sass-flexbox'))
});
// Minify library for public use
gulp.task('minifyLibrary', () => {
return gulp.src('public/sass-flexbox/main.css')
.pipe($.plumber())
.pipe(rename('main.min.css'))
.pipe($.if('*.css', $.cssnano({safe: true, autoprefixer: false})))
.pipe(gulp.dest('public/sass-flexbox'))
});
// zip library
gulp.task('zipLibrary', () => {
return gulp.src('public/sass-flexbox/**/*')
.pipe(zip('sass-flexbox.zip'))
.pipe(gulp.dest('public'))
});
// Copy, compile, minify, zip
gulp.task('buildLibrary', ['cleanLibrary'], function() {
runSequence('copyLibrary', 'compileLibrary', 'minifyLibrary', 'zipLibrary');
});