olum-compiler 1.6.2 → 1.7.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/bundle.js CHANGED
@@ -99,6 +99,25 @@ async function bundle(bootAt) {
99
99
  build: {
100
100
  outDir,
101
101
  emptyOutDir: true,
102
+
103
+ assetsInlineLimit: 0,
104
+ rollupOptions: {
105
+ output: {
106
+ assetFileNames(info) {
107
+ const orig = (info.originalFileNames || [])[0];
108
+ const name = (info.names || [])[0] || "";
109
+ if (
110
+ orig &&
111
+ !orig.startsWith("..") &&
112
+ !path.isAbsolute(orig) &&
113
+ path.extname(orig) === path.extname(name)
114
+ ) {
115
+ return orig;
116
+ }
117
+ return "assets/[name]-[hash][extname]";
118
+ },
119
+ },
120
+ },
102
121
  },
103
122
  });
104
123
 
package/bin/server.js CHANGED
@@ -2,7 +2,7 @@ const fs = require("fs");
2
2
  const path = require("path");
3
3
  const http = require("http");
4
4
 
5
- const jsdom = require("jsdom");
5
+ const { parseDOM } = require("../lib/dom");
6
6
  const colors = require("../lib/colors");
7
7
  const mimes = require("./mimes.json");
8
8
  let wsPort = 8090;
@@ -58,7 +58,7 @@ function serve(entry, port, mode = "development") {
58
58
  res.end("index.html not found");
59
59
  return reject(err);
60
60
  }
61
- const dom = new jsdom.JSDOM(content.toString()).window.document;
61
+ const dom = parseDOM(content.toString());
62
62
  const html = dom.querySelector("html");
