-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
168 lines (148 loc) · 4.95 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
//** NPM Dependencies **//
var gulp = require('gulp'),
del = require('del'),
include = require('gulp-file-include'),
sass = require('gulp-sass'),
autoprefix = require('gulp-autoprefixer'),
minifyCSS = require('gulp-clean-css'),
stylish = require('jshint-stylish'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
sourcemaps = require('gulp-sourcemaps'),
usemin = require('gulp-usemin'),
imagemin = require('gulp-imagemin'),
htmlmin = require('gulp-htmlmin'),
runSequence = require('run-sequence'),
plumber = require('gulp-plumber'),
browserSync = require('browser-sync'),
reload = browserSync.reload;
//yargs = require('yargs').argv;
//** Path Variables **//
var rootPath = 'app/',
tmpPath = '.tmp/',
distPath = 'public/',
htmlSource = 'app/html/*.html',
htmlIncludesSource = 'app/html/**/*.html',
stylesSource = 'app/styles/**/*.scss',
scriptsSource = 'app/scripts/**/*.js',
imagesSource = 'app/images/**/*',
tmpImagesSource = '.tmp/images/**/*',
fontsSource = 'app/fonts/**/*';
//** Dev Task **//
//Compile HTML includes and copy to tmp folder
gulp.task('html', function() {
return gulp.src(htmlSource)
.pipe(plumber())
.pipe(include({
prefix: '@@',
basepath: '@file'
}))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest(tmpPath))
.pipe(reload({ stream: true }));
});
//Compile and add sourcemap to styles
gulp.task('styles', function () {
return gulp.src(stylesSource)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'compact', errLogToConsole: true }))
.pipe(autoprefix())
.pipe(sourcemaps.write())
.pipe(gulp.dest(tmpPath + '/styles'))
.pipe(browserSync.stream({ match: '**/*.css' }));
});
//Copy jQuery from node_modules to dev
gulp.task('copyJquery', function() {
return gulp.src('node_modules/jquery/dist/jquery.min.js')
.pipe(gulp.dest(tmpPath + '/scripts/vendor'));
});
//Lint scripts
gulp.task('jshint', function () {
return gulp.src([scriptsSource, '!app/scripts/vendor/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.pipe(gulp.dest(tmpPath + 'scripts'))
.pipe(browserSync.stream({ match: '**/*.js' }));
});
//Copy images
gulp.task('copyImages', function() {
return gulp.src(imagesSource)
.pipe(gulp.dest(tmpPath + 'images'));
});
//Optimize Images
//this is a separate task because we don't want to optimize
//our svg files automatically
gulp.task('optimizeImages', function() {
return gulp.src(tmpImagesSource + '*.{gif,jpg,png}')
.pipe(imagemin())
.pipe(gulp.dest(tmpPath + 'images'))
.pipe(reload({ stream: true }));
});
//Copy images to tmp folder then, optimize
gulp.task('images', function(cb) {
runSequence('copyImages', 'optimizeImages', cb);
});
//Fire up a dev server
gulp.task('dev:serve', function() {
browserSync({
server: {
baseDir: [rootPath, tmpPath]
}
});
});
//Runs the dev tasks listed above
gulp.task('dev:build', function(cb) {
runSequence(['html', 'styles', 'copyJquery', 'jshint', 'images'], cb);
});
//Run the dev:build task, fire up a local server, and watch for changes
gulp.task('dev', ['dev:build', 'dev:serve'], function () {
gulp.watch([htmlSource, htmlIncludesSource], ['html']);
gulp.watch(stylesSource, ['styles']);
gulp.watch(scriptsSource, ['jshint']);
gulp.watch(imagesSource, ['copyImages']);
});
//** Production Tasks **//
//Clear out the dist folder before doing a build
gulp.task('prod:clean', function () {
return del.sync(
['public/**', '!public']
);
});
//Minify and add html to dist
gulp.task('prod:html', function() {
return gulp.src(tmpPath + '*.html')
.pipe(gulp.dest(distPath));
});
//Combine scripts and css wrapped in usemin block
gulp.task('prod:useMin', function () {
return gulp.src(tmpPath + '*.html')
.pipe(usemin({
js: [sourcemaps.init(), uglify(), sourcemaps.write()],
css: [minifyCSS(), 'concat']
}))
.pipe(gulp.dest(distPath));
});
//Process images
gulp.task('prod:images', function() {
return gulp.src(tmpImagesSource)
.pipe(gulp.dest(distPath + 'images'));
});
//Process fonts
gulp.task('prod:fonts', function() {
return gulp.src(fontsSource)
.pipe(gulp.dest(distPath + 'fonts'));
});
//create a prod task that runs devbuild then runs the other prod tasks above
gulp.task('prod', function(cb) {
runSequence('prod:clean', 'dev:build', ['prod:useMin', 'prod:html', 'prod:images', 'prod:fonts'], cb);
});
//Fire up a prod server to make sure nothing is broken in the prod code
gulp.task('prod:serve', function () {
browserSync({
server: {
baseDir: distPath
}
});
});