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/for.js
CHANGED
|
@@ -1,68 +1,96 @@
|
|
|
1
1
|
const { getLineInfo } = require("./lineCounter");
|
|
2
2
|
const { forOf, forIn } = require("./regex");
|
|
3
|
-
const { isNum,id } = require("./helpers");
|
|
3
|
+
const { isNum, id } = require("./helpers");
|
|
4
4
|
const { RAW_OPEN, RAW_CLOSE, shieldOps } = require("./tokens");
|
|
5
5
|
|
|
6
6
|
function handleForStatement(body, counter, filePath) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const forElms = new Array().slice
|
|
8
|
+
.call(body.querySelectorAll("for"))
|
|
9
|
+
.reverse();
|
|
10
|
+
forElms.forEach((item) => {
|
|
11
11
|
if (item.hasAttribute("each")) {
|
|
12
|
-
// `each` is a plain JS expression (no braces): <for each="item of state.items">
|
|
13
|
-
// shieldOps: protect <,> from the jsdom text round-trip (restored in htmlParser)
|
|
14
12
|
const condition = shieldOps(item.getAttribute("each").trim());
|
|
15
13
|
|
|
16
|
-
// #3 (keyed reconciliation): if the <for> declares key={expr}, stamp every component
|
|
17
|
-
// placeholder in this loop with data-o-key="{expr}". `expr` references the loop variable, so
|
|
18
|
-
// each iteration emits a distinct key, and the runtime (buildTree) reuses the matching
|
|
19
|
-
// instance by identity instead of by position. <for>s are processed deepest-first, so a
|
|
20
|
-
// nested loop has already stamped its own placeholders — we skip any that are already keyed.
|
|
21
14
|
const keyAttr = item.getAttribute("key");
|
|
22
15
|
if (keyAttr) {
|
|
23
16
|
const keyExpr = keyAttr.trim();
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
|
|
18
|
+
new Array().slice
|
|
19
|
+
.call(item.querySelectorAll("olum"))
|
|
20
|
+
.forEach((olum) => {
|
|
21
|
+
if (!olum.hasAttribute("data-o-key"))
|
|
22
|
+
olum.setAttribute("data-o-key", "{" + keyExpr + "}");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const skipTags = ["olum", "show", "if", "for", "else", "else-if"];
|
|
26
|
+
new Array().slice.call(item.children).forEach((child) => {
|
|
27
|
+
if (skipTags.includes(child.tagName.toLowerCase())) return;
|
|
28
|
+
if (!child.hasAttribute("key"))
|
|
29
|
+
child.setAttribute("key", "{" + keyExpr + "}");
|
|
26
30
|
});
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
const innerContent = item.innerHTML;
|
|
30
34
|
|
|
31
|
-
// for of loop
|
|
32
35
|
condition.replace(forOf, (str, $1, $2, $3) => {
|
|
33
36
|
if (isNum($3)) {
|
|
34
37
|
const params = $1.replace(/\(|\)/g, "");
|
|
35
38
|
const [param1] = params.split(",");
|
|
36
39
|
const mkID = id();
|
|
37
|
-
let variable = param1 + "=index"+mkID+"+1";
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
let variable = param1 + "=index" + mkID + "+1";
|
|
41
|
+
|
|
42
|
+
const loop =
|
|
43
|
+
RAW_OPEN +
|
|
44
|
+
"new Array(" +
|
|
45
|
+
$3 +
|
|
46
|
+
").fill().map(function(" +
|
|
47
|
+
param1 +
|
|
48
|
+
", index" +
|
|
49
|
+
mkID +
|
|
50
|
+
")<-olum->\n" +
|
|
51
|
+
variable +
|
|
52
|
+
"\nreturn`" +
|
|
53
|
+
innerContent +
|
|
54
|
+
"`;\n}).join('')" +
|
|
55
|
+
RAW_CLOSE;
|
|
41
56
|
item.insertAdjacentHTML("beforebegin", loop);
|
|
42
57
|
item.remove();
|
|
43
58
|
} else {
|
|
44
|
-
// console.log({ condition, innerContent });
|
|
45
|
-
// console.log("for of loop", { str, $1, $2, $3 });
|
|
46
59
|
const params = $1.replace(/\(|\)/g, "");
|
|
47
|
-
const loop =
|
|
60
|
+
const loop =
|
|
61
|
+
RAW_OPEN +
|
|
62
|
+
$3 +
|
|
63
|
+
".map(function(" +
|
|
64
|
+
params +
|
|
65
|
+
")<-olum->\nreturn`" +
|
|
66
|
+
innerContent +
|
|
67
|
+
"`;\n}).join('')" +
|
|
68
|
+
RAW_CLOSE;
|
|
48
69
|
item.insertAdjacentHTML("beforebegin", loop);
|
|
49
70
|
item.remove();
|
|
50
71
|
}
|
|
51
72
|
});
|
|
52
73
|
|
|
53
|
-
// for in loop
|
|
54
74
|
condition.replace(forIn, (str, $1, $2, $3) => {
|
|
55
|
-
// console.log({ condition, innerContent });
|
|
56
|
-
// console.log("for in loop", { str, $1, $2, $3 });
|
|
57
75
|
const params = $1.replace(/\(|\)/g, "");
|
|
58
76
|
const [param1, param2, param3] = params.split(",");
|
|
59
|
-
|
|
77
|
+
|
|
60
78
|
const value = param3 ? param3 + "=" + $3 + "[" + param1 + "];" : "";
|
|
61
|
-
const loop =
|
|
79
|
+
const loop =
|
|
80
|
+
RAW_OPEN +
|
|
81
|
+
"Object.keys(" +
|
|
82
|
+
$3 +
|
|
83
|
+
").map(function(" +
|
|
84
|
+
params +
|
|
85
|
+
")<-olum->\n" +
|
|
86
|
+
value +
|
|
87
|
+
"\nreturn`" +
|
|
88
|
+
innerContent +
|
|
89
|
+
"`;\n}).join('')" +
|
|
90
|
+
RAW_CLOSE;
|
|
62
91
|
item.insertAdjacentHTML("beforebegin", loop);
|
|
63
92
|
item.remove();
|
|
64
93
|
});
|
|
65
|
-
|
|
66
94
|
} else {
|
|
67
95
|
const msg = "Missing 'each' attribute:";
|
|
68
96
|
getLineInfo(counter, item, filePath, msg);
|
package/lib/getImports.js
CHANGED
|
@@ -1,43 +1,61 @@
|
|
|
1
1
|
const acorn = require("acorn");
|
|
2
2
|
const { isFullArr } = require("./helpers");
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
function getImports(str) {
|
|
5
5
|
let finalImports = [];
|
|
6
|
-
let arr = acorn.parse(str, {
|
|
6
|
+
let arr = acorn.parse(str, {
|
|
7
|
+
ecmaVersion: "latest",
|
|
8
|
+
sourceType: "module",
|
|
9
|
+
}).body;
|
|
7
10
|
arr.forEach((obj) => {
|
|
8
11
|
if (obj.type === "ImportDeclaration") {
|
|
9
12
|
if (obj.specifiers) {
|
|
10
13
|
if (isFullArr(obj.specifiers)) {
|
|
11
14
|
if (obj.specifiers.length === 1) {
|
|
12
15
|
if (obj.specifiers[0].type == "ImportDefaultSpecifier") {
|
|
13
|
-
// default
|
|
14
16
|
const name = obj.specifiers[0].local.name;
|
|
15
17
|
const statement = "import " + name + " from " + obj.source.raw;
|
|
16
|
-
finalImports.push({
|
|
17
|
-
|
|
18
|
+
finalImports.push({
|
|
19
|
+
statement,
|
|
20
|
+
regex: `(from)(\\s+)?(${obj.source.raw}).*`,
|
|
21
|
+
});
|
|
18
22
|
} else {
|
|
19
|
-
// non-default
|
|
20
23
|
const name = obj.specifiers[0].local.name;
|
|
21
|
-
const statement =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
const statement =
|
|
25
|
+
"import { " + name + " } from " + obj.source.raw;
|
|
26
|
+
finalImports.push({
|
|
27
|
+
statement,
|
|
28
|
+
regex: `(from)(\\s+)?(${obj.source.raw}).*`,
|
|
29
|
+
});
|
|
24
30
|
}
|
|
25
31
|
} else {
|
|
26
32
|
let defaultItem = null;
|
|
27
33
|
let nonDefaultArr = [];
|
|
28
34
|
obj.specifiers.forEach((item) => {
|
|
29
|
-
if (item.type === "ImportDefaultSpecifier")
|
|
30
|
-
|
|
35
|
+
if (item.type === "ImportDefaultSpecifier")
|
|
36
|
+
defaultItem = item.local.name;
|
|
37
|
+
if (item.type === "ImportSpecifier")
|
|
38
|
+
nonDefaultArr.push(item.local.name);
|
|
39
|
+
});
|
|
40
|
+
const statement =
|
|
41
|
+
"import " +
|
|
42
|
+
(defaultItem ? defaultItem + ", " : "") +
|
|
43
|
+
"{ " +
|
|
44
|
+
nonDefaultArr.join(", ") +
|
|
45
|
+
" } from " +
|
|
46
|
+
obj.source.raw;
|
|
47
|
+
finalImports.push({
|
|
48
|
+
statement,
|
|
49
|
+
regex: `(from)(\\s+)?(${obj.source.raw}).*`,
|
|
31
50
|
});
|
|
32
|
-
const statement = "import " + (defaultItem ? defaultItem + ", " : "") + "{ " + nonDefaultArr.join(", ") + " } from " + obj.source.raw;
|
|
33
|
-
finalImports.push({ statement, regex: `(from)(\\s+)?(${obj.source.raw}).*` });
|
|
34
|
-
// console.log("mix: ", statement);
|
|
35
51
|
}
|
|
36
52
|
} else {
|
|
37
|
-
// import only
|
|
38
53
|
const statement = "import " + obj.source.raw;
|
|
39
|
-
finalImports.push({
|
|
40
|
-
|
|
54
|
+
finalImports.push({
|
|
55
|
+
statement,
|
|
56
|
+
type: "noFrom",
|
|
57
|
+
regex: `(import)(\\s+)?(${obj.source.raw}).*\\n?`,
|
|
58
|
+
});
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
61
|
}
|
package/lib/getTempPath.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
2
|
const { ls, isFile, isHTML } = require("./helpers");
|
|
3
|
-
const isComp = str => !/^_/.test(str);
|
|
3
|
+
const isComp = (str) => !/^_/.test(str);
|
|
4
4
|
|
|
5
5
|
function getTempPath() {
|
|
6
6
|
const entryPoint = path.resolve(__dirname, "../src");
|
|
7
7
|
let paths = ls(entryPoint);
|
|
8
|
-
|
|
9
|
-
paths = paths
|
|
8
|
+
|
|
9
|
+
paths = paths
|
|
10
|
+
.map((item) =>
|
|
11
|
+
isFile(item) && isHTML(item) && isComp(path.basename(item)) ? item : null,
|
|
12
|
+
)
|
|
13
|
+
.filter((item) => item);
|
|
10
14
|
return paths;
|
|
11
15
|
}
|
|
12
16
|
|
|
13
|
-
module.exports = getTempPath;
|
|
17
|
+
module.exports = getTempPath;
|