basecampjs 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +53 -0
  2. package/package.json +3 -1
package/index.js CHANGED
@@ -11,6 +11,8 @@ import matter from "gray-matter";
11
11
  import MarkdownIt from "markdown-it";
12
12
  import nunjucks from "nunjucks";
13
13
  import { Liquid } from "liquidjs";
14
+ import { minify as minifyCss } from "csso";
15
+ import { minify as minifyHtml } from "html-minifier-terser";
14
16
 
15
17
  const cwd = process.cwd();
16
18
  const md = new MarkdownIt({ html: true, linkify: true, typographer: true });
@@ -21,6 +23,8 @@ const defaultConfig = {
21
23
  outDir: "dist",
22
24
  templateEngine: "nunjucks",
23
25
  markdown: true,
26
+ minifyCSS: false,
27
+ minifyHTML: false,
24
28
  integrations: { nunjucks: true, liquid: false, vue: false, alpine: false }
25
29
  };
26
30
 
@@ -69,6 +73,45 @@ async function copyPublic(publicDir, outDir) {
69
73
  }
70
74
  }
71
75
 
76
+ async function minifyCSSFiles(outDir) {
77
+ const files = await walkFiles(outDir);
78
+ const cssFiles = files.filter((file) => extname(file).toLowerCase() === ".css");
79
+
80
+ await Promise.all(cssFiles.map(async (file) => {
81
+ try {
82
+ const css = await readFile(file, "utf8");
83
+ const { css: minified } = minifyCss(css);
84
+ await writeFile(file, minified, "utf8");
85
+ } catch (err) {
86
+ console.error(kolor.red(`Failed to minify CSS ${relative(outDir, file)}: ${err.message}`));
87
+ }
88
+ }));
89
+ }
90
+
91
+ async function minifyHTMLFiles(outDir, config) {
92
+ const files = await walkFiles(outDir);
93
+ const htmlFiles = files.filter((file) => extname(file).toLowerCase() === ".html");
94
+
95
+ await Promise.all(htmlFiles.map(async (file) => {
96
+ try {
97
+ const html = await readFile(file, "utf8");
98
+ const minified = await minifyHtml(html, {
99
+ collapseWhitespace: true,
100
+ removeComments: true,
101
+ minifyCSS: !!config.minifyCSS,
102
+ minifyJS: true,
103
+ keepClosingSlash: true,
104
+ removeRedundantAttributes: true,
105
+ removeScriptTypeAttributes: true,
106
+ removeStyleLinkTypeAttributes: true
107
+ });
108
+ await writeFile(file, minified, "utf8");
109
+ } catch (err) {
110
+ console.error(kolor.red(`Failed to minify HTML ${relative(outDir, file)}: ${err.message}`));
111
+ }
112
+ }));
113
+ }
114
+
72
115
  async function walkFiles(dir) {
73
116
  const results = [];
74
117
  if (!existsSync(dir)) return results;
@@ -233,6 +276,16 @@ async function build(cwdArg = cwd) {
233
276
 
234
277
  await Promise.all(files.map((file) => renderPage(file, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data })));
235
278
 
279
+ if (config.minifyCSS) {
280
+ await minifyCSSFiles(outDir);
281
+ console.log(kolor.green("CSS minified"));
282
+ }
283
+
284
+ if (config.minifyHTML) {
285
+ await minifyHTMLFiles(outDir, config);
286
+ console.log(kolor.green("HTML minified"));
287
+ }
288
+
236
289
  console.log(kolor.green(`Built ${files.length} page(s) → ${relative(cwdArg, outDir)}`));
237
290
  }
238
291
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basecampjs",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "description": "BasecampJS engine for Campsite static site generator.",
6
6
  "bin": {
@@ -11,6 +11,8 @@
11
11
  ".": "./index.js"
12
12
  },
13
13
  "dependencies": {
14
+ "csso": "^5.0.5",
15
+ "html-minifier-terser": "^7.2.0",
14
16
  "chokidar": "^3.6.0",
15
17
  "gray-matter": "^4.0.3",
16
18
  "kolorist": "^1.8.0",