ghtml 2.1.5 → 2.2.1

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.
package/bin/README.md CHANGED
@@ -1,11 +1,11 @@
1
- Append unique hashes to assets referenced in your views to aggressively cache them while guaranteeing that clients receive the most recent versions.
1
+ Lets you append unique hashes to assets referenced in your views to aggressively cache them while guaranteeing that clients receive the most recent versions.
2
2
 
3
3
  ## Usage
4
4
 
5
- Running the following command will scan asset files found in the `roots` path(s) and replace their references with hashed versions in the `refs` path(s):
5
+ Running the following command will scan asset files found in the `roots` path(s) and replace their references in the `refs` path(s) with hashed versions:
6
6
 
7
7
  ```sh
8
- npx ghtml --roots="path/to/scan/assets/1/,path/to/scan/assets/2/" --refs="views/path/to/append/hashes/1/,views/path/to/append/hashes/2/"
8
+ npx ghtml --roots="base/path/to/scan/assets/1/,base/path/to/scan/assets/2/" --refs="views/path/to/append/hashes/1/,views/path/to/append/hashes/2/"
9
9
  ```
10
10
 
11
11
  ## Example (Fastify)
@@ -3,11 +3,11 @@
3
3
  "main": "./server.js",
4
4
  "scripts": {
5
5
  "start": "node server.js",
6
- "build": "node ../src/index.js --roots=assets/ --refs=routes/"
6
+ "build": "ghtml --roots=assets/ --refs=routes/"
7
7
  },
8
8
  "dependencies": {
9
9
  "@fastify/static": "^7.0.1",
10
10
  "fastify": "^4.26.1",
11
- "fastify-html": "^0.5.0"
11
+ "ghtml": "file:../../"
12
12
  }
13
13
  }
@@ -1,7 +1,11 @@
1
+ /* eslint-disable n/no-missing-import */
2
+
3
+ import { html } from "ghtml";
4
+
1
5
  export default async (fastify) => {
2
- const { html } = fastify;
6
+ fastify.decorateReply("html", function (inner) {
7
+ this.type("text/html; charset=utf-8");
3
8
 
4
- fastify.addLayout((inner) => {
5
9
  return html`<!doctype html>
6
10
  <html lang="en">
7
11
  <head>
@@ -12,17 +16,19 @@ export default async (fastify) => {
12
16
  />
13
17
  <title>Document</title>
14
18
  <link rel="stylesheet" href="/p/assets/style.css" />
19
+ <script src="/p/assets/script.js"></script>
15
20
  </head>
16
21
  <body>
17
22
  !${inner}
18
23
  </body>
19
- </html>`;
24
+ </html> `;
20
25
  });
21
26
 
22
27
  fastify.get("/", async (request, reply) => {
23
- return reply.html`
28
+ return reply.html(html`
24
29
  <h1 class="caption">Hello, world!</h1>
25
- <script src="/p/assets/script.js"></script>
26
- `;
30
+ <p>This is a simple example of a Fastify server.</p>
31
+ <p>It uses <code>ghtml</code>.</p>
32
+ `);
27
33
  });
28
34
  };
@@ -1,4 +1,4 @@
1
- /* eslint n/no-missing-import: "off" */
1
+ /* eslint-disable n/no-missing-import */
2
2
 
3
3
  import Fastify from "fastify";
4
4
 
@@ -13,10 +13,14 @@ await fastify.register(import("@fastify/static"), {
13
13
  immutable: true,
14
14
  maxAge: 31536000 * 1000,
15
15
  });
16
- await fastify.register(import("fastify-html"));
17
16
 
18
17
  // Routes
19
18
  fastify.register(import("./routes/index.js"));
20
19
 
21
- await fastify.listen({ port: 5050 });
22
- console.warn("Server listening at http://localhost:5050");
20
+ fastify.listen({ port: 5050 }, (err, address) => {
21
+ if (err) {
22
+ throw err;
23
+ }
24
+
25
+ console.warn(`Server listening at ${address}`);
26
+ });
package/bin/src/index.js CHANGED
File without changes
package/bin/src/utils.js CHANGED
@@ -22,7 +22,7 @@ const updateFilePathsWithHashes = async (
22
22
  skipPatterns,
23
23
  ) => {
24
24
  for (let ref of refs) {
25
- ref = ref.split(win32.sep).join(posix.sep);
25
+ ref = ref.replaceAll(win32.sep, posix.sep);
26
26
  if (!ref.endsWith("/")) {
27
27
  ref += "/";
28
28
  }
@@ -36,8 +36,8 @@ const updateFilePathsWithHashes = async (
36
36
  ignore: skipPatterns,
37
37
  });
38
38
 
39
- for await (const file of filesIterable) {
40
- let content = await readFile(file, "utf8");
39
+ for await (const filePath of filesIterable) {
40
+ let content = await readFile(filePath, "utf8");
41
41
  let found = false;
42
42
 
43
43
  for (const [originalPath, hash] of fileHashes) {
@@ -66,7 +66,7 @@ const updateFilePathsWithHashes = async (
66
66
  }
67
67
 
68
68
  if (found) {
69
- await writeFile(file, content);
69
+ await writeFile(filePath, content);
70
70
  }
71
71
  }
72
72
  }
@@ -82,10 +82,10 @@ const generateHashesAndReplace = async ({
82
82
  roots = Array.isArray(roots) ? roots : [roots];
83
83
  refs = Array.isArray(refs) ? refs : [refs];
84
84
 
85
- for (let rootPath of roots) {
86
- rootPath = rootPath.split(win32.sep).join(posix.sep);
87
- if (!rootPath.endsWith("/")) {
88
- rootPath += "/";
85
+ for (let root of roots) {
86
+ root = root.replaceAll(win32.sep, posix.sep);
87
+ if (!root.endsWith("/")) {
88
+ root += "/";
89
89
  }
90
90
 
91
91
  const queue = [];
@@ -94,21 +94,21 @@ const generateHashesAndReplace = async ({
94
94
  nodir: true,
95
95
  follow: true,
96
96
  absolute: true,
97
- cwd: rootPath,
97
+ cwd: root,
98
98
  dot: includeDotFiles,
99
99
  ignore: skipPatterns,
100
100
  });
101
101
 
102
102
  for await (let filePath of filesIterable) {
103
- filePath = filePath.split(win32.sep).join(posix.sep);
103
+ filePath = filePath.replaceAll(win32.sep, posix.sep);
104
104
  queue.push(generateFileHash(filePath));
105
105
  files.push(filePath);
106
106
  }
107
107
 
108
108
  const hashes = await Promise.all(queue);
109
109
 
110
- for (let i = 0; i < files.length; i++) {
111
- const fileRelativePath = posix.relative(rootPath, files[i]);
110
+ for (let i = 0; i < files.length; ++i) {
111
+ const fileRelativePath = posix.relative(root, files[i]);
112
112
  fileHashes.set(fileRelativePath, hashes[i]);
113
113
  }
114
114
  }
@@ -121,4 +121,4 @@ const generateHashesAndReplace = async ({
121
121
  );
122
122
  };
123
123
 
124
- export { generateFileHash, generateHashesAndReplace };
124
+ export { generateHashesAndReplace };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
4
4
  "author": "Gürgün Dayıoğlu",
5
5
  "license": "MIT",
6
- "version": "2.1.5",
6
+ "version": "2.2.1",
7
7
  "type": "module",
8
8
  "bin": "./bin/src/index.js",
9
9
  "main": "./src/index.js",