-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
211 lines (176 loc) · 5.15 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
const fs = require('fs');
const browserSync = require('browser-sync').create();
const gulp = require('gulp');
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const include = require('gulp-include');
const eslint = require('gulp-eslint-new');
const isFixed = require('gulp-eslint-if-fixed');
const babel = require('gulp-babel');
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('sass'));
const sassLint = require('gulp-sass-lint');
const gulpif = require('gulp-if');
const uglify = require('gulp-uglify');
const merge = require('merge');
const exec = require('child_process').exec;
let config = {
src: {
scssPath: './src/scss',
jsPath: './src/js'
},
dist: {
cssPath: './bar/css',
jsPath: './bar/js',
deployPath: './dist',
},
compilerPath: './src',
packagesPath: './node_modules',
sync: false,
syncTarget: 'http://localhost/'
};
/* eslint-disable no-sync */
if (fs.existsSync('./gulp-config.json')) {
const overrides = JSON.parse(fs.readFileSync('./gulp-config.json'));
config = merge(config, overrides);
}
/* eslint-enable no-sync */
//
// Helper functions
//
// Base SCSS linting function
function lintSCSS(src) {
return gulp.src(src)
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
}
// Base SCSS compile function
function buildCSS(src, dest) {
dest = dest || config.dist.cssPath;
return gulp.src(src)
.pipe(sass({
includePaths: [config.src.scssPath, config.packagesPath]
})
.on('error', sass.logError))
.pipe(cleanCSS())
.pipe(autoprefixer({
// Supported browsers added in package.json ("browserslist")
cascade: false
}))
.pipe(gulp.dest(dest));
}
// Base JS linting function (with eslint). Fixes problems in-place.
function lintJS(src, dest) {
dest = dest || config.src.jsPath;
return gulp.src(src)
.pipe(eslint({
fix: true
}))
.pipe(eslint.format())
.pipe(isFixed(dest));
}
// Base JS compile function
function buildJS(src, dest, destName, minify = true) {
dest = dest || config.dist.jsPath;
return gulp.src(src)
.pipe(include({
includePaths: [config.packagesPath, config.src.jsPath]
}))
.on('error', console.log) // eslint-disable-line no-console
.pipe(babel())
.pipe(gulpif(minify, uglify({
mangle: {
reserved: ['UCFHB_VERSION', 'UCFHB_GA_ACCOUNT', 'UCFHB_ROOT_URL', '_gaq']
}
})))
.pipe(rename(destName))
.pipe(gulp.dest(dest));
}
// BrowserSync reload function
function serverReload(done) {
if (config.sync) {
browserSync.reload();
}
done();
}
// BrowserSync serve function
function serverServe(done) {
if (config.sync) {
browserSync.init({
proxy: {
target: config.syncTarget
}
});
}
done();
}
//
// CSS
//
// Lint all scss files
gulp.task('scss-lint', () => {
return lintSCSS(`${config.src.scssPath}/*.scss`);
});
// Compile main bar stylesheet
gulp.task('scss-build-bar', () => {
return buildCSS(`${config.src.scssPath}/bar.scss`);
});
// Compile main bar stylesheet
gulp.task('scss-build-bootstrap2', () => {
return buildCSS(`${config.src.scssPath}/bar-bootstrap.scss`);
});
// Compile 1200px breakpoint stylesheet
gulp.task('scss-build-1200bp', () => {
return buildCSS(`${config.src.scssPath}/1200-breakpoint.scss`);
});
// All theme css-related tasks
gulp.task('css', gulp.series('scss-lint', 'scss-build-bar', 'scss-build-bootstrap2', 'scss-build-1200bp'));
//
// JavaScript
//
// Run eslint on the root bar.js file
gulp.task('es-lint', () => {
return lintJS([`${config.src.jsPath}/bar.js`], config.src.jsPath);
});
// Process js files (does not inject environment-specific variables)
gulp.task('js-build-header', () => {
return buildJS(`${config.src.jsPath}/bar.js`, config.src.jsPath, 'university-header.js');
});
gulp.task('js-build-header-full', () => {
return buildJS(`${config.src.jsPath}/bar.js`, config.src.jsPath, 'university-header-full.js', false);
});
// Inject environment-specific variables via compile.sh, and
// save out finalized files to bar/js/
gulp.task('js-inject-vars', (done) => {
exec(`${config.compilerPath}/compile.sh`, (code, stdout, stderr) => {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
done(code);
});
});
// All js-related tasks
gulp.task('js', gulp.series('es-lint', 'js-build-header', 'js-build-header-full', 'js-inject-vars'));
gulp.task('move-homepage', () => {
return gulp.src(['./index.html', './robots.txt'])
.pipe(gulp.dest(config.dist.deployPath));
});
gulp.task('move-assets', () => {
return gulp.src('./bar/**/*')
.pipe(gulp.dest(`${config.dist.deployPath}/bar`));
});
gulp.task('move-deploy', gulp.series('move-homepage', 'move-assets'));
//
// Rerun tasks when files change
//
gulp.task('watch', (done) => {
serverServe(done);
gulp.watch(`${config.src.scssPath}/**/*.scss`, gulp.series('css', serverReload));
gulp.watch(`${config.src.jsPath}/**/*.js`, gulp.series('js', serverReload));
gulp.watch('./index.html', gulp.series(serverReload));
});
//
// Default task
//
gulp.task('default', gulp.series('css', 'js'));