-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
186 lines (169 loc) · 5.33 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
/**
* ===========================================================
* Weather Widget Frontend files: ASSET PIPELINE
* ===========================================================
* @author Andres Osorio <androideosorio@me.com>
* @license MIT
*/
/**
* require all necessary gulp plugins and additional helper libraries
*/
const gulp = require('gulp');
const $ = require('gulp-load-plugins')({ camelCase: true, lazy: true });
var help = require('gulp-help-docs');
const wiredep = require('wiredep').stream;
const del = require('del');
const runSequence = require('run-sequence');
// Config variable can be empty or come with predefined docs
var helpConfig = require('./package.json')['helpDocs'] || {};
// postCSS processors
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
// ------------------------------------------------------------------------------------
/* ------------------------------------ *
* Pipeline configuration
* ------------------------------------ */
/**
* paths configuration used throughout the build process
* @type {Object}
*/
const paths = {
src: {
root: './src',
index: './src/index.html',
styles: './src/sass/app.scss',
images: './src/images/**/*',
videos: './src/videos/**/*'
},
dist: {
root: './dist',
styles: './dist/styles',
scripts: './dist/scripts',
images: './dist/images',
videos: './dist/videos'
}
};
/**
* postCSS preprocessors for optimizing CSS compiled files
* @type {Array}
*/
const preprocessors = [
autoprefixer({ browsers: ['last 3 versions'] }),
cssnano()
];
/**
* configure wiredep to fetch the correct information from bower
* @type {Object}
*/
const wiredepConfig = {
bowerJson: require('./bower.json')
};
// ------------------------------------------------------------------------------------
/* ------------------------------------ *
* Assets Tasks
* ------------------------------------ */
/**
* compile SASS sources to a minified and concatenated CSS file.
* export additional sourcemaps for development
*/
help.task('styles', 'compile SASS files into final CSS files', helpConfig);
gulp.task('styles', () => {
return gulp.src(paths.src.styles)
.pipe($.sass())
.pipe($.sourcemaps.init())
.pipe($.postcss(preprocessors))
.pipe($.rename({ extname: '.min.css' }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(paths.dist.styles))
.pipe($.connect.reload());
});
/**
* wire up script dependencies and build script files based
* on useref configuration defined in the main index file.
*
* Additionally, minify the HTML output
*/
help.task('inject', 'compile dependencies and application scripts, and inject them into the final index file', helpConfig);
gulp.task('inject', () => {
return gulp.src(paths.src.index)
.pipe(wiredep(wiredepConfig))
.pipe($.useref())
.pipe($.if('*.js', $.ngAnnotate()))
.pipe($.if('*.js', $.uglify()))
.pipe($.if('*.css', $.postcss(preprocessors)))
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe(gulp.dest(paths.dist.root))
.pipe($.connect.reload());
});
/**
* copy and optimize images in the project
*/
gulp.task('assets:images', () => {
return gulp.src(paths.src.images)
.pipe(gulp.dest(paths.dist.images))
.pipe($.connect.reload());
});
/**
* copy and optimize media files for web: video sources
*/
gulp.task('assets:videos', () => {
return gulp.src(paths.src.videos)
.pipe(gulp.dest(paths.dist.videos))
.pipe($.connect.reload());
});
// ------------------------------------------------------------------------------------
/* ------------------------------------ *
* Helper Tasks
* ------------------------------------ */
/**
* clean up the build destination directory by deleting it
*/
help.task('clean:dist', 'Clean destination (build) directory', helpConfig);
gulp.task('clean:dist', () => {
return del(paths.dist.root);
});
/**
* open a development server at localhost:8080
* with live reload enabled
*/
gulp.task('connect', () => {
return $.connect.server({
root: paths.dist.root,
livereload: true
});
});
// ------------------------------------------------------------------------------------
/**
* Watch tasks used in development for automatic re-compilation
*/
gulp.task('watch', () => {
gulp.watch('./src/sass/**/*.scss', ['styles']);
gulp.watch('./src/index.html', ['inject']);
gulp.watch('./src/js/**/*.js', ['inject']);
gulp.watch('./src/images/*', ['assets:images']);
gulp.watch('./src/videos/*', ['assets:videos']);
});
// ------------------------------------------------------------------------------------
/* ------------------------------------ *
* main Tasks
* ------------------------------------ */
/**
* build the project into their final HTML/CSS/ JS forms
*/
help.task('build', 'Default task for building production assets', helpConfig);
gulp.task('build', (done) => {
runSequence('clean:dist', [
'styles',
'inject',
'assets:images',
'assets:videos'
], done);
});
/**
* Default task
* build the project, start a development server in the build directory
* with live reload enabled and add watchers to the source files
* to reload the development server
*/
help.task('default', 'Default for setting up a local development environment', helpConfig);
gulp.task('default', (done) => runSequence('build', ['connect', 'watch'], done));