-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
300 lines (281 loc) · 7.49 KB
/
gulpfile.babel.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
297
298
299
300
// Dependencies
import autoprefixer from 'gulp-autoprefixer'
import babel from 'gulp-babel'
import cssnano from 'gulp-cssnano'
import concat from 'gulp-concat'
import cond from 'gulp-cond'
import del from 'del'
import eslint from 'gulp-eslint'
import gulp from 'gulp'
import imagemin from 'gulp-imagemin'
import sass from 'gulp-sass'
import sassdoc from 'sassdoc'
import sourcemaps from 'gulp-sourcemaps'
import stylelint from 'gulp-stylelint'
import todo from 'gulp-todo'
import uglify from 'gulp-uglify'
import htmlmin from 'gulp-htmlmin';
import inlinesource from 'gulp-inline-source';
import { argv } from 'yargs'
// If gulp was called in the terminal with the --prod flag, set the node
// environment to production
if (argv.prod) {
process.env.NODE_ENV = 'production'
}
const PROD = process.env.NODE_ENV === 'production'
// Theme directory configuration
const themeDir = './'
const foundationDir = themeDir + 'node_modules/foundation-sites/dist/'
const paths = {
js: `${themeDir}src/js/`,
scss: `${themeDir}src/scss/`,
img: `${themeDir}src/img/`,
dist_js: `${themeDir}js/`,
dist_css: `${themeDir}css/`,
dist_img: `${themeDir}img/`,
}
// JS file configuration, key represents the compiled file name and value being
// an array represents the JS files that should be compiled for that file.
const js = {
'foundation/foundation.core': [
foundationDir + 'js/plugins/foundation.core.js',
],
'foundation/foundation.util.mediaQuery': [
foundationDir + 'js/plugins/foundation.util.mediaQuery.js',
],
'foundation/foundation.util.keyboard': [
foundationDir + 'js/plugins/foundation.util.keyboard.js',
],
'foundation/foundation.util.triggers': [
foundationDir + 'js/plugins/foundation.util.triggers.js',
],
'foundation/foundation.util.box': [
foundationDir + 'js/plugins/foundation.util.box.js',
],
'foundation/foundation.util.touch': [
foundationDir + 'js/plugins/foundation.util.touch.js',
],
'foundation/foundation.util.motion': [
foundationDir + 'js/plugins/foundation.util.motion.js',
],
'foundation/foundation.util.imageLoader': [
foundationDir + 'js/plugins/foundation.util.imageLoader.js',
],
'foundation/foundation.util.timer': [
foundationDir + 'js/plugins/foundation.util.timer.js',
],
'foundation/foundation.util.nest': [
foundationDir + 'js/plugins/foundation.util.nest.js',
],
'foundation/foundation.offcanvas': [
foundationDir + 'js/plugins/foundation.offcanvas.js',
],
'foundation/foundation.tooltip': [
foundationDir + 'js/plugins/foundation.tooltip.js',
],
'foundation/foundation.reveal': [
foundationDir + 'js/plugins/foundation.reveal.js',
],
'foundation/foundation.tabs': [
foundationDir + 'js/plugins/foundation.tabs.js',
],
'foundation/foundation.drilldown': [
foundationDir + 'js/plugins/foundation.drilldown.js',
],
'foundation/foundation.toggler': [
foundationDir + 'js/plugins/foundation.toggler.js',
],
'foundation/foundation.dropdownMenu': [
foundationDir + 'js/plugins/foundation.dropdownMenu.js',
],
'foundation/foundation.dropdown': [
foundationDir + 'js/plugins/foundation.dropdown.js',
],
'foundation/foundation.sticky': [
foundationDir + 'js/plugins/foundation.sticky.js',
],
'foundation/foundation.abide': [
foundationDir + 'js/plugins/foundation.abide.js',
],
'foundation/foundation.magellan': [
foundationDir + 'js/plugins/foundation.magellan.js',
],
'foundation/foundation.smoothScroll': [
foundationDir + 'js/plugins/foundation.smoothScroll.js',
],
'foundation/foundation.accordionMenu': [
foundationDir + 'js/plugins/foundation.accordionMenu.js',
],
'foundation/foundation.accordion': [
foundationDir + 'js/plugins/foundation.accordion.js',
],
'foundation/foundation.slider': [
foundationDir + 'js/plugins/foundation.slider.js',
],
}
/**
* Deletes files & folders in the compilied CSS, JS & image directories.
*/
export const clean = () => {
return del([
paths.dist_js + '**',
paths.dist_css + '**',
paths.dist_img + '**',
themeDir + 'index.html'
]);
}
/**
* Lints CSS files.
*/
export const lintCSS = () => {
return gulp.src(paths.scss + '**/*.scss').pipe(
stylelint({
syntax: 'scss',
failAfterError: false,
reporters: [{ formatter: 'string', console: true }],
})
)
}
/**
* Compiles sass files to CSS.
*/
export const compileCSS = () => {
return gulp
.src([paths.scss + '**/*.scss'])
.pipe(cond(!PROD, sourcemaps.init()))
.pipe(sassdoc())
.pipe(
sass({
includePaths: [
`${themeDir}node_modules/breakpoint-sass/stylesheets`,
`${themeDir}node_modules/foundation-sites/scss`,
`${themeDir}node_modules/motion-ui/src`,
],
}).on('error', sass.logError)
)
.pipe(autoprefixer())
.pipe(cond(!PROD, sourcemaps.write()))
.pipe(cond(PROD, cssnano()))
.pipe(gulp.dest(paths.dist_css))
}
/**
* Watches scss files and triggers the compileCSS task on change.
*/
export const watchSass = () => {
gulp.watch(paths.scss + '**/*.scss', gulp.series(lintCSS, compileCSS))
}
/**
* Lints JS files.
*/
export const lintJS = () => {
return gulp
.src([paths.js + '**/*.js', `${themeDir}gulpfile.babel.js`])
.pipe(eslint())
.pipe(eslint.format())
}
/**
* Compiles JS files.
*/
export const compileJS = done => {
if (Object.keys(js).length) {
for (let key in js) {
gulp
.src(js[key])
.pipe(cond(!PROD, sourcemaps.init()))
.pipe(babel())
.pipe(cond(PROD, uglify()))
.pipe(concat(key + '.js'))
.pipe(cond(!PROD, sourcemaps.write()))
.pipe(gulp.dest(paths.dist_js))
}
}
gulp.src([paths.js + '**/*.js'])
.pipe(cond(!PROD, sourcemaps.init()))
.pipe(babel())
.pipe(cond(PROD, uglify()))
.pipe(cond(!PROD, sourcemaps.write()))
.pipe(gulp.dest(paths.dist_js))
done()
}
/**
* Watches JS files and triggers the JS tasks on change.
*/
export const watchJS = () => {
gulp.watch(paths.js + '**/*.js', gulp.series(lintJS, compileJS))
}
/**
* Minifies images.
*/
export const minImages = () => {
return gulp
.src(paths.img + '**/*')
.pipe(imagemin())
.pipe(gulp.dest(paths.dist_img))
}
/**
* Watches image files and triggers minImages on change.
*/
export const watchImages = () => {
gulp.watch(paths.img + '**/*', minImages)
}
/**
* Generates a TODO report.
*/
export const generateTODO = () => {
return gulp
.src([paths.scss + '**/*.scss', paths.js + '**/*.js'])
.pipe(todo())
.pipe(gulp.dest(themeDir))
}
/**
* Compiles HTML files
*/
export const compileHTML = () => {
return gulp
.src('src/index.html')
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest(themeDir));
}
/**
* Watches HTML file changes
*/
export const watchHTML = () => {
gulp.watch('src/index.html', compileHTML, inlineCSS)
}
/**
* Inline CSS files in HTML
*/
export const inlineCSS = () => {
return gulp.src('index.html')
.pipe(inlinesource())
.pipe(gulp.dest(themeDir));
}
// Runs all build tasks
export const build = (done) => {
gulp.series(
clean,
lintJS,
compileJS,
minImages,
lintCSS,
compileCSS,
generateTODO,
compileHTML,
inlineCSS
)(done);
}
// Runs all build tasks, then watches files for changes to trigger a recompile.
export const watch = (done) => {
gulp.series(
clean,
lintJS,
compileJS,
minImages,
lintCSS,
compileCSS,
generateTODO,
compileHTML,
inlineCSS,
gulp.parallel(watchSass, watchJS, watchImages, watchHTML)
)(done);
}