olum-compiler 0.9.2 → 0.9.7
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/copySrc.js +1 -3
- package/lib/events.js +6 -1
- package/lib/helpers.js +0 -51
- package/lib/htmlParser.js +8 -1
- package/lib/parser.js +8 -32
- package/lib/placeholder.js +2 -1
- package/package.json +1 -1
package/bin/copySrc.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// todo check changed files only to be copied, use this project https://github.com/eissapk/diff
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const fs = require("fs");
|
|
4
|
-
const { copy, remove,
|
|
4
|
+
const { copy, remove, ls, generateRouteManifest, generateImports, generateRoutes } = require("../lib/helpers");
|
|
5
5
|
const entryPoint = path.resolve(process.cwd(), "src").replace(/\/module/, "");
|
|
6
6
|
const entryPoint2 = path.resolve(process.cwd(), "public").replace(/\/module/, "");
|
|
7
7
|
|
|
@@ -50,8 +50,6 @@ module.exports = function copySrc() {
|
|
|
50
50
|
copy(entryPoint2, path.resolve(__dirname, "../public"), () => {
|
|
51
51
|
const indexHtmlPath = path.resolve(__dirname, "../public/index.html");
|
|
52
52
|
let indexHtmlContent = fs.readFileSync(indexHtmlPath).toString();
|
|
53
|
-
// auto bind router params handler
|
|
54
|
-
indexHtmlContent = indexHtmlContent.replace(/<body>/, `\n<body>\n${routerParamsParser}`);
|
|
55
53
|
// auto bind div#app placeholder and mainjs script to index.html
|
|
56
54
|
indexHtmlContent = indexHtmlContent.replace(/<\/body>/, `\n<div id="app"></div>\n<script defer type="module" src="../src/main.js"></script>\n</body>`);
|
|
57
55
|
fs.writeFileSync(indexHtmlPath, indexHtmlContent);
|
package/lib/events.js
CHANGED
|
@@ -31,7 +31,12 @@ function handleEvents(body, counter, filePath, anonHandlers) {
|
|
|
31
31
|
let methods = attrValue.split("),");
|
|
32
32
|
methods = methods.map(str => (str.replace(/\)$/, "") + ")").trim());
|
|
33
33
|
let methodsNames = methods.map(str => (str.trim().split("(")[0]).trim());
|
|
34
|
-
|
|
34
|
+
// args = everything between the FIRST "(" and the LAST ")", so arguments that themselves
|
|
35
|
+
// contain parens (e.g. toggle(props().todo.id)) aren't truncated at the inner "(".
|
|
36
|
+
let methodsArgs = methods.map(str => {
|
|
37
|
+
const s = str.trim();
|
|
38
|
+
return s.slice(s.indexOf("(") + 1, s.lastIndexOf(")")).trim();
|
|
39
|
+
});
|
|
35
40
|
|
|
36
41
|
function handle$event(str) {
|
|
37
42
|
return str.replace(/\$event/g, "'$event'")
|
package/lib/helpers.js
CHANGED
|
@@ -663,56 +663,6 @@ ${routes
|
|
|
663
663
|
];`;
|
|
664
664
|
}
|
|
665
665
|
|
|
666
|
-
const routerParamsParser = `<script>
|
|
667
|
-
/**
|
|
668
|
-
* Extract route params from a file path and pathname.
|
|
669
|
-
*
|
|
670
|
-
* @param {string} filePath
|
|
671
|
-
* @param {string} pathname
|
|
672
|
-
* @returns {Record<string, string | string[]>}
|
|
673
|
-
*
|
|
674
|
-
* Examples:
|
|
675
|
-
* extractParams("/blog/[slug]/page.html", "/blog/hello")
|
|
676
|
-
* -> { slug: "hello" }
|
|
677
|
-
*
|
|
678
|
-
* extractParams("/users/[id]/posts/[postId]/page.html", "/users/5/posts/10")
|
|
679
|
-
* -> { id: "5", postId: "10" }
|
|
680
|
-
*/
|
|
681
|
-
function extractParams(filePath, pathname) {
|
|
682
|
-
const routeParts = filePath
|
|
683
|
-
.replace(/\\/page\\.[^/]+$/, "")
|
|
684
|
-
.split("/")
|
|
685
|
-
.filter(Boolean);
|
|
686
|
-
|
|
687
|
-
const pathParts = pathname.split("/").filter(Boolean);
|
|
688
|
-
|
|
689
|
-
const params = {};
|
|
690
|
-
|
|
691
|
-
let i = routeParts.length - 1;
|
|
692
|
-
let j = pathParts.length - 1;
|
|
693
|
-
|
|
694
|
-
while (i >= 0 && j >= 0) {
|
|
695
|
-
const part = routeParts[i];
|
|
696
|
-
|
|
697
|
-
// [...slug]
|
|
698
|
-
if (part.startsWith("[...") && part.endsWith("]")) {
|
|
699
|
-
params[part.slice(4, -1)] = pathParts.slice(0, j + 1);
|
|
700
|
-
break;
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
// [slug]
|
|
704
|
-
if (part.startsWith("[") && part.endsWith("]")) {
|
|
705
|
-
params[part.slice(1, -1)] = decodeURIComponent(pathParts[j]);
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
i--;
|
|
709
|
-
j--;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
return params;
|
|
713
|
-
}
|
|
714
|
-
</script>`;
|
|
715
|
-
|
|
716
666
|
const libsMap = {
|
|
717
667
|
development: {
|
|
718
668
|
olum: "/node_modules/olum/dist/olum.js",
|
|
@@ -749,5 +699,4 @@ module.exports.rewriteImports = rewriteImports;
|
|
|
749
699
|
module.exports.generateRouteManifest = generateRouteManifest;
|
|
750
700
|
module.exports.generateImports = generateImports;
|
|
751
701
|
module.exports.generateRoutes = generateRoutes;
|
|
752
|
-
module.exports.routerParamsParser = routerParamsParser;
|
|
753
702
|
module.exports.libsMap = libsMap;
|
package/lib/htmlParser.js
CHANGED
|
@@ -48,7 +48,14 @@ function htmlparser(str, counter, filePath) {
|
|
|
48
48
|
// appears as a literal `{` here. Wrapping each remaining `{expr}` in `olum.esc(...)` therefore
|
|
49
49
|
// escapes user-facing output (preventing XSS and "special characters break the page" bugs) while
|
|
50
50
|
// leaving structural code untouched. `[^{}]+` keeps each match to a single, non-nested pair.
|
|
51
|
-
|
|
51
|
+
// slot content (`children` and `props().children`) is trusted markup authored in the parent
|
|
52
|
+
// template, not user input, so it is emitted RAW (no esc) — escaping it would show its tags as text.
|
|
53
|
+
content = content.replace(/\{([^{}]+)\}/g, (match, expr) => {
|
|
54
|
+
const raw = expr.trim();
|
|
55
|
+
if (raw === "children") return "${children}";
|
|
56
|
+
if (/^props\(\s*\)\.children$/.test(raw)) return "${" + raw + "}";
|
|
57
|
+
return "${olum.esc(" + expr + ")}";
|
|
58
|
+
});
|
|
52
59
|
|
|
53
60
|
content = content.replace(/&/g, "&"); // fix entity
|
|
54
61
|
content = content.replace(/=>/g, "=>"); // preserve arrow functions
|
package/lib/parser.js
CHANGED
|
@@ -5,7 +5,7 @@ const jsParser = require("./jsParser");
|
|
|
5
5
|
const getImports = require("./getImports");
|
|
6
6
|
const toJs = require("./toJs");
|
|
7
7
|
const path = require("path");
|
|
8
|
-
const { isFullArr, id, createScopeHandler, getGlobalMethods, getGlobalProps,
|
|
8
|
+
const { isFullArr, id, createScopeHandler, getGlobalMethods, getGlobalProps, libsMap } = require("./helpers");
|
|
9
9
|
const createSourceMap = require("./sourcemap");
|
|
10
10
|
|
|
11
11
|
const parser = (str, counter, filePath) => {
|
|
@@ -105,37 +105,14 @@ const parser = (str, counter, filePath) => {
|
|
|
105
105
|
return entries.join(",");
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return entry && entry.incomingProps ? entry.incomingProps[key] : undefined;
|
|
113
|
-
},
|
|
114
|
-
set(_, key, val) {
|
|
115
|
-
const entry = olum.app.store[_storeKey];
|
|
116
|
-
if (entry && entry.parentCompName && entry.incomingPropSources) {
|
|
117
|
-
const desc = entry.incomingPropSources[key];
|
|
118
|
-
if (desc) {
|
|
119
|
-
const parent = olum.app.store[entry.parentCompName];
|
|
120
|
-
if (parent) {
|
|
121
|
-
// state source -> write parent state (triggers re-render); props source -> chain up the parent's props proxy
|
|
122
|
-
if (desc.kind === "state" && parent.stateProps) parent.stateProps[desc.key] = val;
|
|
123
|
-
else if (desc.kind === "props" && parent.incomingPropsProxy) parent.incomingPropsProxy[desc.key] = val;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
});`;
|
|
130
|
-
|
|
131
|
-
// children is declared inside getElm (not at factory-call time) so it re-reads the store on every render
|
|
132
|
-
const childrenVar = `const children=(olum.app.store[_storeKey]&&olum.app.store[_storeKey].children)||"";`;
|
|
133
|
-
const routerParamsProxy = `const params = new Proxy({},{ get(_, key) { const values = extractParams(currentFilePath, location.pathname); return values[key];}, ownKeys() { return Reflect.ownKeys(extractParams(currentFilePath, location.pathname));},getOwnPropertyDescriptor() {return {enumerable: true,configurable: true,};},});`;
|
|
108
|
+
js = js.replace(/params\(/, `params("${filePath}",location.pathname`); // handle params of router
|
|
109
|
+
// bind the imported props() accessor to this instance's runtime store key: props() -> props(_storeKey)
|
|
110
|
+
js = js.replace(/props\(\s*\)/g, "props(_storeKey)");
|
|
111
|
+
html = html.replace(/props\(\s*\)/g, "props(_storeKey)");
|
|
134
112
|
|
|
135
|
-
|
|
136
|
-
return `${breaks}${imports ? imports.join("\n") + "\n" : ""}export default (_instanceKey) => {const currentFilePath = "${filePath}"; ${routerParamsProxy} const _storeKey=_instanceKey||"${compName}"; ${minifyJS(propsProxy)}${js}
|
|
113
|
+
return `${breaks}${imports ? imports.join("\n") + "\n" : ""}export default (_instanceKey) => {const _storeKey=_instanceKey||"${compName}"; ${js}
|
|
137
114
|
var el_${currentID} = olum.mkElm("div", "${compName}", "${currentID}");
|
|
138
|
-
var
|
|
115
|
+
var host = el_${currentID};
|
|
139
116
|
var methods_${currentID} = {${globalMethods}};
|
|
140
117
|
var olum_${currentID} = {
|
|
141
118
|
${css}
|
|
@@ -144,7 +121,6 @@ const parser = (str, counter, filePath) => {
|
|
|
144
121
|
compName: "${compName}",
|
|
145
122
|
components: {${mkComponentsList(comps)}},
|
|
146
123
|
get getElm() {
|
|
147
|
-
${childrenVar}
|
|
148
124
|
el_${currentID}.innerHTML = \`
|
|
149
125
|
${html}\`;
|
|
150
126
|
olum.injectStyle("${compName}", olum_${currentID}.__style__());
|
|
@@ -163,7 +139,7 @@ const parser = (str, counter, filePath) => {
|
|
|
163
139
|
props: olum_${currentID}.props,
|
|
164
140
|
__OLUM__: olum_${currentID},
|
|
165
141
|
el: el_${currentID},
|
|
166
|
-
incomingPropsProxy: props,
|
|
142
|
+
incomingPropsProxy: olum.props(_storeKey),
|
|
167
143
|
${isDev ? `stateProps: ${hasState ? "state" : "null"},` : ""}
|
|
168
144
|
hooks: {
|
|
169
145
|
mounted: typeof mounted !== "undefined" ? mounted : null,
|
package/lib/placeholder.js
CHANGED
|
@@ -55,7 +55,8 @@ function parseProps(attrsStr) {
|
|
|
55
55
|
// parent's own prop (the setter recurses until it reaches whoever holds it in state).
|
|
56
56
|
// format: propName:kind:srcKey (kind = state | props)
|
|
57
57
|
const stateMatch = expr.trim().match(/^state\.(\w+)$/);
|
|
58
|
-
|
|
58
|
+
// props source is the wrapper call `props().X` (old proxy access `props.X` also accepted)
|
|
59
|
+
const propsMatch = expr.trim().match(/^props(?:\(\s*\))?\.(\w+)$/);
|
|
59
60
|
if (stateMatch) srcEntries.push(propName + ':state:' + stateMatch[1]);
|
|
60
61
|
else if (propsMatch) srcEntries.push(propName + ':props:' + propsMatch[1]);
|
|
61
62
|
} else {
|