-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
238 lines (214 loc) · 6.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
// Based on https://github.com/SFDigitalServices/sfgov-pattern-lab/blob/master/gulpfile.babel.js
const devBuild = process.title == "gulp";
const gulp = require("gulp");
const browserSync = require("browser-sync").create();
const config = require("./config.json");
// Include plugins.
const autoprefix = require("gulp-autoprefixer");
const babel = require("gulp-babel");
const concat = require("gulp-concat");
const glob = require("gulp-sass-glob");
const mqpacker = require("css-mqpacker");
const notify = require("gulp-notify");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const replace = require("gulp-replace");
const sass = require("gulp-sass");
const through = require("through2");
const sourcemaps = require("gulp-sourcemaps");
const webpack = require("webpack-stream");
const compiler = require("webpack");
const uglify = require("gulp-uglify");
const { exec } = require("child_process");
const csso = require("gulp-csso");
const strip = require("gulp-strip-comments");
const pkg = require("./node_modules/uswds/package.json");
const uswds = require("./node_modules/uswds-gulp/config/uswds");
const jsSrc = config.js.src;
jsSrc.unshift(`${uswds}/js/uswds.js`);
// Error Handler
// -------------------------------------------------------------- //
const errorHandler = error => {
notify.onError({
title: "Task Failed [" + error.plugin + "]",
message: "Error: <%= error.message %>",
sound: "Beep"
})(error);
// Log error to console, unless that's already happening. Sass lint provides
// good error handling/feedback in the terminal, so in this case we only want
// the OS X notification w/sound.
if (error.plugin !== "gulp-sass") {
console.log(error.toString());
}
// Prevent Gulp watch from stopping.
this.emit("end");
};
// Use the Pattern Lab PHP command to generate the pattern library
const plPhp = () => {
return exec("php core/console --generate");
};
const copyUswdsFonts = () => {
return gulp.src(`${uswds}/fonts/**/*`).pipe(gulp.dest(config.fonts.public_fonts));
};
const copyUswdsImages = () => {
return gulp.src(`${uswds}/img/**/*`).pipe(gulp.dest(config.images.public_images));
};
const copyPlStyles = () => {
return gulp.src(config.css.styleguide_src).pipe(gulp.dest(config.css.styleguide_public_folder));
};
const copyIconSprite = () => {
return gulp.src(config.icons.sprite_src).pipe(gulp.dest(config.icons.sprite_dest));
};
const buildCss = (paths, minFileName) => {
const plugins = [
// Pack media queries
mqpacker({ sort: true })
];
let css = gulp
.src(paths)
.pipe(glob())
.pipe(
plumber({
errorHandler: function(error) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Beep"
})(error);
this.emit("end");
}
})
)
.pipe(sourcemaps.init({ largeFile: true }))
.pipe(
sass({
outputStyle: "expanded",
errLogToConsole: true,
includePaths: [
config.css.project_scss // pulling all the sass and converts to css
// "${uswds}/scss",
// "${uswds}/scss/packages",
]
// importer: importOnce
})
)
.pipe(replace(/\buswds @version\b/g, "based on uswds v" + pkg.version))
.pipe(autoprefix("last 2 versions", "> 1%", "ie 9", "ie 10"))
.pipe(postcss(plugins))
.pipe(strip.text())
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(config.css.public_folder)) //writing source map
.pipe(rename(`${minFileName}.min.css`));
if (!devBuild) {
css.pipe(csso());
}
return css
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(config.css.public_folder)) //
.pipe(browserSync.reload({ stream: true, match: "**/*.css" }));
};
const srlCss = () => {
return buildCss(config.css.srl, "styles-srl");
};
const trialCss = () => {
return buildCss(config.css.trial, "styles-trial");
};
const allCss = () => {
return buildCss(config.css.srlTrial, "styles-all");
};
// Component JS.
// -------------------------------------------------------------------- //
// the following task concatenates all the javascript files inside the
// _patterns folder, if new patterns need to be added the config.json array
// needs to be edited to watch for more folders.
const plJs = () => {
return gulp
.src(jsSrc)
.pipe(
webpack(
{
devtool: "eval-source-map",
mode: "development",
plugins: [
new compiler.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
],
output: {
filename: "scripts.js"
}
},
compiler
)
)
.pipe(
babel({
presets: ["@babel/preset-env"]
})
)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(gulp.dest(config.js.dest))
.pipe(strip())
.pipe(uglify())
.pipe(sourcemaps.write("./"))
.pipe(rename("scripts.min.js"))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(config.js.dest))
.pipe(browserSync.reload({ stream: true, match: "**/*.js" }));
};
const watch = cb => {
gulp.watch(config.css.srlTrial, allCss);
gulp.watch(config.js.src, plJs);
gulp.watch(config.pattern_lab.src, dev);
gulp.watch(config.css.styleguide_src, copyPlStyles);
};
const trialwatch = cb => {
gulp.watch(config.css.trial, trialCss);
gulp.watch(config.js.src, plJs);
gulp.watch(config.pattern_lab.src, trialbuild);
gulp.watch(config.css.styleguide_src, copyPlStyles);
};
const srlwatch = cb => {
gulp.watch(config.css.srl, srlCss);
gulp.watch(config.js.src, plJs);
gulp.watch(config.pattern_lab.src, srlbuild);
gulp.watch(config.css.styleguide_src, copyPlStyles);
};
const serve = cb => {
browserSync.init({
server: "public",
callbacks: {
ready: (err, bs) => {
cb();
}
},
reloadDebounce: 200
});
};
const build = gulp.series(
plPhp,
copyUswdsFonts,
copyUswdsImages,
copyIconSprite,
srlCss,
trialCss,
plJs
);
const srlbuild = gulp.series(plPhp, copyUswdsFonts, copyUswdsImages, copyIconSprite, srlCss, plJs);
const trialbuild = gulp.series(
plPhp,
copyUswdsFonts,
copyUswdsImages,
copyIconSprite,
trialCss,
plJs
);
const dev = gulp.series(plPhp, copyUswdsFonts, copyUswdsImages, copyIconSprite, allCss, plJs);
exports.build = build;
exports.trial = gulp.series(trialbuild, serve, trialwatch);
exports.srl = gulp.series(srlbuild, serve, srlwatch);
exports.default = gulp.series(dev, serve, watch);