ghtml 2.1.5 → 2.2.0

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
+ This executable 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,9 @@
1
+ import { html } from "ghtml";
2
+
1
3
  export default async (fastify) => {
2
- const { html } = fastify;
4
+ fastify.decorateReply("html", function (inner) {
5
+ this.type("text/html; charset=utf-8");
3
6
 
4
- fastify.addLayout((inner) => {
5
7
  return html`<!doctype html>
6
8
  <html lang="en">
7
9
  <head>
@@ -12,17 +14,19 @@ export default async (fastify) => {
12
14
  />
13
15
  <title>Document</title>
14
16
  <link rel="stylesheet" href="/p/assets/style.css" />
17
+ <script src="/p/assets/script.js"></script>
15
18
  </head>
16
19
  <body>
17
20
  !${inner}
18
21
  </body>
19
- </html>`;
22
+ </html> `;
20
23
  });
21
24
 
22
25
  fastify.get("/", async (request, reply) => {
23
- return reply.html`
26
+ return reply.html(html`
24
27
  <h1 class="caption">Hello, world!</h1>
25
- <script src="/p/assets/script.js"></script>
26
- `;
28
+ <p>This is a simple example of a Fastify server.</p>
29
+ <p>It uses <code>ghtml</code>.</p>
30
+ `);
27
31
  });
28
32
  };
@@ -1,5 +1,3 @@
1
- /* eslint n/no-missing-import: "off" */
2
-
3
1
  import Fastify from "fastify";
4
2
 
5
3
  const fastify = Fastify();
@@ -13,10 +11,14 @@ await fastify.register(import("@fastify/static"), {
13
11
  immutable: true,
14
12
  maxAge: 31536000 * 1000,
15
13
  });
16
- await fastify.register(import("fastify-html"));
17
14
 
18
15
  // Routes
19
16
  fastify.register(import("./routes/index.js"));
20
17
 
21
- await fastify.listen({ port: 5050 });
22
- console.warn("Server listening at http://localhost:5050");
18
+ fastify.listen({ port: 5050 }, (err, address) => {
19
+ if (err) {
20
+ throw err;
21
+ }
22
+
23
+ console.warn(`Server listening at ${address}`);
24
+ });
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,37 +36,45 @@ 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");
41
- let found = false;
39
+ for await (const filePath of filesIterable) {
40
+ let content = await readFile(filePath, "utf8");
41
+ let hasChanges = false;
42
42
 
43
43
  for (const [originalPath, hash] of fileHashes) {
44
- const escapedPath = originalPath.replace(
45
- /[$()*+.?[\\\]^{|}]/gu,
46
- "\\$&",
47
- );
48
- const regex = new RegExp(
49
- `(?<path>${escapedPath})(\\?(?<queryString>[^#"'\`]*))?`,
50
- "gu",
51
- );
52
-
53
- content = content.replace(
54
- regex,
55
- (match, p1, p2, p3, offset, string, groups) => {
56
- found = true;
57
- const { path, queryString } = groups;
58
-
59
- return !queryString
60
- ? `${path}?hash=${hash}`
61
- : queryString.includes("hash=")
62
- ? `${path}?${queryString.replace(/(?<hash>hash=)[\dA-Fa-f]*/u, `$1${hash}`)}`
63
- : `${path}?hash=${hash}&${queryString}`;
64
- },
65
- );
44
+ const pathIndex = content.indexOf(originalPath);
45
+ if (pathIndex !== -1) {
46
+ hasChanges = true;
47
+ const beforePath = content.slice(0, pathIndex);
48
+ const afterPath = content.slice(pathIndex + originalPath.length);
49
+
50
+ const queryStart = afterPath.indexOf("?");
51
+ if (queryStart === -1) {
52
+ content = `${beforePath}${originalPath}?hash=${hash}${afterPath}`;
53
+ continue;
54
+ }
55
+
56
+ const queryEnd = afterPath.slice(queryStart).search(/[#"'`]/u);
57
+ const queryString =
58
+ queryEnd !== -1
59
+ ? afterPath.slice(queryStart + 1, queryEnd)
60
+ : afterPath.slice(queryStart + 1);
61
+
62
+ const hashRegex = /(?<=(?:^|&))hash=[^&]*/u;
63
+ if (hashRegex.test(queryString)) {
64
+ const newQueryString = queryString.replace(
65
+ hashRegex,
66
+ `hash=${hash}`,
67
+ );
68
+ content = `${beforePath}${originalPath}?${newQueryString}${afterPath.slice(queryStart + queryString.length + 1)}`;
69
+ continue;
70
+ }
71
+
72
+ content = `${beforePath}${originalPath}?hash=${hash}&${afterPath.slice(queryStart + 1)}`;
73
+ }
66
74
  }
67
75
 
68
- if (found) {
69
- await writeFile(file, content);
76
+ if (hasChanges) {
77
+ await writeFile(filePath, content);
70
78
  }
71
79
  }
72
80
  }
@@ -82,10 +90,10 @@ const generateHashesAndReplace = async ({
82
90
  roots = Array.isArray(roots) ? roots : [roots];
83
91
  refs = Array.isArray(refs) ? refs : [refs];
84
92
 
85
- for (let rootPath of roots) {
86
- rootPath = rootPath.split(win32.sep).join(posix.sep);
87
- if (!rootPath.endsWith("/")) {
88
- rootPath += "/";
93
+ for (let root of roots) {
94
+ root = root.replaceAll(win32.sep, posix.sep);
95
+ if (!root.endsWith("/")) {
96
+ root += "/";
89
97
  }
90
98
 
91
99
  const queue = [];
@@ -94,21 +102,21 @@ const generateHashesAndReplace = async ({
94
102
  nodir: true,
95
103
  follow: true,
96
104
  absolute: true,
97
- cwd: rootPath,
105
+ cwd: root,
98
106
  dot: includeDotFiles,
99
107
  ignore: skipPatterns,
100
108
  });
101
109
 
102
110
  for await (let filePath of filesIterable) {
103
- filePath = filePath.split(win32.sep).join(posix.sep);
111
+ filePath = filePath.replaceAll(win32.sep, posix.sep);
104
112
  queue.push(generateFileHash(filePath));
105
113
  files.push(filePath);
106
114
  }
107
115
 
108
116
  const hashes = await Promise.all(queue);
109
117
 
110
- for (let i = 0; i < files.length; i++) {
111
- const fileRelativePath = posix.relative(rootPath, files[i]);
118
+ for (let i = 0; i < files.length; ++i) {
119
+ const fileRelativePath = posix.relative(root, files[i]);
112
120
  fileHashes.set(fileRelativePath, hashes[i]);
113
121
  }
114
122
  }
@@ -121,4 +129,4 @@ const generateHashesAndReplace = async ({
121
129
  );
122
130
  };
123
131
 
124
- export { generateFileHash, generateHashesAndReplace };
132
+ 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.0",
7
7
  "type": "module",
8
8
  "bin": "./bin/src/index.js",
9
9
  "main": "./src/index.js",