-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.js
296 lines (271 loc) · 7.02 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
/**
* angular-starter-kit
*
* @author Andrea SonnY <[email protected]>
* @copyright 2016 Andrea SonnY <[email protected]>
*
* This code may only be used under the MIT style license.
*
* @license MIT https://andreasonny.mit-license.org/@2016/
*/
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var del = require('del');
var runSequence = require('run-sequence');
var wiredep = require('wiredep').stream;
var path = require('path');
var config = require('./gulp/config.js');
var Server = require('karma').Server;
var browserSync = require('browser-sync').create();
var fallback = require('connect-history-api-fallback');
var args = require('minimist')(process.argv.slice(2));
// Replace '/' with your production base URL
//
// eg. setting baseUrl to '/subdomain/' will write your dist/index.html like this:
// <head><base href="/subdomain/">...
// The default value is set to '/'
//
// for more information: https://docs.angularjs.org/guide/$location
var baseUrl = args.base || '/';
// delete build folder
gulp.task('clean', function(done) {
del([
config.tmp,
config.dist
], {
dot: true
}).then(function(paths) {
console.log('Files and folders that would be deleted:\n', paths.join('\n'));
done();
});
});
gulp.task('server:dev', function() {
browserSync.init({
port: 8000,
open: false,
server: {
baseDir: [config.src, config.tmp],
routes: {
'/bower_components': 'bower_components'
},
middleware: [
fallback({
index: '/index.html',
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'] // systemjs workaround
})
]
}
});
});
gulp.task('server:dist', function() {
browserSync.init({
port: 8000,
open: false,
server: {
baseDir: config.dist,
middleware: [
fallback({
index: '/index.html',
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'] // systemjs workaround
})
]
}
});
});
// reload all Browsers
gulp.task('reload', function() {
gulp
.src(config.src + '/index.html')
.pipe(browserSync.reload);
});
// optimize images
gulp.task('images', function() {
return gulp
.src(config.src + '/images/**/*')
.pipe($.imagemin({
optimizationLevel: 7,
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(config.dist + '/images'))
.pipe($.size({title: 'images'}));
});
// copy fonts
gulp.task('fonts', function() {
gulp
.src([config.src + '/fonts/**/*'])
.pipe(gulp.dest(config.dist + '/fonts'))
.pipe($.size({title: 'fonts'}));
});
// SASS task, will run when any SCSS files change & BrowserSync
// will auto-update browsers
gulp.task('sass', function() {
return gulp
.src(config.src + '/sass/main.scss')
.pipe($.sass({
outputStyle: 'expanded'
}).on('error', $.sass.logError))
.pipe(gulp.dest(config.tmp + '/styles'))
.pipe($.size({title: 'sass'}))
.pipe(browserSync.stream());
});
// SASS Build task
gulp.task('sass:build', function() {
return gulp
.src(config.src + '/sass/**/*.scss')
// .pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'compressed'
}).on('error', $.sass.logError))
.pipe($.cssnano({
autoprefixer: {browsers: config.autoprefixer, add: true},
safe: true
}))
// .pipe($.sourcemaps.write('.'))
.pipe(gulp.dest(config.tmp + '/styles'))
.pipe($.size({title: 'sass'}));
});
gulp.task('scripts', function() {
return gulp
.src([
config.src + '/app/**/*.js',
'!**/test/**/*'
]);
});
// Move all script files in the .temp is required by usemin
// in order to find all the source scripts in one place
gulp.task('copy:scripts', function() {
gulp
.src([config.src + '/app/**/*'])
.pipe(gulp.dest(config.tmp + '/app'));
});
gulp.task('fonts', function() {
gulp
.src(['bower_components/font-awesome/fonts/**/*'])
.pipe(gulp.dest(config.dist + '/fonts'));
});
// Copy the root files from your src folder inside your dist one
gulp.task('copy:root', function() {
gulp
.src([
config.src + '/.htaccess',
config.src + '/404.html',
config.src + '/browserconfig.xml',
config.src + '/favicon.ico',
config.src + '/manifest.json',
config.src + '/manifest.webapp',
config.src + '/robots.txt'
], {
dot: true
})
.pipe(gulp.dest(config.dist))
.pipe($.size({title: 'copy'}));
});
gulp.task('wiredep', function() {
return gulp
.src(config.src + '/index.html')
.pipe(wiredep())
.pipe(gulp.dest(config.src));
});
gulp.task('usemin', ['wiredep'], function() {
return gulp
.src(config.src + '/index.html')
.pipe(gulp.dest(config.dist))
.pipe($.htmlReplace({
baseUrl: '<base href="' + baseUrl + '">',
templates: '<script src="app/templates.js"></script>'
}))
.pipe(gulp.dest(config.dist))
.pipe($.usemin({
css: ['concat', $.cssnano({
autoprefixer: {browsers: config.autoprefixer, add: true}
})],
main: [$.uglify(), 'concat']
}))
.pipe(gulp.dest(config.dist));
});
// minify HTML
gulp.task('htmlmin', function() {
return gulp
.src(config.dist + '/index.html')
.pipe($.htmlmin({
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true,
removeTagWhitespace: true,
collapseWhitespace: true
}))
.pipe(gulp.dest(config.dist));
});
// make a templateCache module from all HTML files
gulp.task('templates', function() {
return gulp
.src('src/app/**/*.html')
.pipe($.htmlmin({
collapseWhitespace: true
}))
.pipe($.angularTemplatecache({
module: 'app',
root: 'app'
}))
.pipe(gulp.dest('.tmp/app'));
});
/**
* watch for file changes
*/
gulp.task('watch', function() {
gulp.watch('src/**/*.html').on('change', browserSync.reload);
gulp.watch('src/app/**/*.js').on('change', browserSync.reload);
gulp.watch('src/sass/**/*.scss', ['sass']);
});
/**
* This task will build your source project in a browser & then use Gulp to watch files.
* When a file is changed, The browser page is automatically refreshed.
*/
gulp.task('serve', function() {
runSequence(
'clean',
'sass',
'wiredep',
'server:dev',
'watch'
);
});
/**
* This task will build your source project in a browser & then use Gulp to watch files.
* When a file is changed, The browser page is automatically refreshed.
*/
gulp.task('serve:dist', function() {
runSequence(
'build',
'server:dist'
);
});
/**
* Build a production version
*
* @param {Function} cb Call back function
* @param {String} base Set a default base url.
*/
gulp.task('build', function(cb) {
runSequence(
'clean',
['images', 'templates', 'copy:scripts', 'copy:root'],
['sass:build', 'fonts'],
'usemin',
'htmlmin',
cb
);
});
/**
* Run tests once and exit
*/
gulp.task('test', function(done) {
new Server({
configFile: path.join(__dirname, '/karma.conf.js'),
singleRun: true
}, done).start();
});
gulp.task('default', ['build']);