Skip to content

Commit

Permalink
test minify middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Jan 20, 2024
1 parent 1be5b65 commit 8b8fdd2
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ RUN npm i

COPY public public

RUN npm i -g terser
RUN terser --compress -o public/js/app.js -- public/js/app.js
RUN npm remove terser

COPY views views

COPY tailwind tailwind
Expand Down
106 changes: 63 additions & 43 deletions init.mjs
Original file line number Diff line number Diff line change
@@ -1,48 +1,68 @@

import nunjucks from "nunjucks";
import markdown from "nunjucks-markdown";
import { marked } from "marked";
import { minify } from "html-minifier";

export function configureNunjucks(app) {
const nunjucksEnv = nunjucks.configure("views", {
autoescape: true,
express: app,
});

nunjucksEnv.addFilter("json", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const jsonString = JSON.stringify(value, null, spaces).replace(
/</g,
"\\u003c"
);
return nunjucks.runtime.markSafe(jsonString);
});
nunjucksEnv.addFilter("slug", function (value, spaces) {
const slugify = (input) => {
return input
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
};
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const slug = slugify(value);
return nunjucks.runtime.markSafe(slug);
});

nunjucksEnv.addFilter("date", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const dateValue = new Date(value);
return nunjucks.runtime.markSafe(dateValue.toLocaleDateString("fr"));
function minifyMiddleware(req, res, next) {
res.oldRender = res.render;
res.render = function(view, options) {
this.oldRender(view, options, function(err, html) {
if (err) throw err;
html = minify(html, {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true,
});
res.send(html);
});
markdown.register(nunjucksEnv, marked);
}
};
next();
}

export function configureNunjucks(app) {
const nunjucksEnv = nunjucks.configure("views", {
autoescape: true,
express: app,
});

nunjucksEnv.addFilter("json", function(value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const jsonString = JSON.stringify(value, null, spaces).replace(
/</g,
"\\u003c",
);
return nunjucks.runtime.markSafe(jsonString);
});
nunjucksEnv.addFilter("slug", function(value, spaces) {
const slugify = (input) => {
return input
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
};
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const slug = slugify(value);
return nunjucks.runtime.markSafe(slug);
});

nunjucksEnv.addFilter("date", function(value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const dateValue = new Date(value);
return nunjucks.runtime.markSafe(dateValue.toLocaleDateString("fr"));
});
markdown.register(nunjucksEnv, marked);
app.use(minifyMiddleware);
}
Loading

0 comments on commit 8b8fdd2

Please sign in to comment.