-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
223 lines (188 loc) · 6.47 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
var gulp = require('gulp');
var cp = require('child_process');
var runSequence = require('run-sequence').use(gulp);
var autoprefixer = require('gulp-autoprefixer');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var clean = require('gulp-clean');
var browserSync = require('browser-sync');
var plumber = require('gulp-plumber');
var babel = require('gulp-babel');
var bundle = require('gulp-bundle-assets');
var fs = require('fs');
/* ------------------------------------------------------------------------------
-------------------------- Copy tasks ----------------------------------------
----------------------------------------------------------------------------*/
// Copy from the .tmp to _site directory.
// To reduce build times the assets are compiles at the same time as jekyll
// renders the site. Once the rendering has finished the assets are copied.
gulp.task('copy:assets', function (done) {
return gulp.src('.tmp/assets/**')
.pipe(gulp.dest('_site/assets'));
});
/* ------------------------------------------------------------------------------
--------------------------- Assets tasks -------------------------------------
----------------------------------------------------------------------------*/
var sassInput = 'app/assets/styles/*.scss';
var sassOptions = {
includePaths: ['node_modules/foundation-sites/scss','node_modules/font-awesome/scss','.tmp/assets/styles' ],
errLogToConsole: true,
outputStyle: 'expanded'
};
var autoprefixerOptions = {
browsers: ['last 2 versions', 'ie >= 9', 'and_chr >= 2.3']
};
gulp.task('sass', function() {
return gulp.src(sassInput)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer(autoprefixerOptions))
.pipe(sourcemaps.write('.'))
.pipe(browserSync.reload({stream:true}))
.pipe(gulp.dest('.tmp/assets/styles'));
});
gulp.task('compress', function () {
process.env.NODE_ENV = environment;
gulp.src(['./bundle.config.js'])
.pipe(bundle())
.pipe(gulp.dest('.tmp/assets/scripts'))
.on('end', function () {
var dir = fs.readdirSync('.tmp/assets/scripts');
['main', 'vendor'].map((name) => {
const fileToUpdate = dir.filter((file) => {
return file.match(name);
});
fs.renameSync(
'.tmp/assets/scripts/' + fileToUpdate[0],
'.tmp/assets/scripts/' + name + '-microsite.min.js'
);
});
});
});
// Build the jekyll website.
gulp.task('jekyll', function (done) {
var args = ['exec', 'jekyll', 'build'];
switch (environment) {
case 'development':
args.push('--config=_config.yml,_config-dev.yml');
break;
case 'stage':
args.push('--config=_config.yml,_config-stage.yml');
break;
case 'production':
args.push('--config=_config.yml');
break;
}
return cp.spawn('bundle', args, {stdio: 'inherit'})
.on('close', done);
});
// Copies fonts
gulp.task('fonts', function () {
return gulp.src('app/assets/fonts/**/*')
.pipe(gulp.dest('.tmp/assets/styles/fonts'));
});
// Copies images
gulp.task('images', function () {
return gulp.src('app/assets/graphics/**')
.pipe(gulp.dest('.tmp/assets/graphics'));
});
// Build the jekyll website.
// Reload all the browsers.
gulp.task('jekyll:rebuild', ['jekyll'], function () {
browserSync.reload();
});
// Main build task
// Builds the site. Destination --> _site
gulp.task('build', function (done) {
runSequence(['jekyll', 'compress', 'sass', 'images', 'fonts'], ['copy:assets'], done);
});
// tm-project update tasks.
// get-tasks finds our most up to date tasks
// group-tasks groups tasks by country
// update-pages updates pages with new tasks
gulp.task('get-tasks', function () {
return cp.execSync('npm run get-tasks');
});
gulp.task('update-pages', function () {
return cp.execSync('npm run update-pages updates.json');
});
// Default task.
gulp.task('default', function (done) {
runSequence('build', done);
});
gulp.task('serve', ['build'], function () {
browserSync({
port: 3000,
server: {
baseDir: ['.tmp', '_site']
}
});
gulp.watch(['./app/assets/fonts/**/*', './app/assets/images/**/*'], function () {
runSequence('jekyll', 'build', browserReload);
});
gulp.watch('app/assets/styles/**/*.scss', function () {
runSequence('sass');
});
gulp.watch(['./app/assets/scripts/**/*.js', '!./app/assets/scripts/vendor/**/*'], function () {
runSequence('compress', browserReload);
});
gulp.watch(['app/**/*.html', 'app/**/*.md', 'app/**/*.json', 'app/**/*.geojson', '_config*'], function () {
runSequence('jekyll', browserReload);
});
});
gulp.task('update-serve', ['update-build'], function () {
browserSync({
port: 3000,
server: {
baseDir: ['.tmp', '_site']
}
});
gulp.watch(['./app/assets/fonts/**/*', './app/assets/images/**/*'], function () {
runSequence('jekyll', 'build', browserReload);
});
gulp.watch('app/assets/styles/**/*.scss', function () {
runSequence('sass');
});
gulp.watch(['./app/assets/scripts/**/*.js', '!./app/assets/scripts/vendor/**/*'], function () {
runSequence('compress', browserReload);
});
gulp.watch(['app/**/*.html', 'app/**/*.md', 'app/**/*.json', 'app/**/*.geojson', '_config*'], function () {
runSequence('jekyll', browserReload);
});
});
var shouldReload = true;
gulp.task('no-reload', function (done) {
shouldReload = false;
runSequence('serve', done);
});
var environment = 'development';
// gulp.task('prod', function (done) {
// environment = 'production';
// runSequence('clean', 'build', done);
// });
gulp.task('prod', function (done) {
environment = 'production';
runSequence('clean', 'update-tasks-build', done)
});
gulp.task('stage', function (done) {
environment = 'stage';
runSequence('clean', 'update-tasks-build', done);
});
// Removes jekyll's _site folder
gulp.task('clean', function () {
return gulp.src(['_site', '.tmp'], {read: false})
.pipe(clean());
});
// builds site w/page updates
gulp.task('update-tasks-build', function (done) {
runSequence(['get-tasks'], ['update-pages'], ['jekyll', 'compress', 'sass', 'images', 'fonts'], ['copy:assets'], done);
});
/* ------------------------------------------------------------------------------
--------------------------- Helper functions ---------------------------------
----------------------------------------------------------------------------*/
function browserReload () {
if (shouldReload) {
browserSync.reload();
}
}