Skip to content

Commit

Permalink
feat: gzip js, css, SVG icons assets (#11208)
Browse files Browse the repository at this point in the history
As pointed by @github-throwaway in
openfoodfacts/smooth-app#5583 (comment)
, we currently serve uncompressed assets (CSS, JS etc.).

We should add .gz versions so that they can be served by nginx. We need
to keep the non .gz files as well as we use try_files, which returns a
404 if the uncompressed file does not exist:

```
                try_files resources/$uri $uri $uri/ =404;
                gzip_static always;
                gunzip on;
```
  • Loading branch information
stephanegigandet authored Jan 8, 2025
1 parent 85fd575 commit c0a5275
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 405 deletions.
67 changes: 59 additions & 8 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { init, write } from "gulp-sourcemaps";

import concat from "gulp-concat";
import gulpSass from "gulp-sass";
import gzip from "gulp-gzip";
import minifyCSS from "gulp-csso";
import sassLib from "sass";
import svgmin from "gulp-svgmin";
Expand All @@ -23,27 +24,41 @@ const sassSrc = "./scss/**/*.scss";

const imagesSrc = ["./node_modules/leaflet/dist/**/*.png"];

// nginx needs both uncompressed and compressed files as we use try_files with gzip_static always & gunzip

export function icons() {
return src("*.svg", { cwd: "./icons" }).
const processed = src("*.svg", { cwd: "./icons" }).
pipe(
svgmin({
// @ts-ignore
configFile: "icons/svgo.config.js",
})
).
pipe(dest("./html/images/icons/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/images/icons/dist"));

return processed && compressed;
}

export function attributesIcons() {
return src("*.svg", { cwd: "./html/images/attributes/src" }).
const processed = src("*.svg", { cwd: "./html/images/attributes/src" }).
pipe(svgmin()).
pipe(dest("./html/images/attributes/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/images/attributes/dist"));

return processed && compressed;
}

export function css() {
console.log("(re)building css");

return src(sassSrc).
const processed = src(sassSrc).
pipe(init()).
pipe(
sass({
Expand All @@ -55,10 +70,16 @@ export function css() {
pipe(minifyCSS()).
pipe(write(".")).
pipe(dest("./html/css/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/css/dist"));

return processed && compressed;
}

export function copyJs() {
return src([
const processed = src([
"./node_modules/@webcomponents/**/webcomponentsjs/**/*.js",
"./node_modules/foundation-sites/js/vendor/*.js",
"./node_modules/foundation-sites/js/foundation.js",
Expand All @@ -84,20 +105,32 @@ export function copyJs() {
pipe(terser()).
pipe(write(".")).
pipe(dest("./html/js/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/js/dist"));

return processed && compressed;
}

export function buildJs() {
console.log("(re)building js");

return src(jsSrc).
const processed = src(jsSrc).
pipe(init()).
pipe(terser()).
pipe(write(".")).
pipe(dest("./html/js/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/js/dist"));

return processed && compressed;
}

function buildjQueryUi() {
return src([
const processed = src([
"./node_modules/jquery-ui/ui/jquery-patch.js",
"./node_modules/jquery-ui/ui/version.js",
"./node_modules/jquery-ui/ui/widget.js",
Expand All @@ -113,10 +146,16 @@ function buildjQueryUi() {
pipe(concat("jquery-ui.js")).
pipe(write(".")).
pipe(dest("./html/js/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/js/dist"));

return processed && compressed;
}

function jQueryUiThemes() {
return src([
const processed = src([
"./node_modules/jquery-ui/themes/base/core.css",
"./node_modules/jquery-ui/themes/base/autocomplete.css",
"./node_modules/jquery-ui/themes/base/menu.css",
Expand All @@ -127,10 +166,16 @@ function jQueryUiThemes() {
pipe(concat("jquery-ui.css")).
pipe(write(".")).
pipe(dest("./html/css/dist/jqueryui/themes/base"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/css/dist/jqueryui/themes/base"));

return processed && compressed;
}

function copyCss() {
return src([
const processed = src([
"./node_modules/leaflet/dist/leaflet.css",
"./node_modules/leaflet.markercluster/dist/MarkerCluster.css",
"./node_modules/leaflet.markercluster/dist/MarkerCluster.Default.css",
Expand All @@ -142,6 +187,12 @@ function copyCss() {
pipe(minifyCSS()).
pipe(write(".")).
pipe(dest("./html/css/dist"));

const compressed = processed.
pipe(gzip()).
pipe(dest("./html/css/dist"));

return processed && compressed;
}

function copyImages() {
Expand Down
Loading

0 comments on commit c0a5275

Please sign in to comment.