1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 10:01:18 -04:00

add a *very* basic minifier to remove the insane amount of whitespace from compiled MDX

This commit is contained in:
2024-02-28 14:59:01 -05:00
parent 1c1f2ef9e9
commit e661ca48b8
4 changed files with 64 additions and 24 deletions

25
lib/helpers/minifier.ts Normal file
View File

@@ -0,0 +1,25 @@
import { trimLines } from "trim-lines";
import stripComments from "strip-comments";
// do some _very_ rudimentary JS minifying.
export const minifier = (source: string): string => {
// save the first line for later, it might be important?
const firstLine = source.split("\n")[0];
// remove JS comments
source = stripComments(source, {
block: false,
keepProtected: true,
});
// remove indentation
source = trimLines(source);
// remove newlines
source = source.replace(/\n/g, "");
// restore JSX flags if they were there at the beginning
if (firstLine.startsWith("/*@jsx")) {
source = `${firstLine}${source}`;
}
return source;
};