vite-plugin-unit 0.0.5 → 1.0.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/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  A vite plugin to enable you build websites in units using alpine.js
6
6
 
7
+ Check out the [create-unit](https://github.com/henryhale/create-unit) library for details.
8
+
7
9
  ## installation
8
10
 
9
11
  ```sh
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * vite-plugin-unit
3
- * @description A vite plugin to enable you build websites in units using alpine.js
3
+ * @description A vite plugin to enable you build websites in units.
4
4
  * @author Henry Hale
5
5
  * @license MIT
6
6
  * @url https://github.com/henryhale/vite-plugin-unit
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * vite-plugin-unit
3
- * @description A vite plugin to enable you build websites in units using alpine.js
3
+ * @description A vite plugin to enable you build websites in units.
4
4
  * @author Henry Hale
5
5
  * @license MIT
6
6
  * @url https://github.com/henryhale/vite-plugin-unit
@@ -12,7 +12,7 @@ import { log } from "node:console";
12
12
  const defaultOptions = {
13
13
  pages: "pages/",
14
14
  template: "template.html",
15
- slot: "#slot#"
15
+ slot: "<slot></slot>"
16
16
  };
17
17
  export default function plugin(options = {}) {
18
18
  const opt = Object.assign({}, defaultOptions, options);
@@ -23,9 +23,17 @@ export default function plugin(options = {}) {
23
23
  // source code folder
24
24
  const srcDir = "src/";
25
25
  // intermediate output folder for unit.js
26
- const outputDir = "./.unit/";
27
- // regexp to match single html tag
26
+ const outputDir = ".unit/";
27
+ // regexp to match single html tag with attributes
28
28
  const htmlRegex = /<(.+?) ([/]?|.+?)>/g;
29
+ // import statements
30
+ const importRegex = /import (.+?) from "(.+?)"(.+?)/g;
31
+ // html tag regexp
32
+ const tagRegex = /<(.+?)>/;
33
+ // regexp to match attributes in html tag
34
+ const attrRegex = /(\w+(?:-\w+)*)\s*=\s*["']([^"']+)["']/g;
35
+ // regexp to match placeholders like {text}
36
+ const valueRegex = /{(\w*)}/g;
29
37
  // mapping file path to thier contents
30
38
  const pathToCode = new Map();
31
39
  // function that compiles .unit files
@@ -33,9 +41,18 @@ export default function plugin(options = {}) {
33
41
  let filePath = null;
34
42
  let content = null;
35
43
  const nameToPath = new Map();
44
+ // create a key-value object from attributes of an html tag
45
+ function mapAttributes(attr = "") {
46
+ const map = {};
47
+ let match;
48
+ while ((match = attrRegex.exec(attr)) !== null) {
49
+ map[match[1]] = match[2];
50
+ }
51
+ return map;
52
+ }
36
53
  // remove all import statements while saving the import names & content
37
54
  return (code
38
- .replace(/import (.+?) from "(.+?)"(.+?)/g, (_, importName, importPath) => {
55
+ .replace(importRegex, (_, importName, importPath) => {
39
56
  filePath = join(dirname(file), importPath);
40
57
  nameToPath.set(importName, filePath);
41
58
  if (!pathToCode.has(filePath)) {
@@ -48,9 +65,26 @@ export default function plugin(options = {}) {
48
65
  .replace(htmlRegex, (match, tag, attr = "") => {
49
66
  const path = nameToPath.get(tag);
50
67
  if (path) {
68
+ // get rid of trailing forward slash
51
69
  if (attr.endsWith("/"))
52
70
  attr = attr.slice(0, -1);
53
- return pathToCode.get(path).replace(/<(.+?)>/, (m) => m.slice(0, -1) + " " + attr + ">");
71
+ const map = mapAttributes(attr);
72
+ return pathToCode
73
+ .get(path)
74
+ .replace(valueRegex, (m, key) => {
75
+ const value = map[key];
76
+ if (value) {
77
+ delete map[key];
78
+ return value;
79
+ }
80
+ return m;
81
+ })
82
+ .replace(tagRegex, (m) => {
83
+ const others = Object.entries(map).reduce((r, [k, v]) => {
84
+ return r + k + "=" + (/'/.test(v) ? `"${v}"` : `'${v}'`);
85
+ }, "");
86
+ return m.slice(0, -1) + " " + others + ">";
87
+ });
54
88
  }
55
89
  return match;
56
90
  })
@@ -180,7 +214,12 @@ export default function plugin(options = {}) {
180
214
  const compiledPage = compile(filePath, fileContent);
181
215
  const result = template.replace(opt.slot, compiledPage);
182
216
  log("build: ", page);
183
- return await writeFile(join(outputDir, page.replace(ext, ".html")), result);
217
+ const fullPath = join(outputDir, page.replace(ext, ".html"));
218
+ const dirPath = dirname(fullPath);
219
+ if (!existsSync(dirPath)) {
220
+ await mkdir(dirPath, { recursive: true });
221
+ }
222
+ return await writeFile(fullPath, result);
184
223
  }
185
224
  /**
186
225
  * Clear the input files
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-unit",
3
- "version": "0.0.5",
4
- "description": "A vite plugin to enable you build websites in units using alpine.js",
3
+ "version": "1.0.0",
4
+ "description": "A vite plugin to enable you build websites in units.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -17,8 +17,11 @@
17
17
  },
18
18
  "keywords": [
19
19
  "vite",
20
- "alpine",
20
+ "static",
21
+ "site",
22
+ "static site generator",
21
23
  "ui",
24
+ "website",
22
25
  "browser"
23
26
  ],
24
27
  "author": {
@@ -57,16 +60,16 @@
57
60
  },
58
61
  "devDependencies": {
59
62
  "@release-it/conventional-changelog": "^8.0.1",
60
- "@types/node": "^20.10.6",
61
- "@typescript-eslint/eslint-plugin": "^6.16.0",
62
- "@typescript-eslint/parser": "^6.16.0",
63
- "eslint": "^8.56.0",
64
- "eslint-plugin-prettier": "^5.1.2",
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",
65
68
  "husky": "^8.0.3",
66
- "lint-staged": "^15.2.0",
67
- "prettier": "^3.1.1",
68
- "release-it": "^17.0.1",
69
- "typescript": "^5.3.3",
70
- "vite": "^5.0.11"
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"
71
74
  }
72
75
  }