This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathGulpfile.js
235 lines (198 loc) · 6.3 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
////
// !!RECOMMEND WRITING YOUR OWN GULPFILE FROM SCRATCH!!
//
// Think of tasks as their actions and NOT what they act on.
// Icons, fonts, images are separate groups, but share the same actions.
//
// 1. Configure Folder Paths for groups
// 2. Setup global defaults and flags (default, `--prod`, `gulp watch`, `gulp build`)
// 3. Create tasks
// 3.1. Create wrapper (default) task
// 3.2. Create variations of task with path+configuration passed to reusable generator
// 3.3. Reusable task generator, configuration is abstracted and passed in
////
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')(),
mainBowerFiles = require('main-bower-files'),
opn = require('opn'),
del = require('del');
////// PATH CONFIGURATIONS //////
// NOTE: Refrane from configuring tasks here
// NOTE: `watch` key is optional, falls back to `src`
var paths = {
clean: ['public/'],
rev: {
src: ['app/views/layouts/application.html.erb'],
dest: 'app/views/layouts/'
},
static: {
client: {
src: ['client/static/**'],
dest: 'public/',
},
images: {
src: ['app/assets/images/**'],
dest: 'public/images/',
},
fonts: {
src: ['bower_components/font-awesome/fonts/*'],
dest: 'public/fonts',
}
},
html: {
client: {
src: ['client/**/*.html'],
dest: 'public/',
}
},
styles: {
client: {
src: ['client/modules/townsquared.scss'],
dest: 'public/',
watch: ['client/modules/**/*.scss', 'client/styles/**/*.scss'],
includes: ['app/assets/stylesheets', 'client/modules', 'client/styles', 'bower_components']
}
},
scripts: {
client: {
src: ['client/modules/**/*.js'],
dest: 'public/modules/',
file: 'townsquared.js',
},
vendors: {
src: mainBowerFiles({ filter: '**/*.js' }),
dest: 'public/',
file: 'vendors.js',
watch: ['bower_components/**/*.js']
}
}
};
////// GLOBALS //////
// `--prod` can be used on any task
var isProduction = plugins.util.env.prod;
gulp.task('default', ['build']);
if (isProduction)
gulp.task('build', plugins.sequence('clean', ['static', 'html', 'styles', 'scripts'], 'rev'));
else
gulp.task('build', plugins.sequence('clean', ['static', 'html', 'styles', 'scripts']));
////// WATCH //////
gulp.task('watch', ['build'], function(){
plugins.livereload.listen();
gulp.watch(paths.static.client.watch || paths.static.client.src, ['static:client']);
gulp.watch(paths.static.images.watch || paths.static.images.src, ['static:images']);
gulp.watch(paths.html.client.watch || paths.html.client.src, ['html:client']);
gulp.watch(paths.styles.client.watch || paths.styles.client.src, ['styles:client']);
gulp.watch(paths.scripts.client.watch || paths.scripts.client.src, ['scripts:client']);
});
////// CLEAN //////
gulp.task('clean', ['clean:files', 'clean:rev']);
gulp.task('clean:files', clean(paths.clean));
gulp.task('clean:rev', rev(paths.rev, true));
function clean(paths) {
return function(cb) {
del(paths, cb);
};
}
////// REVISION //////
// TODO: Don't use query params for cachebusting (proxies won't cache them)
gulp.task('rev', rev(paths.rev));
function rev(paths, reset) {
return function() {
return gulp.src(paths.src)
.pipe(plugins.replace(/\?CACHEBUST=(\d+)/g, '?CACHEBUST=' + ( reset ? 0 : Date.now() )))
.pipe(gulp.dest(paths.dest));
};
}
////// STATIC //////
gulp.task('static', ['static:client', 'static:images', 'static:fonts'])
gulp.task('static:client', static(paths.static.client));
gulp.task('static:images', static(paths.static.images));
gulp.task('static:fonts', static(paths.static.fonts));
function static(paths) {
return function() {
return gulp.src(paths.src)
.pipe(gulp.dest(paths.dest))
.pipe(plugins.livereload());
};
}
////// HTML //////
gulp.task('html', ['html:client']);
gulp.task('html:client', static(paths.html.client));
function html(paths) {
return function() {
return gulp.src(paths.src)
.pipe(plugins.plumber({ errorHandler: plugins.notify.onError("Error: <%= error.message %>") }))
.pipe(plugins.if(isProduction,
plugins.angularTemplatecache()
))
.pipe(gulp.dest(paths.dest))
.pipe(plugins.livereload());
};
}
////// STYLES //////
gulp.task('styles', ['styles:client']);
gulp.task('styles:client', styles(paths.styles.client));
function styles(paths) {
return function() {
return gulp.src(paths.src)
.pipe(plugins.if(!isProduction,
plugins.sourcemaps.init()
))
.pipe(plugins.plumber({ errorHandler: plugins.notify.onError("Error: <%= error.message %>\n<%= error.fileName %>:<%= error.lineNumber %>") }))
.pipe(plugins.sass({
includePaths: paths.includes
}))
.pipe(plugins.autoprefixer())
.pipe(plugins.bless())
.pipe(plugins.if(!isProduction,
plugins.sourcemaps.write('.')
))
.pipe(gulp.dest(paths.dest))
// .pipe(plugins.ignore.exclude('**/*.map'))
.pipe(plugins.livereload());
};
}
////// SCRIPTS //////
gulp.task('scripts', ['scripts:client', 'scripts:vendors']);
gulp.task('scripts:client', scripts(paths.scripts.client, true, true));
gulp.task('scripts:vendors', scripts(paths.scripts.vendors));
function scripts(paths, sort, parse) {
return function() {
return gulp.src(paths.src)
.pipe(plugins.if(!isProduction,
plugins.sourcemaps.init()
))
.pipe(plugins.plumber({ errorHandler: plugins.notify.onError("Error: <%= error.message %>") }))
.pipe(plugins.if(parse,
plugins.babel({
blacklist: ["strict"]
})
))
.pipe(plugins.if(sort,
plugins.angularFilesort()
))
.pipe(plugins.concat(paths.file))
// TODO: no annotate due to rumors of issues with ui-router
// .pipe(plugins.if(isProduction,
// plugins.ngAnnotate()
// ))
.pipe(plugins.if(isProduction,
plugins.uglify({ mangle: false })
))
.pipe(plugins.if(!isProduction,
plugins.sourcemaps.write('.')
))
.pipe(gulp.dest(paths.dest))
.pipe(plugins.livereload());
};
}
////// LOCAL SERVER //////
gulp.task('connect', ['build'], function() {
plugins.connect.server({
root: 'public',
livereload: true
});
});
gulp.task('open', ['connect'], function (done) {
opn('http://localhost:8080', done);
});