-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgulpfile.js
296 lines (269 loc) · 7.54 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
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
const autoprefixer = require("autoprefixer");
const csso = require("postcss-csso");
const { src, dest, series, parallel, watch } = require("gulp");
const path = require("path");
const postcss = require("gulp-postcss");
const replace = require("gulp-replace");
const sass = require("gulp-sass")(require("sass-embedded"));
const del = require("del");
const svgSprite = require("gulp-svgstore");
const rename = require("gulp-rename");
const log = console.log;
const colors = {
red: "\x1b[31m%s\x1b[0m",
blue: "\x1b[34m%s\x1b[0m",
yellow: "\x1b[33m%s\x1b[0m",
};
/*
----------------------------------------
SETTINGS
----------------------------------------
*/
let settings = {
version: 2,
compile: {
paths: {
src: {
uswds: null,
sass: null,
theme: null,
fonts: null,
img: null,
js: null,
projectSass: "./sass",
projectIcons: "",
defaults: {
v2: {
uswds: "./node_modules/uswds/dist",
sass: "./node_modules/uswds/dist/scss",
theme: "./node_modules/uswds/dist/scss/theme",
fonts: "./node_modules/uswds/dist/fonts",
img: "./node_modules/uswds/dist/img",
js: "./node_modules/uswds/dist/js",
},
v3: {
uswds: "./node_modules/@uswds",
sass: "./node_modules/@uswds/uswds/packages",
theme: "./node_modules/@uswds/uswds/dist/theme",
fonts: "./node_modules/@uswds/uswds/dist/fonts",
img: "./node_modules/@uswds/uswds/dist/img",
js: "./node_modules/@uswds/uswds/dist/js",
},
},
},
/**
* ? project paths
* - all paths are relative to the project root
*/
dist: {
theme: "./sass",
img: "./assets/uswds/img",
fonts: "./assets/uswds/fonts",
js: "./assets/uswds/js",
css: "./assets/uswds/css",
},
},
browserslist: ["> 2%", "last 2 versions", "IE 11", "not dead"],
sassSourcemaps: true,
sassDeprecationWarnings: false
},
sprite: {
width: 24,
height: 24,
separator: "-",
projectIconsOnly: false,
},
};
let paths = settings.compile.paths;
let getSrcFrom = (key) => {
if (paths.src[key]) {
return paths.src[key];
}
return paths.src.defaults[`v${settings.version}`][key];
};
/*
----------------------------------------
TASKS
----------------------------------------
*/
/*
----------------------------------------
USWDS specific tasks
----------------------------------------
*/
const copy = {
theme() {
log(
colors.blue,
`Copy USWDS theme files: ${getSrcFrom("theme")} → ${paths.dist.theme}`
);
return src(`${getSrcFrom("theme")}/**/**`.replace("//", "/")).pipe(
dest(paths.dist.theme)
);
},
fonts() {
log(
colors.blue,
`Copy USWDS fonts: ${getSrcFrom("fonts")} → ${paths.dist.fonts}`
);
return src(`${getSrcFrom("fonts")}/**/**`.replace("//", "/"), {
encoding: false,
}).pipe(dest(paths.dist.fonts));
},
images() {
log(
colors.blue,
`Copy USWDS images: ${getSrcFrom("img")} → ${paths.dist.img}`
);
return src(`${getSrcFrom("img")}/**/**`.replace("//", "/"), {
encoding: false,
}).pipe(
dest(paths.dist.img)
);
},
js() {
log(
colors.blue,
`Copy USWDS compiled JS: ${getSrcFrom("js")} → ${paths.dist.js}`
);
return src(`${getSrcFrom("js")}/**/**`.replace("//", "/")).pipe(
dest(paths.dist.js)
);
},
};
/*
----------------------------------------
General tasks
----------------------------------------
*/
function handleError(error) {
log(error.message);
return this.emit("end");
}
function logVersion() {
log(colors.blue, `uswds.version: ${settings.version}`);
return Promise.resolve("logged version");
}
function getUswdsVersion() {
let uswdsPackage = "uswds";
if (settings.version === 3) {
uswdsPackage = "@uswds/uswds";
}
const packagePath = path.join(
path.dirname(require.resolve(uswdsPackage)),
"../../"
);
const version = require(`${packagePath}/package.json`).version;
return version;
}
function buildSass() {
const pkg = getUswdsVersion();
log(colors.blue, `Compiling with USWDS ${pkg}`);
const buildSettings = {
plugins: [
autoprefixer({
cascade: false,
grid: true,
overrideBrowserslist: settings.compile.browserslist,
}),
csso({ forceMediaMerge: false }),
],
includes: [
// 1. local theme files
paths.dist.theme,
// 2. uswds organization directory (npm packages)
getSrcFrom("uswds"),
// 3. v2 packages directory
`${getSrcFrom("sass")}/packages`.replace("//", "/"),
// 4. local uswds package
getSrcFrom("sass"),
],
};
return src([`${paths.dist.theme}/*.scss`.replace("//", "/")], {
sourcemaps: !!settings.compile.sassSourcemaps,
})
.pipe(
sass({
outputStyle: "compressed",
includePaths: buildSettings.includes,
quietDeps: !settings.compile.sassDeprecationWarnings,
}).on("error", handleError)
)
.pipe(replace(/\buswds @version\b/g, `based on uswds v${pkg}`))
.pipe(postcss(buildSettings.plugins))
.pipe(
dest(paths.dist.css, {
sourcemaps: settings.compile.sassSourcemaps ? "." : undefined,
})
);
}
function watchSass() {
return watch(
[
`${paths.dist.theme}/**/*.scss`.replace("//", "/"),
`${paths.src.projectSass}/**/*.scss`.replace("//", "/"),
],
buildSass
);
}
function getSpritePaths(spritePaths = []) {
const defaultSpritePath = `${paths.dist.img}/usa-icons/**/*.svg`.replace(
"//",
"/"
);
const customSpritePath = paths.src.projectIcons.length
? `${paths.src.projectIcons}/**/*.svg`.replace("//", "/")
: "";
if (customSpritePath) {
spritePaths.push(customSpritePath);
if (settings.sprite.projectIconsOnly) {
return spritePaths;
}
}
if (settings.sprite.projectIconsOnly && !customSpritePath) {
log(
colors.yellow,
`You've set sprite.projectIconsOnly to true, but haven't defined a custom project icon path directory (paths.src.projectIcons). Using default: "${defaultSpritePath}"`
);
}
spritePaths.push(defaultSpritePath);
return spritePaths;
}
function buildSprite() {
const spritePaths = getSpritePaths();
return src(spritePaths, {
allowEmpty: true,
encoding: false
})
.pipe(svgSprite())
.pipe(rename("usa-icons.svg"))
.on("error", handleError)
.pipe(dest(`${paths.dist.img}`));
}
function renameSprite() {
return src(`${paths.dist.img}/usa-icons.svg`.replace("//", "/"), {
allowEmpty: true,
encoding: false
})
.pipe(rename(`${paths.dist.img}/sprite.svg`.replace("//", "/")))
.pipe(dest(`./`));
}
function cleanSprite() {
return del(`${paths.dist.img}/usa-icons.svg`.replace("//", "/"));
}
exports.settings = settings;
exports.paths = paths;
exports.sprite = settings.sprite;
exports.copyTheme = copy.theme;
exports.copyFonts = copy.fonts;
exports.copyImages = copy.images;
exports.copyJS = copy.js;
exports.copyAssets = series(copy.fonts, copy.images, copy.js);
exports.copyAll = series(copy.theme, this.copyAssets);
exports.compileSass = series(logVersion, buildSass);
exports.compileIcons = series(buildSprite, renameSprite, cleanSprite);
exports.compile = series(logVersion, parallel(buildSass, this.compileIcons));
exports.updateUswds = series(this.copyAssets, this.compile);
exports.init = series(logVersion, this.copyAll, this.compile);
exports.watch = series(logVersion, buildSass, watchSass);
exports.default = this.watch;