-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
169 lines (153 loc) · 5.59 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
const { series, parallel, watch } = require('gulp');
const gulp = require('gulp');
const del = require('del');
const fs = require('fs');
const yaml = require('js-yaml');
const spawn = require('cross-spawn');
const yargs = require('yargs');
const sitemap = require('gulp-sitemap');
const gulpif = require('gulp-if');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const imagemin = require('gulp-imagemin');
const browserSync = require('browser-sync');
const hashsum = require('gulp-hashsum');
const config = require('./gulp-tasks/loadGulpConfig'); // import custom js to parse YAML-configuration file (gulpconfig.yml)
// Clean stuff (delete '_site/' dir)
function clean(done) {
del(config.clean); // config.key.other-key references values from gulpconfig.yml
done();
}
// Run Jekll
function jekyllBuild(done) {
browserSync.notify(config.jekyll.notification);
return spawn('jekyll', ['build'], {
stdio: 'inherit'
})
.on('close', done);
}
// Create sitemap on production builds
function gulpSitemap(done) {
const PRODUCTION = !!(yargs.argv.production); // Run things that say 'PRODCUTION' on production builds only ($ gulp --production)
gulp.src((config.sitemap.src), {
read: false
})
.pipe(sitemap({
siteUrl: (config.sitemap.siteUrl),
}))
.pipe(gulpif(PRODUCTION, gulp.dest('./')))
.pipe(gulpif(PRODUCTION, gulp.dest('./_site')));
done();
}
// Compile main.css file from sass modules
function mainScss() {
const PRODUCTION = !!(yargs.argv.production);
return gulp.src(config.sass.src)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError)) // errors shown in terminal for when you screw up your SASS
.pipe(autoprefixer(config.sass.compatibility)) // Automatically prefix any CSS that is not compatible with the browsers defined in the gulpconfig
.pipe(hashsum({filename: './_data/cache_bust_css.yml', hash: 'md5'}))
.pipe(gulpif(PRODUCTION, cssnano({ zindex: false }))) // {zindex:false} to prevent override of z-index values -- higher z-index's are needed in our projects to bring objects above bootstrap's default z-index values
.pipe(gulpif(!PRODUCTION, sourcemaps.write()))
.pipe(gulp.dest(config.sass.dest.jekyllRoot))
.pipe(gulp.dest(config.sass.dest.buildDir))
.pipe(browserSync.stream());
}
// compile 'content.css' which creates custom styles that are available to users the CloudCannon interface.
function cmsScss() {
const PRODUCTION = !!(yargs.argv.production);
return gulp.src(config.cmsScss.src)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer(config.cmsScss.compatibility))
.pipe(gulpif(PRODUCTION, cssnano({ zindex: false }))) // {zindex:false} to prevent override of z-index values -- higher z-index's needed to bring objects above bootstrap's default z-index values
.pipe(gulpif(!PRODUCTION, sourcemaps.write()))
.pipe(gulp.dest(config.cmsScss.dest.jekyllRoot))
.pipe(gulp.dest(config.cmsScss.dest.buildDir))
.pipe(browserSync.stream());
}
// copy static assets/**/* !except for SCSS, CSS, or JS files
// JS is handled by webpack. CSS and SCSS are handled by other gulp tasks.
function copy() {
const PRODUCTION = !!(yargs.argv.production);
browserSync.notify(config.copy.notification);
return gulp.src(config.copy.assets)
.pipe(gulpif(PRODUCTION, imagemin())) // compresses images when gulp --production is run.
.pipe(gulp.dest(config.copy.dist));
}
// Initiate Broswersync
function browserSyncInit(done) {
browserSync.init({
notify: config.browsersync.notify,
open: config.browsersync.open,
port: config.browsersync.port,
server: {
baseDir: config.browsersync.server.basedir
},
xip: config.browsersync.xip,
browser: config.browsersync.browser
});
done();
}
// Reload Browsersync
function browserSyncReload(done) {
browserSync.reload();
done();
}
// Watch the project for file changes ( with Gulp 4's new watch API )
function watchFiles() {
// Watch all-things jekyll related (i.e. *.yml, *.md, *.html, files and jekyll underscored _dir's)
watch(
config.watch.pages,
series(
build,
browserSyncReload
)
);
// Watch for SASS changes in main.scss
watch(
config.watch.sass,
mainScss
);
// Watch for SASS changes in content.scss
watch(
config.watch.sass,
cmsScss
);
// Watchin' for static asset changes (e.g. new images)
watch(
config.watch.images,
series(
copy,
browserSyncReload
)
);
}
// More complex tasks go like this:
// Task lineup (calling functions defined above) for compiling the files that make up the actual static site (in _site dir)
const build = series( // Series items need to be executed in a specific order (new to Gulp 4)
clean,
jekyllBuild,
parallel( // These parallel tasks require the '_site' to be built, but it doesnt really matter what order they execute.
gulpSitemap,
mainScss,
cmsScss,
copy
),
);
// unless you export a task, you cannot call that task from terminal (e.g. `$ gulp build` would run just the '_site' compile)
// Gulp 4 docs call this private vs public tasks. public meaning tasks that are exported and callable from terminal
exports.build = build;
// While the tasks below are commented out, they remain private tasks (i.e. you cannot call `$ gulp watch` from terminal. Why would you need to?)
//exports.browserSync = browserSync;
//exports.watchFiles = watchFiles;
// Define gulp's default task
exports.default = series(
build,
parallel(
browserSyncInit,
watchFiles
)
);