vite-plugin-unit 1.0.0 → 1.1.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/dist/index.js +29 -16
- package/package.json +58 -73
package/dist/index.js
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @license MIT
|
|
6
6
|
* @url https://github.com/henryhale/vite-plugin-unit
|
|
7
7
|
*/
|
|
8
|
+
import { log } from "node:console";
|
|
8
9
|
import { existsSync, readFileSync } from "node:fs";
|
|
10
|
+
import { cp, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
11
|
import { dirname, extname, join, resolve } from "node:path";
|
|
10
|
-
import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
11
|
-
import { log } from "node:console";
|
|
12
12
|
const defaultOptions = {
|
|
13
13
|
pages: "pages/",
|
|
14
14
|
template: "template.html",
|
|
@@ -44,9 +44,10 @@ export default function plugin(options = {}) {
|
|
|
44
44
|
// create a key-value object from attributes of an html tag
|
|
45
45
|
function mapAttributes(attr = "") {
|
|
46
46
|
const map = {};
|
|
47
|
-
let match;
|
|
48
|
-
while (
|
|
47
|
+
let match = attrRegex.exec(attr);
|
|
48
|
+
while (match !== null) {
|
|
49
49
|
map[match[1]] = match[2];
|
|
50
|
+
match = attrRegex.exec(attr);
|
|
50
51
|
}
|
|
51
52
|
return map;
|
|
52
53
|
}
|
|
@@ -62,16 +63,16 @@ export default function plugin(options = {}) {
|
|
|
62
63
|
return "";
|
|
63
64
|
})
|
|
64
65
|
// replace every html tag matching an import name
|
|
65
|
-
.replace(htmlRegex, (match, tag, attr
|
|
66
|
+
.replace(htmlRegex, (match, tag, attr) => {
|
|
66
67
|
const path = nameToPath.get(tag);
|
|
67
68
|
if (path) {
|
|
68
69
|
// get rid of trailing forward slash
|
|
69
70
|
if (attr.endsWith("/"))
|
|
70
71
|
attr = attr.slice(0, -1);
|
|
71
72
|
const map = mapAttributes(attr);
|
|
72
|
-
return pathToCode
|
|
73
|
+
return (pathToCode
|
|
73
74
|
.get(path)
|
|
74
|
-
|
|
75
|
+
?.replace(valueRegex, (m, key) => {
|
|
75
76
|
const value = map[key];
|
|
76
77
|
if (value) {
|
|
77
78
|
delete map[key];
|
|
@@ -81,10 +82,10 @@ export default function plugin(options = {}) {
|
|
|
81
82
|
})
|
|
82
83
|
.replace(tagRegex, (m) => {
|
|
83
84
|
const others = Object.entries(map).reduce((r, [k, v]) => {
|
|
84
|
-
return r + k
|
|
85
|
+
return `${r + k}=${/'/.test(v) ? `"${v}"` : `'${v}'`}`;
|
|
85
86
|
}, "");
|
|
86
|
-
return m.slice(0, -1)
|
|
87
|
-
});
|
|
87
|
+
return `${m.slice(0, -1)} ${others}>`;
|
|
88
|
+
}) ?? "");
|
|
88
89
|
}
|
|
89
90
|
return match;
|
|
90
91
|
})
|
|
@@ -122,15 +123,23 @@ export default function plugin(options = {}) {
|
|
|
122
123
|
async function respond(file) {
|
|
123
124
|
res.statusCode = 200;
|
|
124
125
|
res.setHeader("Content-Type", "text/html");
|
|
125
|
-
const contents = await readFile(join(config.root, opt.template), {
|
|
126
|
+
const contents = await readFile(join(config.root, opt.template), {
|
|
127
|
+
encoding: "utf-8"
|
|
128
|
+
});
|
|
126
129
|
pathToCode.clear();
|
|
127
130
|
const compiled = compile(file, await readFile(file, { encoding: "utf-8" }));
|
|
128
131
|
res.end(contents.replace(opt.slot, compiled));
|
|
129
132
|
}
|
|
130
133
|
// folder containing pages
|
|
131
134
|
const srcDir = join(config.root, opt.pages);
|
|
135
|
+
// strip the base path from the request URL
|
|
136
|
+
let url = req.url || "";
|
|
137
|
+
const base = config.base || "/";
|
|
138
|
+
if (base !== "/" && url.startsWith(base)) {
|
|
139
|
+
url = "/" + url.slice(base.length);
|
|
140
|
+
}
|
|
132
141
|
// generate actual file path
|
|
133
|
-
let filePath = join(srcDir,
|
|
142
|
+
let filePath = join(srcDir, url + (url?.endsWith("/") ? "index.html" : ""));
|
|
134
143
|
// check if the requested html file does not exist
|
|
135
144
|
if (filePath.endsWith(".html") && !existsSync(filePath)) {
|
|
136
145
|
filePath = filePath.replace(".html", ext);
|
|
@@ -143,7 +152,7 @@ export default function plugin(options = {}) {
|
|
|
143
152
|
}
|
|
144
153
|
// try checking if the requested asset exists in the `src` folder
|
|
145
154
|
if (filePath.lastIndexOf(".") > -1) {
|
|
146
|
-
filePath = join(config.root,
|
|
155
|
+
filePath = join(config.root, url || "");
|
|
147
156
|
if (existsSync(filePath)) {
|
|
148
157
|
// direct the file path relative to the `src` folder
|
|
149
158
|
req.url = filePath;
|
|
@@ -191,7 +200,9 @@ export default function plugin(options = {}) {
|
|
|
191
200
|
* Grab the template file
|
|
192
201
|
*/
|
|
193
202
|
const templateFile = join(outputDir, opt.template);
|
|
194
|
-
const template = await readFile(templateFile, {
|
|
203
|
+
const template = await readFile(templateFile, {
|
|
204
|
+
encoding: "utf-8"
|
|
205
|
+
});
|
|
195
206
|
/**
|
|
196
207
|
* Capture the pages
|
|
197
208
|
*/
|
|
@@ -210,7 +221,9 @@ export default function plugin(options = {}) {
|
|
|
210
221
|
else {
|
|
211
222
|
// ...
|
|
212
223
|
}
|
|
213
|
-
const fileContent = await readFile(filePath, {
|
|
224
|
+
const fileContent = await readFile(filePath, {
|
|
225
|
+
encoding: "utf-8"
|
|
226
|
+
});
|
|
214
227
|
const compiledPage = compile(filePath, fileContent);
|
|
215
228
|
const result = template.replace(opt.slot, compiledPage);
|
|
216
229
|
log("build: ", page);
|
|
@@ -249,7 +262,7 @@ export default function plugin(options = {}) {
|
|
|
249
262
|
/**
|
|
250
263
|
* Copy the entire unit dist folder to the root of the project
|
|
251
264
|
*/
|
|
252
|
-
await cp(outputDir
|
|
265
|
+
await cp(`${outputDir}dist`, distFolder, { recursive: true });
|
|
253
266
|
// await new Promise((res) => setTimeout(res, 5000));
|
|
254
267
|
/**
|
|
255
268
|
* Delete the entire unit output folder
|
package/package.json
CHANGED
|
@@ -1,75 +1,60 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
"devDependencies": {
|
|
62
|
-
"@release-it/conventional-changelog": "^8.0.1",
|
|
63
|
-
"@types/node": "^20.12.2",
|
|
64
|
-
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
65
|
-
"@typescript-eslint/parser": "^6.21.0",
|
|
66
|
-
"eslint": "^8.57.0",
|
|
67
|
-
"eslint-plugin-prettier": "^5.1.3",
|
|
68
|
-
"husky": "^8.0.3",
|
|
69
|
-
"lint-staged": "^15.2.2",
|
|
70
|
-
"prettier": "^3.2.5",
|
|
71
|
-
"release-it": "^17.1.1",
|
|
72
|
-
"typescript": "^5.4.3",
|
|
73
|
-
"vite": "^5.2.7"
|
|
74
|
-
}
|
|
2
|
+
"name": "vite-plugin-unit",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A vite plugin to enable you build websites in units.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"./dist/",
|
|
10
|
+
"./LICENSE.md",
|
|
11
|
+
"./README.md",
|
|
12
|
+
"./package.json"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/henryhale/vite-plugin-unit.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"vite",
|
|
20
|
+
"static",
|
|
21
|
+
"site",
|
|
22
|
+
"static site generator",
|
|
23
|
+
"ui",
|
|
24
|
+
"website",
|
|
25
|
+
"browser"
|
|
26
|
+
],
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Henry Hale",
|
|
29
|
+
"url": "https://github.com/henryhale"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/henryhale/vite-plugin-unit/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/henryhale/vite-plugin-unit#readme",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -b",
|
|
39
|
+
"dev": "tsc -w",
|
|
40
|
+
"lint": "biome check",
|
|
41
|
+
"lint:fix": "biome check --fix",
|
|
42
|
+
"prepack": "pnpm build",
|
|
43
|
+
"prepare": "husky || true",
|
|
44
|
+
"release": "release-it",
|
|
45
|
+
"test": "echo \"Error: no test specified\""
|
|
46
|
+
},
|
|
47
|
+
"lint-staged": {
|
|
48
|
+
"*.ts": "pnpm lint:fix"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@biomejs/biome": "^2.4.10",
|
|
52
|
+
"@release-it/conventional-changelog": "^10.0.6",
|
|
53
|
+
"@types/node": "^25.5.2",
|
|
54
|
+
"husky": "^9.1.7",
|
|
55
|
+
"lint-staged": "^16.4.0",
|
|
56
|
+
"release-it": "^19.2.4",
|
|
57
|
+
"typescript": "^6.0.2",
|
|
58
|
+
"vite": "^8.0.3"
|
|
59
|
+
}
|
|
75
60
|
}
|