Skip to content

Commit

Permalink
Optimize gulp build (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
software-project authored Jun 15, 2020
1 parent 6e7b484 commit 4379b94
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 103 deletions.
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"source/_patterns/*.scss",
"source/_patterns/**/*.scss",
"source/css/styles-trial.scss"
],
"srlTrial": [
"source/_patterns/*.scss",
"source/_patterns/**/*.scss",
"source/css/styles-srl.scss",
"source/css/styles-trial.scss"
]
},
"fonts": {
Expand Down
85 changes: 30 additions & 55 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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 cleanCSS = require("gulp-clean-css");
const concat = require("gulp-concat");
const glob = require("gulp-sass-glob");
const mqpacker = require("css-mqpacker");
Expand All @@ -23,6 +23,8 @@ 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 stripCssComments = require("gulp-strip-css-comments");

const pkg = require("./node_modules/uswds/package.json");
const uswds = require("./node_modules/uswds-gulp/config/uswds");
Expand Down Expand Up @@ -68,13 +70,14 @@ const copyIconSprite = () => {
return gulp.src(config.icons.sprite_src).pipe(gulp.dest(config.icons.sprite_dest));
};

const srlCss = () => {
const buildCss = (paths, minFileName) => {
const plugins = [
// Pack media queries
mqpacker({ sort: true })
];
return gulp
.src(config.css.srl)

let css = gulp
.src(paths)
.pipe(glob())
.pipe(
plumber({
Expand Down Expand Up @@ -105,59 +108,31 @@ const srlCss = () => {
.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(stripCssComments())
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(config.css.public_folder)) //writing source map
.pipe(rename("styles-srl.min.css"))
.pipe(cleanCSS({ compatibility: "ie9" }))
.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 = () => {
const plugins = [
// Pack media queries
mqpacker({ sort: true })
];
return gulp
.src(config.css.trial)
.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(sourcemaps.write("./"))
.pipe(gulp.dest(config.css.public_folder)) //writing source map
.pipe(rename("styles-trial.min.css"))
.pipe(cleanCSS({ compatibility: "ie9" }))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(config.css.public_folder)) //
.pipe(browserSync.reload({ stream: true, match: "**/*.css" }));
return buildCss(config.css.trial, "styles-trial");
};

const allCss = () => {
return buildCss(config.css.srlTrial, "styles-all");
};

// Component JS.
Expand Down Expand Up @@ -204,10 +179,9 @@ const plJs = () => {
};

const watch = cb => {
gulp.watch(config.css.src, srlCss);
gulp.watch(config.css.trial, trialCss);
gulp.watch(config.css.srlTrial, allCss);
gulp.watch(config.js.src, plJs);
gulp.watch(config.pattern_lab.src, buildPattern);
gulp.watch(config.pattern_lab.src, dev);
gulp.watch(config.css.styleguide_src, copyPlStyles);
};

Expand All @@ -232,7 +206,8 @@ const serve = cb => {
ready: (err, bs) => {
cb();
}
}
},
reloadDebounce: 200
});
};

Expand All @@ -245,7 +220,6 @@ const build = gulp.series(
trialCss,
plJs
);
const buildPattern = gulp.series(plPhp, copyUswdsFonts, copyUswdsImages, copyIconSprite, plJs);
const srlbuild = gulp.series(plPhp, copyUswdsFonts, copyUswdsImages, copyIconSprite, srlCss, plJs);
const trialbuild = gulp.series(
plPhp,
Expand All @@ -256,7 +230,8 @@ const trialbuild = gulp.series(
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(build, serve, watch);
exports.default = gulp.series(dev, serve, watch);
162 changes: 122 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"gulp": "^4.0.2",
"gulp-autoprefixer": "^7.0.1",
"gulp-babel": "^8.0.0",
"gulp-clean-css": "^4.3.0",
"gulp-concat": "^2.6.1",
"gulp-csso": "^4.0.1",
"gulp-notify": "^3.2.0",
"gulp-plumber": "^1.2.1",
"gulp-postcss": "^8.0.0",
Expand All @@ -31,6 +31,7 @@
"gulp-sass-glob": "^1.1.0",
"gulp-shell": "^0.7.1",
"gulp-sourcemaps": "^2.6.5",
"gulp-strip-css-comments": "^2.0.0",
"gulp-uglify": "^3.0.2",
"husky": "^3.1.0",
"path": "^0.12.7",
Expand Down
1 change: 0 additions & 1 deletion public/css/styles-srl.css.map

This file was deleted.

4 changes: 3 additions & 1 deletion public/css/styles-srl.min.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/css/styles-srl.min.css.map

This file was deleted.

4 changes: 3 additions & 1 deletion public/css/styles-trial.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/scripts.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4379b94

Please sign in to comment.