63
63
  if (mode == "development") {
64
64
  const wsContent = handleWsFile();
package/lib/changes.txt CHANGED
@@ -1,2 +0,0 @@
1
- change /home/eissapk/olum/template-compiler/src/page.html
2
- change /home/eissapk/olum/template-compiler/src/page.html
package/lib/dom.js ADDED
@@ -0,0 +1,216 @@
1
+ const parse5 = require("parse5");
2
+
3
+ const P5_OPTS = { scriptingEnabled: false };
4
+
5
+ const wrapCache = new WeakMap();
6
+
7
+ function wrap(node) {
8
+ if (node == null) return null;
9
+ let w = wrapCache.get(node);
10
+ if (!w) {
11
+ w = new NodeWrapper(node);
12
+ wrapCache.set(node, w);
13
+ }
14
+ return w;
15
+ }
16
+
17
+ const isElement = (node) => !!node && typeof node.tagName === "string";
18
+ const isText = (node) => !!node && node.nodeName === "#text";
19
+
20
+ function compileSelector(selector) {
21
+ const m = String(selector)
22
+ .trim()
23
+ .match(/^([a-zA-Z][\w-]*|\*)?(?:\[([a-zA-Z][\w-]*)\])?$/);
24
+ if (!m || (!m[1] && !m[2]))
25
+ throw new Error("dom.js: unsupported selector: " + selector);
26
+ const tag = m[1] && m[1] !== "*" ? m[1].toLowerCase() : null;
27
+ const attr = m[2] ? m[2].toLowerCase() : null;
28
+ return (node) =>
29
+ (!tag || node.tagName === tag) &&
30
+ (!attr || node.attrs.some((a) => a.name === attr));
31
+ }
32
+
33
+ function walk(node, visit) {
34
+ for (const child of node.childNodes || []) {
35
+ if (isElement(child)) {
36
+ visit(child);
37
+ walk(child, visit);
38
+ }
39
+ }
40
+ }
41
+
42
+ function parseInContext(contextNode, html) {
43
+ const context = isElement(contextNode)
44
+ ? contextNode
45
+ : findTag(contextNode, "body");
46
+ const frag = parse5.parseFragment(context, String(html), P5_OPTS);
47
+ return frag.childNodes.slice();
48
+ }
49
+
50
+ function findTag(root, tag) {
51
+ let found = null;
52
+ walk(root, (node) => {
53
+ if (!found && node.tagName === tag) found = node;
54
+ });
55
+ return found;
56
+ }
57
+
58
+ function detach(node) {
59
+ const parent = node.parentNode;
60
+ if (!parent) return;
61
+ const i = parent.childNodes.indexOf(node);
62
+ if (i !== -1) parent.childNodes.splice(i, 1);
63
+ node.parentNode = null;
64
+ }
65
+
66
+ function insertNodesBefore(parent, nodes, refNode) {
67
+ const i = refNode ? parent.childNodes.indexOf(refNode) : -1;
68
+ const at = i === -1 ? parent.childNodes.length : i;
69
+ nodes.forEach((n) => {
70
+ n.parentNode = parent;
71
+ });
72
+ parent.childNodes.splice(at, 0, ...nodes);
73
+ }
74
+
75
+ class NodeWrapper {
76
+ constructor(node) {
77
+ this.node = node;
78
+ }
79
+
80
+ get tagName() {
81
+ return this.node.tagName
82
+ ? this.node.tagName.toUpperCase()
83
+ : this.node.nodeName;
84
+ }
85
+ get nodeName() {
86
+ return this.tagName;
87
+ }
88
+
89
+ hasAttribute(name) {
90
+ name = String(name).toLowerCase();
91
+ return (this.node.attrs || []).some((a) => a.name === name);
92
+ }
93
+ getAttribute(name) {
94
+ name = String(name).toLowerCase();
95
+ const attr = (this.node.attrs || []).find((a) => a.name === name);
96
+ return attr ? attr.value : null;
97
+ }
98
+ setAttribute(name, value) {
99
+ name = String(name).toLowerCase();
100
+ const attr = (this.node.attrs || []).find((a) => a.name === name);
101
+ if (attr) attr.value = String(value);
102
+ else this.node.attrs.push({ name, value: String(value) });
103
+ }
104
+ removeAttribute(name) {
105
+ name = String(name).toLowerCase();
106
+ const i = (this.node.attrs || []).findIndex((a) => a.name === name);
107
+ if (i !== -1) this.node.attrs.splice(i, 1);
108
+ }
109
+ getAttributeNames() {
110
+ return (this.node.attrs || []).map((a) => a.name);
111
+ }
112
+
113
+ get children() {
114
+ return (this.node.childNodes || []).filter(isElement).map(wrap);
115
+ }
116
+ get firstElementChild() {
117
+ return wrap((this.node.childNodes || []).find(isElement) || null);
118
+ }
119
+ get firstChild() {
120
+ return wrap((this.node.childNodes || [])[0] || null);
121
+ }
122
+ get parentNode() {
123
+ return wrap(this.node.parentNode || null);
124
+ }
125
+ get parentElement() {
126
+ const parent = this.node.parentNode;
127
+ return isElement(parent) ? wrap(parent) : null;
128
+ }
129
+ get nextElementSibling() {
130
+ const parent = this.node.parentNode;
131
+ if (!parent) return null;
132
+ const siblings = parent.childNodes;
133
+ for (let i = siblings.indexOf(this.node) + 1; i < siblings.length; i++) {
134
+ if (isElement(siblings[i])) return wrap(siblings[i]);
135
+ }
136
+ return null;
137
+ }
138
+ get body() {
139
+ return wrap(findTag(this.node, "body"));
140
+ }
141
+
142
+ querySelectorAll(selector) {
143
+ const matches = compileSelector(selector);
144
+ const out = [];
145
+ walk(this.node, (node) => {
146
+ if (matches(node)) out.push(wrap(node));
147
+ });
148
+ return out;
149
+ }
150
+ querySelector(selector) {
151
+ return this.querySelectorAll(selector)[0] || null;
152
+ }
153
+
154
+ get innerHTML() {
155
+ return parse5.serialize(this.node, P5_OPTS);
156
+ }
157
+ set innerHTML(html) {
158
+ const nodes = parseInContext(this.node, html);
159
+ this.node.childNodes = [];
160
+ insertNodesBefore(this.node, nodes, null);
161
+ }
162
+ get outerHTML() {
163
+ return parse5.serializeOuter(this.node, P5_OPTS);
164
+ }
165
+ get textContent() {
166
+ let out = "";
167
+ (function collect(node) {
168
+ for (const child of node.childNodes || []) {
169
+ if (isText(child)) out += child.value;
170
+ else collect(child);
171
+ }
172
+ })(this.node);
173
+ return out;
174
+ }
175
+ set textContent(value) {
176
+ this.node.childNodes = [];
177
+ if (String(value)) {
178
+ this.node.childNodes.push({
179
+ nodeName: "#text",
180
+ value: String(value),
181
+ parentNode: this.node,
182
+ });
183
+ }
184
+ }
185
+
186
+ insertAdjacentHTML(position, html) {
187
+ if (position === "beforebegin") {
188
+ const parent = this.node.parentNode;
189
+ insertNodesBefore(parent, parseInContext(parent, html), this.node);
190
+ } else if (position === "beforeend") {
191
+ insertNodesBefore(this.node, parseInContext(this.node, html), null);
192
+ } else {
193
+ throw new Error(
194
+ "dom.js: unsupported insertAdjacentHTML position: " + position,
195
+ );
196
+ }
197
+ }
198
+ insertBefore(newWrapper, refWrapper) {
199
+ detach(newWrapper.node);
200
+ insertNodesBefore(
201
+ this.node,
202
+ [newWrapper.node],
203
+ refWrapper ? refWrapper.node : null,
204
+ );
205
+ return newWrapper;
206
+ }
207
+ remove() {
208
+ detach(this.node);
209
+ }
210
+ }
211
+
212
+ function parseDOM(html) {
213
+ return wrap(parse5.parse(String(html), P5_OPTS));
214
+ }
215
+
216
+ module.exports = { parseDOM };
package/lib/htmlParser.js CHANGED
@@ -1,4 +1,4 @@
1
- const jsdom = require("jsdom");
1
+ const { parseDOM } = require("./dom");
2
2
  const { script, style, selfClosingTag, normalTag } = require("./regex");
3
3
  const placeholder = require("./placeholder");
4
4
  const clean = require("./clean");
@@ -43,7 +43,7 @@ function htmlparser(str, counter, filePath, parentMethods, propsAliases) {
43
43
 
44
44
  const anonHandlers = [];
45
45
 
46
- const body = new jsdom.JSDOM(shieldTags(content)).window.document.body;
46
+ const body = parseDOM(shieldTags(content)).body;
47
47
  handleHtml(body);
48
48
  handleLog(body, counter, filePath);
49
49
  handleTransition(body, counter, filePath);
package/lib/jsParser.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { script, exportDefault } = require("./regex");
2
- const jsdom = require("jsdom");
2
+ const { parseDOM } = require("./dom");
3
3
 
4
4
  const clean = require("./clean");
5
5
  function jsParser(str) {
@@ -9,7 +9,7 @@ function jsParser(str) {
9
9
 
10
10
  const attrsObj = {};
11
11
  if (content !== "") {
12
- const dom = new jsdom.JSDOM(content).window.document;
12
+ const dom = parseDOM(content);
13
13
  const scriptElm = dom.querySelector("script");
14
14
  const attrs = scriptElm.getAttributeNames();
15
15
  if (attrs.length)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -29,6 +29,10 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "acorn": "^8.17.0",
32
+ "acorn-walk": "^8.3.5",
33
+ "parse5": "^8.0.1"
34
+ },
35
+ "devDependencies": {
32
36
  "jsdom": "^19.0.0"
33
37
  }
34
38
  }
@@ -128,7 +128,7 @@ check(
128
128
  );
129
129
 
130
130
  check(
131
- "comparison operators in a text {expr} survive the jsdom round-trip",
131
+ "comparison operators in a text {expr} survive the parse5 round-trip",
132
132
  comp(`<p>{state.width > 380 && state.width < 800 ? 'mid' : 'other'}</p>`),
133
133
  (out) =>
134
134
  /olum\.esc\(state\.width > 380 && state\.width < 800 \? 'mid' : 'other'\)/.test(