-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
246 lines (217 loc) · 5.84 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
241
242
243
244
245
246
/*
* * * * * ==============================
* * * * * ==============================
* * * * * ==============================
* * * * * ==============================
========================================
========================================
========================================
----------------------------------------
USWDS SASS GULPFILE
----------------------------------------
*/
const autoprefixer = require("autoprefixer");
const csso = require("postcss-csso");
const gulp = require("gulp");
const pkg = require("./node_modules/uswds/package.json");
const postcss = require("gulp-postcss");
const replace = require("gulp-replace");
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require("gulp-sourcemaps");
const uswds = require("./node_modules/uswds-gulp/config/uswds");
const del = require("del");
const svgSprite = require("gulp-svg-sprite");
const rename = require("gulp-rename");
const connect = require('gulp-connect');
const fileinclude = require('gulp-file-include');
/*
----------------------------------------
PATHS
----------------------------------------
- All paths are relative to the
project root
- Don't use a trailing `/` for path
names
----------------------------------------
*/
// Project Sass source directory
const PROJECT_SASS_SRC = "./assets/sass";
const PROJECT_HTML_SRC = "./assets/html";
const PROJECT_IMG_SRC = "./assets/img";
const PROJECT_JS_SRC = "./assets/js";
// Images destination
const IMG_DEST = "./dist/assets/img";
// Fonts destination
const FONTS_DEST = "./dist/assets/fonts";
// Javascript destination
const JS_DEST = "./dist/assets/js";
// HTML destination
const HTML_DEST = "./dist";
// Compiled CSS destination
const CSS_DEST = "./dist/assets/css";
// Site CSS destination
// Like the _site/assets/css directory in Jekyll, if necessary.
// If using, uncomment line 106
const SITE_CSS_DEST = "./path/to/site/css/destination";
/*
----------------------------------------
TASKS
----------------------------------------
*/
gulp.task("copy-uswds-setup", () => {
return gulp
.src(`${uswds}/scss/theme/**/**`)
.pipe(gulp.dest(`${PROJECT_SASS_SRC}`));
});
gulp.task("copy-uswds-fonts", () => {
return gulp.src(`${uswds}/fonts/**/**`).pipe(gulp.dest(`${FONTS_DEST}`));
});
gulp.task("copy-uswds-images", () => {
return gulp.src(`${uswds}/img/**/**`).pipe(gulp.dest(`${IMG_DEST}`));
});
gulp.task("copy-uswds-js", () => {
return gulp.src(`${uswds}/js/**/**`).pipe(gulp.dest(`${JS_DEST}`));
});
gulp.task("copy-html", () => {
return gulp.src(`${PROJECT_HTML_SRC}/*.html`)
.pipe(fileinclude({
prefix: '@@',
basepath: `${PROJECT_HTML_SRC}/partials`
}))
.pipe(gulp.dest(`${HTML_DEST}`))
.pipe(connect.reload());
});
gulp.task("copy-img", () => {
return gulp.src(`${PROJECT_IMG_SRC}/*.*`)
.pipe(gulp.dest(`${IMG_DEST}`))
.pipe(connect.reload());
});
gulp.task("copy-js", () => {
return gulp.src(`${PROJECT_JS_SRC}/*.js`)
.pipe(gulp.dest(`${JS_DEST}`))
.pipe(connect.reload());
});
gulp.task("build-sass", function (done) {
var plugins = [
// Autoprefix
autoprefixer({
cascade: false,
grid: true,
}),
// Minify
csso({ forceMediaMerge: false }),
];
return (
gulp
.src([`${PROJECT_SASS_SRC}/*.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("."))
// uncomment the next line if necessary for Jekyll to build properly
//.pipe(gulp.dest(`${SITE_CSS_DEST}`))
.pipe(gulp.dest(`${CSS_DEST}`))
.pipe(connect.reload())
);
});
// SVG sprite configuration
config = {
shape: {
dimension: {
// Set maximum dimensions
maxWidth: 24,
maxHeight: 24,
},
id: {
separator: "-",
},
spacing: {
// Add padding
padding: 0,
},
},
mode: {
symbol: true, // Activate the «symbol» mode
},
};
gulp.task("build-sprite", function (done) {
gulp
.src(`${IMG_DEST}/usa-icons/**/*.svg`, {
allowEmpty: true,
})
.pipe(svgSprite(config))
.on("error", function (error) {
console.log("There was an error");
})
.pipe(gulp.dest(`${IMG_DEST}`))
.on("end", function () {
done();
});
});
gulp.task("rename-sprite", function (done) {
gulp
.src(`${IMG_DEST}/symbol/svg/sprite.symbol.svg`, {
allowEmpty: true,
})
.pipe(rename(`${IMG_DEST}/sprite.svg`))
.pipe(gulp.dest(`./`))
.on("end", function () {
done();
});
});
gulp.task("clean-sprite", function (cb) {
cb();
return del.sync(`${IMG_DEST}/symbol`);
});
gulp.task('connect', function() {
connect.server({
root: 'dist',
livereload: true
});
});
gulp.task(
"init",
gulp.series(
"copy-uswds-setup",
"copy-uswds-fonts",
"copy-uswds-images",
"copy-uswds-js",
"copy-html",
"copy-img",
"copy-js",
"build-sass"
)
);
gulp.task(
"build",
gulp.series(
"copy-uswds-fonts",
"copy-uswds-images",
"copy-uswds-js",
"copy-html",
"copy-img",
"copy-js",
"build-sass"
)
);
gulp.task("watch-source", function () {
gulp.watch(`${PROJECT_SASS_SRC}/**/*.scss`, gulp.series("build-sass"));
gulp.watch(`${PROJECT_HTML_SRC}/**/*.html`, gulp.series("copy-html"));
gulp.watch(`${PROJECT_IMG_SRC}/**/*.*`, gulp.series("copy-img"));
gulp.watch(`${PROJECT_JS_SRC}/**/*.js`, gulp.series("copy-js"));
});
gulp.task("watch", gulp.parallel("connect", gulp.series("build-sass", "copy-html", "copy-img", "copy-js", "watch-source")));
gulp.task("default", gulp.series("watch"));
gulp.task(
"svg-sprite",
gulp.series("build-sprite", "rename-sprite", "clean-sprite")
);