olum-compiler 1.2.0 → 1.6.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/.claude/settings.local.json +3 -1
- package/bin/build.js +14 -8
- package/bin/bundle.js +108 -0
- package/bin/compiler.js +25 -7
- package/bin/copySrc.js +73 -25
- package/bin/dev.js +54 -34
- package/bin/index.js +6 -4
- package/bin/server.js +58 -34
- package/bin/watcher.js +15 -11
- package/bin/ws.js +16 -11
- package/lib/attr.js +1 -25
- package/lib/boolAttr.js +32 -15
- package/lib/changes.txt +0 -0
- package/lib/chokidar.js +52 -101
- package/lib/colors.js +1 -7
- package/lib/compPlaceholder.js +5 -3
- package/lib/compiler.js +11 -8
- package/lib/coreLibs.js +2 -7
- package/lib/cross-spawn.js +45 -59
- package/lib/cssParser.js +61 -37
- package/lib/domEvents.js +6 -2
- package/lib/events.js +75 -77
- package/lib/for.js +56 -28
- package/lib/getImports.js +35 -17
- package/lib/getTempPath.js +8 -4
- package/lib/helpers.js +289 -184
- package/lib/html.js +3 -10
- package/lib/htmlParser.js +45 -45
- package/lib/if.js +40 -20
- package/lib/importMap.js +49 -24
- package/lib/jsParser.js +8 -13
- package/lib/lineCounter.js +18 -5
- package/lib/liveProps.js +266 -0
- package/lib/log.js +30 -0
- package/lib/magic-string.js +45 -52
- package/lib/models.js +8 -7
- package/lib/parser.js +82 -43
- package/lib/placeholder.js +73 -58
- package/lib/regex.js +4 -3
- package/lib/show.js +16 -8
- package/lib/sourcemap.js +23 -24
- package/lib/styles.js +7 -13
- package/lib/textarea.js +3 -6
- package/lib/toJs.js +1 -1
- package/lib/tokens.js +10 -22
- package/lib/transition.js +86 -0
- package/lib/websocket.js +68 -62
- package/package.json +1 -1
- package/tests/compiler.test.js +450 -172
package/lib/html.js
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
const { RAW_OPEN, RAW_CLOSE, shieldOps } = require("./tokens");
|
|
2
2
|
|
|
3
|
-
// `html="expr"` — render the expression as UNESCAPED HTML, replacing the element's content
|
|
4
|
-
// (like Vue's v-html / React's dangerouslySetInnerHTML). It's a "code attribute": the "" holds a
|
|
5
|
-
// JS expression. Output is a raw `${expr}` interpolation (NOT wrapped in olum.esc), so use it only
|
|
6
|
-
// on trusted/sanitized HTML. The per-value primitive olum.html(x) still exists for inline/mixed use.
|
|
7
|
-
//
|
|
8
|
-
// Runs before the control-flow handlers (which dissolve elements into template text), so an html
|
|
9
|
-
// element inside <for>/<if> is rewritten while it's still a real DOM node.
|
|
10
3
|
function handleHtml(body) {
|
|
11
4
|
const elms = new Array().slice.call(body.querySelectorAll("[html]"));
|
|
12
5
|
elms.forEach((item) => {
|
|
13
|
-
const expr = shieldOps((item.getAttribute("html") || "").trim());
|
|
6
|
+
const expr = shieldOps((item.getAttribute("html") || "").trim());
|
|
14
7
|
item.removeAttribute("html");
|
|
15
8
|
if (!expr) return;
|
|
16
|
-
const encoded = expr.replace(/{/g, "<-olum->");
|
|
17
|
-
item.innerHTML = RAW_OPEN + encoded + RAW_CLOSE;
|
|
9
|
+
const encoded = expr.replace(/{/g, "<-olum->");
|
|
10
|
+
item.innerHTML = RAW_OPEN + encoded + RAW_CLOSE;
|
|
18
11
|
});
|
|
19
12
|
}
|
|
20
13
|
|
package/lib/htmlParser.js
CHANGED
|
@@ -6,38 +6,50 @@ const handleIfStatement = require("./if");
|
|
|
6
6
|
const handleForStatement = require("./for");
|
|
7
7
|
const handleShowStatement = require("./show");
|
|
8
8
|
const handleEvents = require("./events");
|
|
9
|
-
|
|
10
|
-
// const handleModels = require("./models");
|
|
11
|
-
// const handleAttr = require("./attr");
|
|
9
|
+
|
|
12
10
|
const handleCompPlaceholder = require("./compPlaceholder");
|
|
13
11
|
const handleHtml = require("./html");
|
|
12
|
+
const handleLog = require("./log");
|
|
13
|
+
const handleTransition = require("./transition");
|
|
14
14
|
const handleBoolAttrs = require("./boolAttr");
|
|
15
15
|
const handleTextarea = require("./textarea");
|
|
16
|
-
const {
|
|
16
|
+
const {
|
|
17
|
+
RAW_OPEN,
|
|
18
|
+
RAW_CLOSE,
|
|
19
|
+
LT,
|
|
20
|
+
GT,
|
|
21
|
+
QUOT,
|
|
22
|
+
shieldTags,
|
|
23
|
+
unshieldTags,
|
|
24
|
+
} = require("./tokens");
|
|
17
25
|
|
|
18
26
|
function htmlparser(str, counter, filePath, parentMethods, propsAliases) {
|
|
19
|
-
let content = str
|
|
20
|
-
content =
|
|
21
|
-
content = placeholder(
|
|
22
|
-
|
|
27
|
+
let content = clean(str);
|
|
28
|
+
content = content.replace(script, "").replace(style, "");
|
|
29
|
+
content = placeholder(
|
|
30
|
+
content,
|
|
31
|
+
selfClosingTag,
|
|
32
|
+
"self",
|
|
33
|
+
parentMethods,
|
|
34
|
+
propsAliases,
|
|
35
|
+
);
|
|
36
|
+
content = placeholder(
|
|
37
|
+
content,
|
|
38
|
+
normalTag,
|
|
39
|
+
"normal",
|
|
40
|
+
parentMethods,
|
|
41
|
+
propsAliases,
|
|
42
|
+
);
|
|
23
43
|
|
|
24
|
-
// Inline arrow/function event handlers (oninput="(e)=> expr") are extracted into named
|
|
25
|
-
// __olumAnon_ methods inside handleEvents (collected here, injected into the component JS later).
|
|
26
44
|
const anonHandlers = [];
|
|
27
45
|
|
|
28
|
-
// using dom parser — shieldTags renames parser-restricted containers (select/table/tr/…) to
|
|
29
|
-
// unknown elements so a <for>/<if>/<show> inside them survives the parse; restored after serialize
|
|
30
46
|
const body = new jsdom.JSDOM(shieldTags(content)).window.document.body;
|
|
31
|
-
handleHtml(body);
|
|
32
|
-
|
|
47
|
+
handleHtml(body);
|
|
48
|
+
handleLog(body, counter, filePath);
|
|
49
|
+
handleTransition(body, counter, filePath);
|
|
50
|
+
handleTextarea(body);
|
|
33
51
|
handleEvents(body, counter, filePath, anonHandlers, propsAliases);
|
|
34
|
-
|
|
35
|
-
// {expr} interpolation instead, e.g. style="color:{state.color}". Uncomment to re-enable.
|
|
36
|
-
// handleStyles(body, counter, filePath);
|
|
37
|
-
// handleAttr(body, counter, filePath);
|
|
38
|
-
// DISABLED — `model` two-way binding. Bind manually instead: value="{state.x}" + an
|
|
39
|
-
// oninput handler that writes state.x. Uncomment to re-enable.
|
|
40
|
-
// handleModels(body, counter, filePath);
|
|
52
|
+
|
|
41
53
|
handleIfStatement(body, counter, filePath);
|
|
42
54
|
handleForStatement(body, counter, filePath);
|
|
43
55
|
handleShowStatement(body, counter, filePath);
|
|
@@ -45,41 +57,29 @@ function htmlparser(str, counter, filePath, parentMethods, propsAliases) {
|
|
|
45
57
|
|
|
46
58
|
content = unshieldTags(body.innerHTML);
|
|
47
59
|
|
|
48
|
-
// presence-based boolean attributes: checked="{expr}" -> ${(expr) ? "checked" : ""}.
|
|
49
|
-
// Must run before the {expr} escape pass below, which would otherwise interpolate the VALUE
|
|
50
|
-
// (checked="false" is still checked — the browser only checks attribute presence).
|
|
51
60
|
content = handleBoolAttrs(content);
|
|
52
61
|
|
|
53
|
-
// #2 (escape-by-default): at this point every literal `{expr}` left in the markup is a USER value
|
|
54
|
-
// interpolation (text like `{props.todo.text}` or a simple attribute like `id="todo-{x.id}"`).
|
|
55
|
-
// Control-flow interpolations were emitted with RAW_OPEN/RAW_CLOSE sentinels instead of braces,
|
|
56
|
-
// and nested function-body / object-literal braces are entity-encoded as `<-olum->`, so neither
|
|
57
|
-
// appears as a literal `{` here. Wrapping each remaining `{expr}` in `olum.esc(...)` therefore
|
|
58
|
-
// escapes user-facing output (preventing XSS and "special characters break the page" bugs) while
|
|
59
|
-
// leaving structural code untouched. `[^{}]+` keeps each match to a single, non-nested pair.
|
|
60
|
-
// slot content (`children` and `props().children`) is trusted markup authored in the parent
|
|
61
|
-
// template, not user input, so it is emitted RAW (no esc) — escaping it would show its tags as text.
|
|
62
62
|
content = content.replace(/\{([^{}]+)\}/g, (match, expr) => {
|
|
63
63
|
const raw = expr.trim();
|
|
64
64
|
if (raw === "children") return "${children}";
|
|
65
65
|
if (/^props\(\s*\)\.children$/.test(raw)) return "${" + raw + "}";
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
|
|
67
|
+
const js = expr
|
|
68
|
+
.replace(/</g, "<")
|
|
69
|
+
.replace(/>/g, ">")
|
|
70
|
+
.replace(/&/g, "&");
|
|
69
71
|
return "${olum.esc(" + js + ")}";
|
|
70
72
|
});
|
|
71
73
|
|
|
72
|
-
content = content.replace(/&/g, "&");
|
|
73
|
-
content = content.replace(/=>/g, "=>");
|
|
74
|
-
content = content.replace(/<-olum->|&lt;-olum-&gt;/g, "{");
|
|
75
|
-
content = content.split(LT).join("<").split(GT).join(">");
|
|
74
|
+
content = content.replace(/&/g, "&");
|
|
75
|
+
content = content.replace(/=>/g, "=>");
|
|
76
|
+
content = content.replace(/<-olum->|&lt;-olum-&gt;/g, "{");
|
|
77
|
+
content = content.split(LT).join("<").split(GT).join(">");
|
|
78
|
+
content = content.split(QUOT).join('"');
|
|
76
79
|
|
|
77
|
-
// #2: structural (compiler-generated) interpolations produce raw HTML, so emit them as plain
|
|
78
|
-
// `${...}` WITHOUT esc(). Done after the escape pass so user braces are already handled.
|
|
79
80
|
content = content.split(RAW_OPEN).join("${").split(RAW_CLOSE).join("}");
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return {html: content.trim(), comps, anonHandlers};
|
|
81
|
+
|
|
82
|
+
return { html: content.trim(), comps, anonHandlers };
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
module.exports = htmlparser;
|
package/lib/if.js
CHANGED
|
@@ -3,27 +3,35 @@ const { RAW_OPEN, RAW_CLOSE, shieldOps } = require("./tokens");
|
|
|
3
3
|
|
|
4
4
|
function hasElseIfOrElse(node) {
|
|
5
5
|
const nextNode = node.nextElementSibling;
|
|
6
|
-
if (
|
|
6
|
+
if (
|
|
7
|
+
nextNode &&
|
|
8
|
+
(nextNode.nodeName === "ELSE-IF" || nextNode.nodeName === "ELSE")
|
|
9
|
+
) {
|
|
7
10
|
return nextNode;
|
|
8
11
|
}
|
|
9
12
|
return false;
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
function handleIfStatement(body, counter, filePath) {
|
|
13
|
-
const ifElms = new Array().slice.call(body.querySelectorAll("if")).reverse();
|
|
14
|
-
ifElms.forEach(item => {
|
|
15
|
-
// console.log(item.outerHTML + "\n----------");
|
|
16
|
+
const ifElms = new Array().slice.call(body.querySelectorAll("if")).reverse();
|
|
17
|
+
ifElms.forEach((item) => {
|
|
16
18
|
if (item.hasAttribute("when")) {
|
|
17
|
-
// `when` is a plain JS expression (no braces): <if when="state.n > 0">
|
|
18
|
-
// shieldOps: protect <,> from the jsdom text round-trip (restored in htmlParser)
|
|
19
19
|
const condition = shieldOps(item.getAttribute("when").trim());
|
|
20
20
|
|
|
21
|
-
// start: store if statement value in olum tag
|
|
22
21
|
const olums = item.querySelectorAll("olum");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
|
|
23
|
+
olums.forEach((olum) =>
|
|
24
|
+
olum.setAttribute(
|
|
25
|
+
"if",
|
|
26
|
+
"-[[-" +
|
|
27
|
+
RAW_OPEN +
|
|
28
|
+
"JSON.stringify(!!" +
|
|
29
|
+
condition +
|
|
30
|
+
")" +
|
|
31
|
+
RAW_CLOSE +
|
|
32
|
+
"-]]-",
|
|
33
|
+
),
|
|
34
|
+
);
|
|
27
35
|
|
|
28
36
|
const innerContent = item.innerHTML;
|
|
29
37
|
|
|
@@ -32,29 +40,41 @@ function handleIfStatement(body, counter, filePath) {
|
|
|
32
40
|
let nodesArr = [{ content: innerContent, when: condition }];
|
|
33
41
|
function recursive(item) {
|
|
34
42
|
const node = hasElseIfOrElse(item);
|
|
35
|
-
nodesArr.push({
|
|
43
|
+
nodesArr.push({
|
|
44
|
+
content: node.innerHTML,
|
|
45
|
+
when: shieldOps((node.getAttribute("when") || "").trim()),
|
|
46
|
+
});
|
|
36
47
|
toBeRemovedArr.push(node);
|
|
37
48
|
if (hasElseIfOrElse(node)) recursive(node);
|
|
38
49
|
}
|
|
39
50
|
recursive(item);
|
|
40
|
-
|
|
41
|
-
// RAW_OPEN/RAW_CLOSE mark the structural interpolation braces so #2's escape pass keeps the
|
|
42
|
-
// ternary raw (its branches return HTML). User braces inside each branch are still escaped.
|
|
51
|
+
|
|
43
52
|
let ternaryOperator = RAW_OPEN;
|
|
44
53
|
nodesArr.forEach((node, index) => {
|
|
45
54
|
if (index === nodesArr.length - 1) {
|
|
46
|
-
if (node.when)
|
|
55
|
+
if (node.when)
|
|
56
|
+
ternaryOperator +=
|
|
57
|
+
node.when + "?`" + node.content + "`:''" + RAW_CLOSE;
|
|
47
58
|
else ternaryOperator += "`" + node.content + "`" + RAW_CLOSE;
|
|
48
59
|
} else {
|
|
49
60
|
ternaryOperator += node.when + "?`" + node.content + "`:";
|
|
50
61
|
}
|
|
51
62
|
});
|
|
52
|
-
|
|
63
|
+
|
|
53
64
|
item.insertAdjacentHTML("beforebegin", ternaryOperator);
|
|
54
|
-
toBeRemovedArr.forEach(item => item.remove());
|
|
65
|
+
toBeRemovedArr.forEach((item) => item.remove());
|
|
55
66
|
} else {
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
item.insertAdjacentHTML(
|
|
68
|
+
"beforebegin",
|
|
69
|
+
RAW_OPEN +
|
|
70
|
+
condition +
|
|
71
|
+
"?" +
|
|
72
|
+
"`" +
|
|
73
|
+
innerContent +
|
|
74
|
+
"`" +
|
|
75
|
+
":''" +
|
|
76
|
+
RAW_CLOSE,
|
|
77
|
+
);
|
|
58
78
|
}
|
|
59
79
|
item.remove();
|
|
60
80
|
} else {
|
package/lib/importMap.js
CHANGED
|
@@ -3,9 +3,8 @@ const path = require("path");
|
|
|
3
3
|
const colors = require("./colors");
|
|
4
4
|
const { ls, isFile } = require("./helpers");
|
|
5
5
|
|
|
6
|
-
const { useLocalCore, localLibs } = require("./coreLibs");
|
|
6
|
+
const { useLocalCore, localLibs } = require("./coreLibs");
|
|
7
7
|
|
|
8
|
-
// pick the browser-usable file from a package.json "exports" condition tree
|
|
9
8
|
function pickEntry(exp) {
|
|
10
9
|
if (typeof exp === "string") return exp;
|
|
11
10
|
if (Array.isArray(exp)) {
|
|
@@ -27,11 +26,16 @@ function pickEntry(exp) {
|
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
function resolvePackage(rootDir, pkgName) {
|
|
30
|
-
const pkgJsonPath = path.join(
|
|
29
|
+
const pkgJsonPath = path.join(
|
|
30
|
+
rootDir,
|
|
31
|
+
"node_modules",
|
|
32
|
+
pkgName,
|
|
33
|
+
"package.json",
|
|
34
|
+
);
|
|
31
35
|
if (!fs.existsSync(pkgJsonPath)) return null;
|
|
32
36
|
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
33
37
|
let exp = pkg.exports;
|
|
34
|
-
|
|
38
|
+
|
|
35
39
|
if (exp && typeof exp === "object" && !Array.isArray(exp)) {
|
|
36
40
|
const keys = Object.keys(exp);
|
|
37
41
|
if (keys.some((k) => k.startsWith("."))) exp = exp["."];
|
|
@@ -41,17 +45,19 @@ function resolvePackage(rootDir, pkgName) {
|
|
|
41
45
|
return "/node_modules/" + pkgName + "/" + entry;
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
// collect bare specifiers used in src: from "pkg" | import "pkg" | import("pkg")
|
|
45
48
|
function getBareSpecifiers(srcDir) {
|
|
46
49
|
const specifiers = new Set();
|
|
47
|
-
const files = ls(srcDir, { absolute: true }).filter(
|
|
50
|
+
const files = ls(srcDir, { absolute: true }).filter(
|
|
51
|
+
(p) => isFile(p) && /\.(html|js)$/i.test(p),
|
|
52
|
+
);
|
|
48
53
|
const regex = /(?:from\s+|import\s+|import\s*\(\s*)["']([^"'\n]+)["']/g;
|
|
49
54
|
files.forEach((file) => {
|
|
50
55
|
const content = fs.readFileSync(file, "utf8");
|
|
51
56
|
let match;
|
|
52
57
|
while ((match = regex.exec(content))) {
|
|
53
58
|
const spec = match[1];
|
|
54
|
-
if (spec.startsWith(".") || spec.startsWith("/") || /^https?:/.test(spec))
|
|
59
|
+
if (spec.startsWith(".") || spec.startsWith("/") || /^https?:/.test(spec))
|
|
60
|
+
continue;
|
|
55
61
|
specifiers.add(spec);
|
|
56
62
|
}
|
|
57
63
|
});
|
|
@@ -60,44 +66,59 @@ function getBareSpecifiers(srcDir) {
|
|
|
60
66
|
|
|
61
67
|
function toPkgName(specifier) {
|
|
62
68
|
const segments = specifier.split("/");
|
|
63
|
-
return specifier.startsWith("@")
|
|
69
|
+
return specifier.startsWith("@")
|
|
70
|
+
? segments.slice(0, 2).join("/")
|
|
71
|
+
: segments[0];
|
|
64
72
|
}
|
|
65
73
|
|
|
66
|
-
// map a package AND its transitive deps — a package's own ESM files import their deps as bare
|
|
67
|
-
// specifiers too (d3-scale imports "d3-array"), and the browser resolves those via the import map
|
|
68
74
|
function addPackage(rootDir, pkgName, imports) {
|
|
69
75
|
if (imports[pkgName]) return;
|
|
70
76
|
const entry = resolvePackage(rootDir, pkgName);
|
|
71
77
|
if (!entry) {
|
|
72
|
-
console.warn(
|
|
78
|
+
console.warn(
|
|
79
|
+
colors(
|
|
80
|
+
"yellow",
|
|
81
|
+
"Can't resolve \"" +
|
|
82
|
+
pkgName +
|
|
83
|
+
'" — install it at the project root: npm i ' +
|
|
84
|
+
pkgName,
|
|
85
|
+
),
|
|
86
|
+
);
|
|
73
87
|
return;
|
|
74
88
|
}
|
|
75
89
|
imports[pkgName] = entry;
|
|
76
|
-
imports[pkgName + "/"] = "/node_modules/" + pkgName + "/";
|
|
77
|
-
const pkgJsonPath = path.join(
|
|
90
|
+
imports[pkgName + "/"] = "/node_modules/" + pkgName + "/";
|
|
91
|
+
const pkgJsonPath = path.join(
|
|
92
|
+
rootDir,
|
|
93
|
+
"node_modules",
|
|
94
|
+
pkgName,
|
|
95
|
+
"package.json",
|
|
96
|
+
);
|
|
78
97
|
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
79
|
-
Object.keys(pkg.dependencies || {}).forEach((dep) =>
|
|
98
|
+
Object.keys(pkg.dependencies || {}).forEach((dep) =>
|
|
99
|
+
addPackage(rootDir, dep, imports),
|
|
100
|
+
);
|
|
80
101
|
}
|
|
81
102
|
|
|
82
|
-
/**
|
|
83
|
-
* Build the <script type="importmap"> tag injected into index.html so bare imports
|
|
84
|
-
* (e.g. import { marked } from "marked") resolve in the browser without a bundler.
|
|
85
|
-
* App deps install at the project root (rootDir = srcDir/..); framework libs map to /core.
|
|
86
|
-
* @param {*} srcDir absolute path of the project's src directory
|
|
87
|
-
*/
|
|
88
103
|
function importMap(srcDir) {
|
|
89
104
|
const rootDir = path.resolve(srcDir, "..");
|
|
90
105
|
const imports = {};
|
|
91
106
|
|
|
92
|
-
// handle internal libs
|
|
93
107
|
if (useLocalCore) {
|
|
94
108
|
Object.assign(imports, localLibs);
|
|
95
109
|
} else {
|
|
96
|
-
// npm mode: the framework resolves like any other root dependency
|
|
97
110
|
Object.keys(localLibs).forEach((pkgName) => {
|
|
98
111
|
const entry = resolvePackage(rootDir, pkgName);
|
|
99
112
|
if (!entry) {
|
|
100
|
-
console.warn(
|
|
113
|
+
console.warn(
|
|
114
|
+
colors(
|
|
115
|
+
"yellow",
|
|
116
|
+
"Can't resolve \"" +
|
|
117
|
+
pkgName +
|
|
118
|
+
'" — install it at the project root: npm i ' +
|
|
119
|
+
pkgName,
|
|
120
|
+
),
|
|
121
|
+
);
|
|
101
122
|
return;
|
|
102
123
|
}
|
|
103
124
|
imports[pkgName] = entry;
|
|
@@ -110,7 +131,11 @@ function importMap(srcDir) {
|
|
|
110
131
|
if (pkgName in localLibs) return;
|
|
111
132
|
addPackage(rootDir, pkgName, imports);
|
|
112
133
|
});
|
|
113
|
-
return
|
|
134
|
+
return (
|
|
135
|
+
'<script type="importmap">' +
|
|
136
|
+
JSON.stringify({ imports }, null, 2) +
|
|
137
|
+
"</script>"
|
|
138
|
+
);
|
|
114
139
|
}
|
|
115
140
|
|
|
116
141
|
module.exports = importMap;
|
package/lib/jsParser.js
CHANGED
|
@@ -6,27 +6,22 @@ function jsParser(str) {
|
|
|
6
6
|
str = clean(str);
|
|
7
7
|
const arr = str.match(script);
|
|
8
8
|
let content = Array.isArray(arr) && arr.length ? arr[0] : "";
|
|
9
|
-
|
|
10
|
-
// fill empty logic to make component works if there is no script tag
|
|
11
|
-
// if (content === "") return `__isInitial__: true,\n__render__(obj, prop) { olum.lifeCycle.render(obj, prop); },\n};`;
|
|
12
|
-
|
|
13
|
-
// handle script tag attributes
|
|
9
|
+
|
|
14
10
|
const attrsObj = {};
|
|
15
|
-
if(content !== "") {
|
|
11
|
+
if (content !== "") {
|
|
16
12
|
const dom = new jsdom.JSDOM(content).window.document;
|
|
17
13
|
const scriptElm = dom.querySelector("script");
|
|
18
14
|
const attrs = scriptElm.getAttributeNames();
|
|
19
|
-
if (attrs.length)
|
|
20
|
-
|
|
15
|
+
if (attrs.length)
|
|
16
|
+
attrs.forEach((key) => (attrsObj[key] = scriptElm.getAttribute(key)));
|
|
21
17
|
}
|
|
22
18
|
|
|
23
19
|
content = content.replace(/(<script.*>)/, "");
|
|
24
20
|
content = content.replace(/(<\/script>)$/, "");
|
|
25
|
-
content = content.replace(/onMount\(/, "var mounted = onMount(");
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return {js: content, jsAttrs: attrsObj};
|
|
21
|
+
content = content.replace(/onMount\(/, "var mounted = onMount(");
|
|
22
|
+
|
|
23
|
+
content = content.replace(/const state/g, "var state");
|
|
24
|
+
return { js: content, jsAttrs: attrsObj };
|
|
30
25
|
}
|
|
31
26
|
|
|
32
27
|
module.exports = jsParser;
|
package/lib/lineCounter.js
CHANGED
|
@@ -2,24 +2,37 @@ const colors = require("./colors");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const path = require("path");
|
|
4
4
|
|
|
5
|
-
const lineCounter = str => str.split("\n").map(line => line);
|
|
5
|
+
const lineCounter = (str) => str.split("\n").map((line) => line);
|
|
6
6
|
|
|
7
7
|
function getLineInfo(counter, item, filePath, msg) {
|
|
8
8
|
const portion = item.outerHTML.split("\n")[0].trim();
|
|
9
9
|
counter.forEach((line, index) => {
|
|
10
10
|
index = index + 1;
|
|
11
11
|
const origin = line;
|
|
12
|
-
line = line.replace(/(\s+)?\/\>/g, ">").trim();
|
|
12
|
+
line = line.replace(/(\s+)?\/\>/g, ">").trim();
|
|
13
13
|
if (line === portion) {
|
|
14
14
|
const log =
|
|
15
15
|
colors("yellow.bold", "\n<<Olum Warning>>\n") +
|
|
16
|
-
colors(
|
|
16
|
+
colors(
|
|
17
|
+
"yellow",
|
|
18
|
+
" File: " + filePath + "\n Line: " + index + "\n " + msg + "\n",
|
|
19
|
+
) +
|
|
17
20
|
colors("red", " " + origin.trim()) +
|
|
18
21
|
"\n";
|
|
19
22
|
console.warn(log);
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
const _log =
|
|
25
|
+
"\n<<Olum Warning>>\n" +
|
|
26
|
+
" File: " +
|
|
27
|
+
filePath +
|
|
28
|
+
"\n Line: " +
|
|
29
|
+
index +
|
|
30
|
+
"\n " +
|
|
31
|
+
msg +
|
|
32
|
+
"\n" +
|
|
33
|
+
" " +
|
|
34
|
+
origin.trim() +
|
|
35
|
+
"\n";
|
|
23
36
|
const logFilePath = path.resolve(__dirname, "./log.txt");
|
|
24
37
|
if (!fs.existsSync(logFilePath)) fs.writeFileSync(logFilePath, "");
|
|
25
38
|
fs.appendFileSync(logFilePath, _log);
|