forked from usds/html-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
240 lines (208 loc) · 6.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
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
/*jshint node:true, esversion: 6 */
/*
* * * * * ==============================
* * * * * ==============================
* * * * * ==============================
* * * * * ==============================
========================================
========================================
========================================
----------------------------------------
USWDS SASS GULPFILE
----------------------------------------
*/
"use strict";
const autoprefixer = require("autoprefixer");
const browsersync = require("browser-sync");
const cp = require("child_process");
const csso = require("postcss-csso");
const del = require("del");
const gulp = require("gulp");
const log = require("fancy-log");
const notify = require("gulp-notify");
const pkg = require("./node_modules/uswds/package.json");
const postcss = require("gulp-postcss");
const replace = require("gulp-replace");
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const uswds = require("./node_modules/uswds-gulp/config/uswds");
const config = require("./config");
const bSConfig = config.browsersync.development;
const env = process.env.NODE_ENV || "prod";
const watchConfig = config.watch;
/*
----------------------------------------
PATHS
----------------------------------------
- All paths are relative to the project root
- Don't use a trailing `/` for path names
----------------------------------------
*/
// Project Sass source directory
// As a user of this repo, this is where you make style changes
const PROJECT_SASS_SRC = './assets/uswds-theme';
// This is where the html-prototype application stylesheets live
// As a user of this repo, you can change these but should not have to
const PROTOTYPE_SASS_SRC = './assets/stylesheets';
// USWDS Sass source directory
const PROJECT_USWDS_SASS_SRC = './assets/uswds-sass';
// Images destination
const IMG_DEST = './assets/img';
// Fonts destination
const FONTS_DEST = './assets/fonts';
// Javascript destination
const JS_DEST = './assets/js/vendor';
// Compiled CSS destinations
// 1. Compile uswds-theme/styles.scss
// 2. That is imported by stylesheets/application.scss which generates application.css
// 3. application.css is what is consumed by the pages of the application being built
// 4. In order for browserSync to work, we must drop the application.css in the _site directory
const PROJECT_SASS_DEST = './assets/stylesheets';
const PROJECT_SASS_SITE_DEST = './_site/assets/stylesheets';
/*
----------------------------------------
FUNCTIONS
----------------------------------------
*/
function browserSync(done) {
browsersync.init(bSConfig);
done();
}
// Reload task, that is used by jekyll-rebuild
function browserSyncReload(done) {
browsersync.reload();
done();
}
// Clean assets
function clean() {
return del([
"./_site/assets/",
`${PROJECT_SASS_DEST}/*.css`,
`${PROJECT_SASS_DEST}/*.css.map`
]);
}
function css() {
var plugins = [
autoprefixer({
cascade: false,
grid: true
}),
// Minify
csso(({ forcedMediaMerge: false }))
];
return (
gulp
.src([`${PROJECT_SASS_SRC}/styles.scss`, `${PROTOTYPE_SASS_SRC}/application.scss`])
.pipe(sourcemaps.init({ largeFile: true }))
.pipe(
sass.sync({
includePaths: [
`${PROJECT_SASS_SRC}`,
`${uswds}/scss`,
`${uswds}/scss/packages`,
]
})
)
.pipe(replace(/\buswds @version\b/g, 'based on uswds v' + pkg.version))
.pipe(postcss(plugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${PROJECT_SASS_DEST}`))
.pipe(gulp.dest(`${PROJECT_SASS_SITE_DEST}`))
.pipe(notify({
"sound": "Pop" // case sensitive
}))
);
}
function jekyll(done) {
log("Running buildJekyll");
if (env === "prod") {
log("Building for production");
return cp
.spawn("bundle", ["exec", "jekyll", "build"], { stdio: "inherit" })
.on("close", done);
} else {
log("Building for development");
return cp
.spawn(
"bundle",
["exec", "jekyll", "build", "--config", "_config.yml,_config-dev.yml"],
{ stdio: "inherit" }
)
.on("close", done);
}
}
function watchFiles() {
gulp.watch(watchConfig.jekyll, gulp.series(jekyll, browserSyncReload));
gulp.watch(
[
`${PROTOTYPE_SASS_SRC}/application.scss`,
`${PROTOTYPE_SASS_SRC}/base/_base.scss`,
`${PROTOTYPE_SASS_SRC}/base/_forms.scss`,
`${PROTOTYPE_SASS_SRC}/_variables.scss`,
`${PROJECT_SASS_SRC}/*.scss`,
],
gulp.series(css, browserSyncReload)
);
}
/*
----------------------------------------
TASKS
----------------------------------------
*/
const copyUSWDSSetup = () => {
return gulp.src(`${uswds}/scss/theme/**/**`)
.pipe(gulp.dest(`${PROJECT_SASS_SRC}`));
}
const copyUSWDSCore = () => {
return gulp.src(`${uswds}/scss/core/**/**`)
.pipe(gulp.dest(`${PROJECT_USWDS_SASS_SRC}/core`));
}
const copyUSWDSLib = () => {
return gulp.src(`${uswds}/scss/lib/**/**`)
.pipe(gulp.dest(`${PROJECT_USWDS_SASS_SRC}/lib`));
}
const copyUSWDSSettings = () => {
return gulp.src(`${uswds}/scss/settings/**/**`)
.pipe(gulp.dest(`${PROJECT_USWDS_SASS_SRC}/settings`));
}
const copyUSWDSFonts = () => {
return gulp.src(`${uswds}/fonts/**/**`)
.pipe(gulp.dest(`${FONTS_DEST}`));
}
const copyUSWDSImages = () => {
return gulp.src(`${uswds}/img/**/**`)
.pipe(gulp.dest(`${IMG_DEST}`));
}
const copyUSWDSJS = () => {
return gulp.src(`${uswds}/js/**/**`)
.pipe(gulp.dest(`${JS_DEST}`));
}
// Define complex tasks
const build = gulp.series(clean, gulp.parallel(css, jekyll));
const init = gulp.series(
copyUSWDSSetup,
copyUSWDSCore,
copyUSWDSLib,
copyUSWDSSettings,
copyUSWDSFonts,
copyUSWDSImages,
copyUSWDSJS,
css
);
const update = gulp.series(
copyUSWDSCore,
copyUSWDSLib,
copyUSWDSSettings,
copyUSWDSFonts,
copyUSWDSImages,
copyUSWDSJS,
css
);
const watch = gulp.parallel(watchFiles, browserSync);
// Export tasks
exports.css = css;
exports.init = init;
exports.jekyll = jekyll;
exports.build = build;
exports.watch = watch;
exports.default = build;