components-ejs 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/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 Pigly3
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # components-ejs
2
+
3
+ Adds components on top of EJS.
4
+
5
+ ```html
6
+ <Component src="component.ejs">
7
+ <p>
8
+ Other HTML elements can go inside components.
9
+ </p>
10
+ </Component>
11
+ ```
12
+
13
+ Components specify where to display the elements inside of them:
14
+ ```html
15
+ <div>
16
+ <%- innerHTML %>
17
+ </div>
18
+ ```
19
+
20
+ All attribtues on the component (except for src, which determines the component) are passed through to the component:
21
+ ```html
22
+ <div <%- stringifyAttributes() %>>
23
+ <%- innerHTML %>
24
+ </div>
25
+ ```
26
+
27
+ It can also consume attributes, removing them from the `stringifyAttributes()` output and allowing the component to use them in a different place than the other attributes. Attributes should be consumed at the top of the file to ensure they are fully consumed.
28
+ ```html
29
+ <% const attrs = consumeAttributes("id", "class") %>
30
+ <% const name = consumeAttribute("name") %>
31
+
32
+ <div <%- stringifyAttributes() %>>
33
+ <%- innerHTML %>
34
+ </div>
35
+ ```
36
+
37
+ The package provides the following exported functions:
38
+ ```typescript
39
+ export async function renderFile(path:string, args={}, ejsOptions={}): Promise<string>
40
+ export async function render(src:string, args={}, path="raw", ejsOptions={}): Promise<string>
41
+ ```
42
+
43
+ The path argument in `render` is only used to identify the code in error messages.
@@ -0,0 +1,108 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // index.ts
40
+ var exports_component_ejs = {};
41
+ __export(exports_component_ejs, {
42
+ render: () => render,
43
+ renderFile: () => renderFile
44
+ });
45
+ module.exports = __toCommonJS(exports_component_ejs);
46
+ var import_promises = require("node:fs/promises");
47
+ var ejs = require("ejs");
48
+ var utilEJS = `
49
+ <%
50
+ function consumeAttribute(name) {
51
+ let temp = attributes[name]
52
+ if (temp) {
53
+ delete attributes[name]
54
+ return temp
55
+ } else return ""
56
+ }
57
+
58
+ function consumeAttributes(...names){
59
+ out = {}
60
+ for (const name of names){
61
+ out[name] = consumeAttribute(name)
62
+ }
63
+ return out
64
+ }
65
+ function stringifyAttributes() {
66
+ let out = ""
67
+ for (const attribute in attributes) out += attribute + '="' + attributes[attribute] + '"'
68
+ return out
69
+ }
70
+ %>
71
+ `;
72
+ async function replaceAll(str, regex, replacer) {
73
+ const matches = [...str.matchAll(regex)];
74
+ const replacements = await Promise.all(matches.map((match) => replacer(...match)));
75
+ let out = "";
76
+ let lastIndex = 0;
77
+ matches.forEach((match, i) => {
78
+ out += str.slice(lastIndex, match.index);
79
+ out += replacements[i];
80
+ lastIndex = match.index + match[0].length;
81
+ });
82
+ return out + str.slice(lastIndex);
83
+ }
84
+ async function render(src, args = {}, path = "raw", ejsOptions = {}) {
85
+ const data = ejs.render(src, args, ejsOptions);
86
+ return await replaceAll(data, /<Component([\s\S]*?)>([\s\S]*?)<\/Component>/g, async (match, p1, p2) => {
87
+ const attributes = {};
88
+ const attributeRegex = /([A-z]*)="((?:\\.|[^"\\])*)"/g;
89
+ const matches = [...p1.matchAll(attributeRegex)];
90
+ for (const match2 of matches)
91
+ attributes[match2[1]] = match2[2];
92
+ if (!attributes.src) {
93
+ console.error(`Cannot load component in ${path} without source.`);
94
+ return;
95
+ }
96
+ const componentSrc = attributes.src;
97
+ const componentArgs = attributes.args ? JSON.parse(attributes.args) : {};
98
+ delete attributes.src;
99
+ delete attributes.args;
100
+ componentArgs["attributes"] = attributes;
101
+ componentArgs["innerHTML"] = p2;
102
+ const componentData = await import_promises.readFile(componentSrc, "utf8");
103
+ return await render(utilEJS + componentData, componentArgs, componentSrc, ejsOptions);
104
+ });
105
+ }
106
+ async function renderFile(path, args = {}, ejsOptions = {}) {
107
+ return await render(await import_promises.readFile(path, "utf8"), args, path, ejsOptions);
108
+ }
@@ -0,0 +1,71 @@
1
+ import { createRequire } from "node:module";
2
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
+
4
+ // index.ts
5
+ import { readFile } from "node:fs/promises";
6
+ var ejs = __require("ejs");
7
+ var utilEJS = `
8
+ <%
9
+ function consumeAttribute(name) {
10
+ let temp = attributes[name]
11
+ if (temp) {
12
+ delete attributes[name]
13
+ return temp
14
+ } else return ""
15
+ }
16
+
17
+ function consumeAttributes(...names){
18
+ out = {}
19
+ for (const name of names){
20
+ out[name] = consumeAttribute(name)
21
+ }
22
+ return out
23
+ }
24
+ function stringifyAttributes() {
25
+ let out = ""
26
+ for (const attribute in attributes) out += attribute + '="' + attributes[attribute] + '"'
27
+ return out
28
+ }
29
+ %>
30
+ `;
31
+ async function replaceAll(str, regex, replacer) {
32
+ const matches = [...str.matchAll(regex)];
33
+ const replacements = await Promise.all(matches.map((match) => replacer(...match)));
34
+ let out = "";
35
+ let lastIndex = 0;
36
+ matches.forEach((match, i) => {
37
+ out += str.slice(lastIndex, match.index);
38
+ out += replacements[i];
39
+ lastIndex = match.index + match[0].length;
40
+ });
41
+ return out + str.slice(lastIndex);
42
+ }
43
+ async function render(src, args = {}, path = "raw", ejsOptions = {}) {
44
+ const data = ejs.render(src, args, ejsOptions);
45
+ return await replaceAll(data, /<Component([\s\S]*?)>([\s\S]*?)<\/Component>/g, async (match, p1, p2) => {
46
+ const attributes = {};
47
+ const attributeRegex = /([A-z]*)="((?:\\.|[^"\\])*)"/g;
48
+ const matches = [...p1.matchAll(attributeRegex)];
49
+ for (const match2 of matches)
50
+ attributes[match2[1]] = match2[2];
51
+ if (!attributes.src) {
52
+ console.error(`Cannot load component in ${path} without source.`);
53
+ return;
54
+ }
55
+ const componentSrc = attributes.src;
56
+ const componentArgs = attributes.args ? JSON.parse(attributes.args) : {};
57
+ delete attributes.src;
58
+ delete attributes.args;
59
+ componentArgs["attributes"] = attributes;
60
+ componentArgs["innerHTML"] = p2;
61
+ const componentData = await readFile(componentSrc, "utf8");
62
+ return await render(utilEJS + componentData, componentArgs, componentSrc, ejsOptions);
63
+ });
64
+ }
65
+ async function renderFile(path, args = {}, ejsOptions = {}) {
66
+ return await render(await readFile(path, "utf8"), args, path, ejsOptions);
67
+ }
68
+ export {
69
+ render,
70
+ renderFile
71
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "components-ejs",
3
+ "module": "./dist/index.js",
4
+ "main": "./dist/index.cjs",
5
+ "description": "Adds components on top of EJS.",
6
+ "type": "module",
7
+ "devDependencies": {
8
+ "@types/bun": "latest"
9
+ },
10
+ "peerDependencies": {
11
+ "typescript": "^7"
12
+ },
13
+ "dependencies": {
14
+ "ejs": "^6.0.1"
15
+ },
16
+ "scripts": {
17
+ "build": "bun run build.ts"
18
+ },
19
+ "files": ["dist"],
20
+ "keywords": ["ejs", "component"],
21
+ "license": "MIT",
22
+ "author": "Pigly3",
23
+ "exports": {
24
+ "import": "./dist/index.js",
25
+ "require": "./dist/index.cjs"
26
+ },
27
+ "version": "1.0.0"
28
+ }