olum-compiler 0.2.7 → 0.5.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.md +1 -1
- package/README.md +1 -1
- package/bin/build.js +9 -0
- package/bin/compiler.js +23 -0
- package/bin/copySrc.js +22 -0
- package/bin/dev.js +47 -0
- package/bin/index.js +1 -24
- package/bin/mimes.json +340 -0
- package/bin/server.js +77 -0
- package/bin/watcher.js +6 -0
- package/bin/ws.js +54 -0
- package/core/app.js +530 -0
- package/lib/attr.js +39 -0
- package/lib/clean.js +4 -0
- package/lib/colors.js +72 -0
- package/lib/compPlaceholder.js +10 -0
- package/lib/compiler.js +21 -0
- package/lib/cssParser.js +87 -0
- package/lib/domEvents.js +112 -0
- package/lib/events.js +48 -0
- package/lib/for.js +71 -0
- package/lib/getImports.js +48 -0
- package/lib/getTempPath.js +13 -0
- package/lib/helpers.js +509 -0
- package/lib/htmlParser.js +71 -0
- package/lib/if.js +65 -0
- package/lib/jsParser.js +31 -0
- package/lib/lineCounter.js +31 -0
- package/lib/log.txt +0 -0
- package/lib/mode.js +12 -0
- package/lib/models.js +51 -0
- package/lib/parser.js +170 -0
- package/lib/placeholder.js +76 -0
- package/lib/regex.js +12 -0
- package/lib/show.js +22 -0
- package/lib/sourcemap.js +34 -0
- package/lib/styles.js +32 -0
- package/lib/toJs.js +12 -0
- package/lib/tokens.js +14 -0
- package/package.json +7 -39
- package/babel.config.json +0 -4
- package/compiler.js +0 -486
- package/tasks/build.js +0 -24
- package/tasks/bundle.js +0 -33
- package/tasks/catchall.js +0 -39
- package/tasks/clean.js +0 -18
- package/tasks/compile.js +0 -32
- package/tasks/copy-build.js +0 -33
- package/tasks/desktop.js +0 -63
- package/tasks/dev.js +0 -18
- package/tasks/index.js +0 -12
- package/tasks/linux.js +0 -255
- package/tasks/logger.js +0 -19
- package/tasks/mac.js +0 -8
- package/tasks/reload.js +0 -7
- package/tasks/server.js +0 -26
- package/tasks/watch.js +0 -44
- package/tasks/windows.js +0 -8
- package/webpack.config.js +0 -152
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const colors = require("./colors");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const lineCounter = str => str.split("\n").map(line => line);
|
|
6
|
+
|
|
7
|
+
function getLineInfo(counter, item, filePath, msg) {
|
|
8
|
+
const portion = item.outerHTML.split("\n")[0].trim();
|
|
9
|
+
counter.forEach((line, index) => {
|
|
10
|
+
index = index + 1;
|
|
11
|
+
const origin = line;
|
|
12
|
+
line = line.replace(/(\s+)?\/\>/g, ">").trim(); // fix jsdom bug which removes self closing tag
|
|
13
|
+
if (line === portion) {
|
|
14
|
+
const log =
|
|
15
|
+
colors("yellow.bold", "\n<<Olum Warning>>\n") +
|
|
16
|
+
colors("yellow", " File: " + filePath + "\n Line: " + index + "\n " + msg + "\n") +
|
|
17
|
+
colors("red", " " + origin.trim()) +
|
|
18
|
+
"\n";
|
|
19
|
+
console.warn(log);
|
|
20
|
+
|
|
21
|
+
// append logs to log.txt
|
|
22
|
+
const _log = "\n<<Olum Warning>>\n" + " File: " + filePath + "\n Line: " + index + "\n " + msg + "\n" + " " + origin.trim() + "\n";
|
|
23
|
+
const logFilePath = path.resolve(__dirname, "./log.txt");
|
|
24
|
+
if (!fs.existsSync(logFilePath)) fs.writeFileSync(logFilePath, "");
|
|
25
|
+
fs.appendFileSync(logFilePath, _log);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports.lineCounter = lineCounter;
|
|
31
|
+
module.exports.getLineInfo = getLineInfo;
|
package/lib/log.txt
ADDED
|
File without changes
|
package/lib/mode.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
module.exports = function (files, value) {
|
|
3
|
+
if (Array.isArray(files) && files.length) {
|
|
4
|
+
files.forEach((filePath) => {
|
|
5
|
+
if (fs.existsSync(filePath)) {
|
|
6
|
+
let jsStr = fs.readFileSync(filePath).toString();
|
|
7
|
+
jsStr = jsStr.replace(/"{{mode}}"/, JSON.stringify(value));
|
|
8
|
+
fs.writeFileSync(filePath, jsStr);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
};
|
package/lib/models.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const { getLineInfo } = require("./lineCounter");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
function handleModels(body, counter, filePath) {
|
|
5
|
+
const elms = new Array().slice.call(body.querySelectorAll("[model]"));
|
|
6
|
+
const name = path.basename(filePath).replace(/\.html/i, "");
|
|
7
|
+
elms.forEach(item => {
|
|
8
|
+
const value = item.getAttribute("model").trim();
|
|
9
|
+
if (value) {
|
|
10
|
+
const data = value.split(".");
|
|
11
|
+
const index = hasMode(data);
|
|
12
|
+
if (index) {
|
|
13
|
+
const prop = data.slice(0, index).join(".");
|
|
14
|
+
const mode = data.slice(index);
|
|
15
|
+
item.setAttribute("data-o-e", "true");
|
|
16
|
+
item.setAttribute("data-o-model", prop + "," + name);
|
|
17
|
+
item.removeAttribute("model");
|
|
18
|
+
if (mode.length) item.setAttribute("data-o-model-mode", mode.join(","));
|
|
19
|
+
// console.log({ index, data, prop, mode });
|
|
20
|
+
// console.log(item.outerHTML + "\n----------");
|
|
21
|
+
} else if (index === false) {
|
|
22
|
+
const prop = data.join(".");
|
|
23
|
+
item.setAttribute("data-o-e", "true");
|
|
24
|
+
item.setAttribute("data-o-model", prop + "," + name);
|
|
25
|
+
item.removeAttribute("model");
|
|
26
|
+
// console.log({ index, data, prop });
|
|
27
|
+
// console.log(item.outerHTML + "\n----------");
|
|
28
|
+
} else if (index === 0) {
|
|
29
|
+
getLineInfo(counter, item, filePath, "Can't start model attribute with a modifier!");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hasMode(data) {
|
|
36
|
+
const max = 999;
|
|
37
|
+
let mode1 = data.indexOf("trim");
|
|
38
|
+
let mode2 = data.indexOf("num");
|
|
39
|
+
let mode3 = data.indexOf("bool");
|
|
40
|
+
mode1 = mode1 === -1 ? max : mode1;
|
|
41
|
+
mode2 = mode2 === -1 ? max : mode2;
|
|
42
|
+
mode3 = mode3 === -1 ? max : mode3;
|
|
43
|
+
const indexArr = [mode1, mode2, mode3];
|
|
44
|
+
const min = Math.min(...indexArr);
|
|
45
|
+
// console.log({ min, mode1, mode2, mode3 });
|
|
46
|
+
if (min === max) return false;
|
|
47
|
+
else if (min === 0) return 0;
|
|
48
|
+
return min;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = handleModels;
|
package/lib/parser.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const isDev = true; // todo: handle this later to be global var accessable every where in compiler files
|
|
2
|
+
const htmlParser = require("./htmlParser");
|
|
3
|
+
const cssParser = require("./cssParser");
|
|
4
|
+
const jsParser = require("./jsParser");
|
|
5
|
+
const getImports = require("./getImports");
|
|
6
|
+
const toJs = require("./toJs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const { isFullArr, id, createScopeHandler, getGlobalMethods, getGlobalProps, minifyJS } = require("./helpers");
|
|
9
|
+
const createSourceMap = require("./sourcemap");
|
|
10
|
+
|
|
11
|
+
const parser = (str, counter, filePath) => {
|
|
12
|
+
// start: calc lines above script tag (from line 1 to script tag)
|
|
13
|
+
let startIndex = str.split("\n").findIndex(item => /<script[\s\S]*?>/gi.test(item)) || 0;
|
|
14
|
+
let breaks = "";
|
|
15
|
+
for (let i = 0; i < startIndex; i++) breaks += "//\n";
|
|
16
|
+
// end
|
|
17
|
+
|
|
18
|
+
let { js, jsAttrs } = jsParser(str, counter, filePath); // js content and attributes in script opening tag
|
|
19
|
+
let {html, comps, anonHandlers} = htmlParser(str, counter, filePath); // html content and all components tags <olum></olum>
|
|
20
|
+
html = html.replace(/\"\-\[\[\-/g, "'").replace(/\-\]\]\-\"/g, "'"); // fix json in attribute issue
|
|
21
|
+
|
|
22
|
+
// Inject anonymous handler declarations so getGlobalMethods picks them up as regular methods
|
|
23
|
+
if (anonHandlers && anonHandlers.length) js = js + "\n" + anonHandlers.join("\n");
|
|
24
|
+
|
|
25
|
+
// clean import statements
|
|
26
|
+
let imports = getImports(js);
|
|
27
|
+
if (imports) {
|
|
28
|
+
const importRegex = /(import)(\s+)?(?!from)/g;
|
|
29
|
+
imports.forEach((item) => {
|
|
30
|
+
if (item.type && item.type == "noFrom") js = js.replace(new RegExp(item.regex, "gm"), "");
|
|
31
|
+
if (!item.type) js = js.replace(new RegExp(item.regex, "g"), "*/");
|
|
32
|
+
});
|
|
33
|
+
js = js.replace(importRegex, "/* olum:toBeRemoved");
|
|
34
|
+
// clean multiple line comments of imports and delete lines
|
|
35
|
+
js = js.replace(/(\/\* olum\:toBeRemoved)([\s\S]*?)(\*\/).*\n?/gm, "");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toCamelCase(name) {
|
|
39
|
+
return name;
|
|
40
|
+
return name.slice(0, 1).toLowerCase() + name.slice(1); // todo decide what to do with component name, leave it as it is or make it pascal case or camelcase of cababcase
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let compName = path.basename(filePath).replace(/.html/gi, "");
|
|
44
|
+
compName = toCamelCase(compName);
|
|
45
|
+
const currentID = id().slice(1);
|
|
46
|
+
const css = cssParser(str, currentID, compName);
|
|
47
|
+
|
|
48
|
+
let globalProps = [];
|
|
49
|
+
let globalMethods = [];
|
|
50
|
+
let stateProps = [];
|
|
51
|
+
let hasState = false;
|
|
52
|
+
let hasWatcher = false;
|
|
53
|
+
// let hasComputed = false;
|
|
54
|
+
let hasMounted = false;
|
|
55
|
+
let hasUnMounted = false;
|
|
56
|
+
|
|
57
|
+
const methodsTobeExcluded = ["mounted", "unMounted"];
|
|
58
|
+
const propsTobeExcluded = ["watcher"];
|
|
59
|
+
|
|
60
|
+
globalProps = getGlobalProps(js);
|
|
61
|
+
globalMethods = getGlobalMethods(js);
|
|
62
|
+
|
|
63
|
+
// check existance before runtime
|
|
64
|
+
// methods
|
|
65
|
+
hasMounted = globalMethods.includes("mounted") ? true : false;
|
|
66
|
+
hasUnMounted = globalMethods.includes("unMounted") ? true : false;
|
|
67
|
+
// props
|
|
68
|
+
hasState = globalProps.includes("state") ? true : false;
|
|
69
|
+
hasWatcher = globalProps.includes("watcher") ? true : false;
|
|
70
|
+
// hasComputed = globalProps.includes("computed") ? true : false;
|
|
71
|
+
|
|
72
|
+
// exclude props/methods
|
|
73
|
+
globalMethods = globalMethods.filter((name) => !methodsTobeExcluded.includes(name));
|
|
74
|
+
globalProps = globalProps.filter((name) => !propsTobeExcluded.includes(name));
|
|
75
|
+
|
|
76
|
+
const handleScope = createScopeHandler({ stateProps, hasState, propsTobeExcluded, jsAttrs });
|
|
77
|
+
const scopedProps = handleScope(globalProps, "props");
|
|
78
|
+
const scopedMethods = handleScope(globalMethods, "methods");
|
|
79
|
+
|
|
80
|
+
// convert .html to .js and add .js if it doesn't exist
|
|
81
|
+
if (imports) imports = imports.map((item) => toJs(item.statement));
|
|
82
|
+
|
|
83
|
+
function mkComponentsList(comps) {
|
|
84
|
+
if (!imports || !isFullArr(imports)) return "";
|
|
85
|
+
// emit a deduped name->factory map; word-boundary match so "Todos" doesn't also match "FilterTodos"
|
|
86
|
+
const seen = {};
|
|
87
|
+
const entries = [];
|
|
88
|
+
imports.forEach(importStatement => {
|
|
89
|
+
comps.forEach((name) => {
|
|
90
|
+
if (seen[name]) return;
|
|
91
|
+
if (new RegExp("\\b" + name + "\\b").test(importStatement)) {
|
|
92
|
+
seen[name] = true;
|
|
93
|
+
entries.push(toCamelCase(name) + ":" + name);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
return entries.join(",");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// resolves this instance's store slot via _storeKey (the runtime instance key)
|
|
101
|
+
const propsProxy = `const props = new Proxy({}, {
|
|
102
|
+
get(_, key) {
|
|
103
|
+
const entry = olum.app.store[_storeKey];
|
|
104
|
+
return entry && entry.incomingProps ? entry.incomingProps[key] : undefined;
|
|
105
|
+
},
|
|
106
|
+
set(_, key, val) {
|
|
107
|
+
const entry = olum.app.store[_storeKey];
|
|
108
|
+
if (entry && entry.parentCompName && entry.incomingPropSources) {
|
|
109
|
+
const desc = entry.incomingPropSources[key];
|
|
110
|
+
if (desc) {
|
|
111
|
+
const parent = olum.app.store[entry.parentCompName];
|
|
112
|
+
if (parent) {
|
|
113
|
+
// state source -> write parent state (triggers re-render); props source -> chain up the parent's props proxy
|
|
114
|
+
if (desc.kind === "state" && parent.stateProps) parent.stateProps[desc.key] = val;
|
|
115
|
+
else if (desc.kind === "props" && parent.incomingPropsProxy) parent.incomingPropsProxy[desc.key] = val;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
});`;
|
|
122
|
+
|
|
123
|
+
// children is declared inside getElm (not at factory-call time) so it re-reads the store on every render
|
|
124
|
+
const childrenVar = `const children=(olum.app.store[_storeKey]&&olum.app.store[_storeKey].children)||"";`;
|
|
125
|
+
|
|
126
|
+
// bind props proxy at the top to be accessed by ${js} code
|
|
127
|
+
return `${breaks}${imports ? imports.join("\n") + "\n" : ""}export default (_instanceKey) => {const _storeKey=_instanceKey||"${compName}";${minifyJS(propsProxy)}${js}
|
|
128
|
+
var el_${currentID} = olum.mkElm("div", "${compName}", "${currentID}");
|
|
129
|
+
var $0 = el_${currentID};
|
|
130
|
+
var methods_${currentID} = {${globalMethods}};
|
|
131
|
+
var olum_${currentID} = {
|
|
132
|
+
${css}
|
|
133
|
+
methods: ${scopedMethods},
|
|
134
|
+
props: ${scopedProps},
|
|
135
|
+
compName: "${compName}",
|
|
136
|
+
components: {${mkComponentsList(comps)}},
|
|
137
|
+
get getElm() {
|
|
138
|
+
${childrenVar}
|
|
139
|
+
el_${currentID}.innerHTML = \`
|
|
140
|
+
${html}\`;
|
|
141
|
+
olum.injectStyle("${compName}", olum_${currentID}.__style__());
|
|
142
|
+
return olum.handleMarkup("${compName}", "${currentID}", el_${currentID}, methods_${currentID});
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// todo: check if state.__olum__ needed since it's wiped out here
|
|
147
|
+
${ hasState ? `
|
|
148
|
+
state.__olum__ = { compName: _storeKey, compId: "${currentID}" };
|
|
149
|
+
state = olum.proxyHandler(state, ${hasWatcher ? "watcher" : "null"}, el_${currentID});
|
|
150
|
+
` : "" }
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
methods: olum_${currentID}.methods,
|
|
154
|
+
props: olum_${currentID}.props,
|
|
155
|
+
__OLUM__: olum_${currentID},
|
|
156
|
+
el: el_${currentID},
|
|
157
|
+
incomingPropsProxy: props,
|
|
158
|
+
${isDev ? `stateProps: ${hasState ? "state" : "null"},` : ""}
|
|
159
|
+
hooks: {
|
|
160
|
+
mounted: ${hasMounted ? "mounted" : "null"},
|
|
161
|
+
unMounted: ${hasUnMounted ? "unMounted" : "null"},
|
|
162
|
+
isMounted: false,
|
|
163
|
+
isUnMounted: false,
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,${createSourceMap(str, filePath)}`;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
module.exports = parser;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const { isFullArr, isUpper } = require("./helpers");
|
|
2
|
+
|
|
3
|
+
// { must be double-encoded so it survives jsdom + htmlParser transformations
|
|
4
|
+
const ESC = "&lt;-olum-&gt;";
|
|
5
|
+
|
|
6
|
+
function parseProps(attrsStr) {
|
|
7
|
+
const entries = [];
|
|
8
|
+
const srcEntries = []; // prop-name:state-key pairs for setter write-back
|
|
9
|
+
// six alternatives in priority order:
|
|
10
|
+
// 1. {key} 2. key="{expr}" 3. key='{expr}' 4. key={expr} 5. key="value" 6. key='value'
|
|
11
|
+
const attrRegex = /\{(\w+)\}|(\w+)="\{([^}]+)\}"|(\w+)='\{([^}]+)\}'|(\w+)=\{([^}]+)\}|(\w+)="([^"]*)"|(\w+)='([^']*)'/g;
|
|
12
|
+
let match;
|
|
13
|
+
while ((match = attrRegex.exec(attrsStr)) !== null) {
|
|
14
|
+
let propName, expr, isExpr = false;
|
|
15
|
+
if (match[1] !== undefined) {
|
|
16
|
+
propName = match[1]; expr = match[1]; isExpr = true; // {key} shorthand
|
|
17
|
+
} else if (match[2] && match[3] !== undefined) {
|
|
18
|
+
propName = match[2]; expr = match[3]; isExpr = true; // key="{expr}"
|
|
19
|
+
} else if (match[4] && match[5] !== undefined) {
|
|
20
|
+
propName = match[4]; expr = match[5]; isExpr = true; // key='{expr}'
|
|
21
|
+
} else if (match[6] && match[7] !== undefined) {
|
|
22
|
+
propName = match[6]; expr = match[7]; isExpr = true; // key={expr}
|
|
23
|
+
} else if (match[8] && match[9] !== undefined) {
|
|
24
|
+
// key="value" — always single-quote in JS so no " appears in the HTML attr
|
|
25
|
+
let v = match[9];
|
|
26
|
+
if (v.startsWith("'") && v.endsWith("'") && v.length >= 2) v = v.slice(1, -1);
|
|
27
|
+
propName = match[8]; expr = "'" + v.replace(/'/g, "\\'") + "'"; isExpr = false;
|
|
28
|
+
} else if (match[10] && match[11] !== undefined) {
|
|
29
|
+
propName = match[10]; expr = "'" + match[11].replace(/'/g, "\\'") + "'"; isExpr = false;
|
|
30
|
+
}
|
|
31
|
+
if (!propName) continue;
|
|
32
|
+
if (isExpr) {
|
|
33
|
+
// object literal: restore {} wrappers, escaping { so htmlParser won't turn it into ${
|
|
34
|
+
const entryExpr = /^\s*\w+\s*:/.test(expr) ? ESC + expr + '}' : expr;
|
|
35
|
+
entries.push(propName + ': ' + entryExpr);
|
|
36
|
+
// record write-back source: state.X writes to the parent's state; props.X chains up to the
|
|
37
|
+
// parent's own prop (the setter recurses until it reaches whoever holds it in state).
|
|
38
|
+
// format: propName:kind:srcKey (kind = state | props)
|
|
39
|
+
const stateMatch = expr.trim().match(/^state\.(\w+)$/);
|
|
40
|
+
const propsMatch = expr.trim().match(/^props\.(\w+)$/);
|
|
41
|
+
if (stateMatch) srcEntries.push(propName + ':state:' + stateMatch[1]);
|
|
42
|
+
else if (propsMatch) srcEntries.push(propName + ':props:' + propsMatch[1]);
|
|
43
|
+
} else {
|
|
44
|
+
entries.push(propName + ': ' + expr);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { entries, srcEntries };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildPropsAttr(attrsStr) {
|
|
51
|
+
const { entries, srcEntries } = parseProps(attrsStr);
|
|
52
|
+
|
|
53
|
+
if (!entries.length) return "";
|
|
54
|
+
const expr = "$" + ESC + "encodeURIComponent(JSON.stringify(" + ESC + entries.join(", ") + "}))}";
|
|
55
|
+
let attr = ' data-o-props="-[[-' + expr + '-]]-"';
|
|
56
|
+
if (srcEntries.length) attr += ' data-o-props-src="' + srcEntries.join("|") + '"';
|
|
57
|
+
return attr;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// handle components placeholders with olum tag <olum></olum>
|
|
61
|
+
function placeholder(content, regex, type) {
|
|
62
|
+
return content.replace(regex, (str, $1, $2) => {
|
|
63
|
+
let data = $1.split(" ");
|
|
64
|
+
if (isFullArr(data)) {
|
|
65
|
+
let name = data[0].trim();
|
|
66
|
+
if (isUpper(name[0])) {
|
|
67
|
+
name = "name='" + name + "'";
|
|
68
|
+
const propsAttr = data.length > 1 ? buildPropsAttr(data.slice(1).join(" ")) : "";
|
|
69
|
+
const slot = type === "normal" ? $2 : "";
|
|
70
|
+
return "<olum " + name + propsAttr + ">" + slot + "</olum>";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = placeholder;
|
package/lib/regex.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
script: /<script[\s\S]*?>[\s\S]*?<\/script>/gi,
|
|
3
|
+
style: /<style[\s\S]*?>[\s\S]*?<\/style>/gi,
|
|
4
|
+
forOf: /(.*)( of )(.*)/g,
|
|
5
|
+
forIn: /(.*)( in )(.*)/g,
|
|
6
|
+
htmlComment: /\<\!\-\-(?:.|\n|\r)*?-->/gi,
|
|
7
|
+
multiLineComment: /(\/\*)([\s\S]*?)(\*\/)/g,
|
|
8
|
+
selfClosingTag: /<([A-Z][^\/>]+)\/>/g,
|
|
9
|
+
normalTag: /<([A-Z][^\/>]+)>([\s\S]*?)<\/[A-Z][^\/>]+>/g,
|
|
10
|
+
exportDefault: /(export default)(\s+)?(\{)/,
|
|
11
|
+
compExt: /\.html$/i
|
|
12
|
+
};
|
package/lib/show.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const { getLineInfo } = require("./lineCounter");
|
|
2
|
+
const { RAW_OPEN, RAW_CLOSE } = require("./tokens");
|
|
3
|
+
|
|
4
|
+
function handleShowStatement(body, counter, filePath) {
|
|
5
|
+
const showElms = new Array().slice.call(body.querySelectorAll("show")).reverse(); // we must reverse to start with the very deep if tag which doesn't have nested if tags inside it
|
|
6
|
+
showElms.forEach(item => {
|
|
7
|
+
// console.log(item.outerHTML + "\n----------");
|
|
8
|
+
if (item.hasAttribute("when")) {
|
|
9
|
+
const condition = item.getAttribute("when").replace(/^\{|\}$/g, "");
|
|
10
|
+
const innerContent = item.innerHTML;
|
|
11
|
+
// RAW_OPEN/RAW_CLOSE: structural interpolation (both branches return HTML); see tokens.js / #2.
|
|
12
|
+
const ternaryOperator = RAW_OPEN+condition +"?`"+ innerContent + "`:`"+ "<div style=\"display:none;\">"+innerContent+"</div>`"+RAW_CLOSE;
|
|
13
|
+
item.insertAdjacentHTML("beforebegin", ternaryOperator);
|
|
14
|
+
item.remove();
|
|
15
|
+
} else {
|
|
16
|
+
const msg = "Missing 'when' attribute:";
|
|
17
|
+
getLineInfo(counter, item, filePath, msg);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = handleShowStatement;
|
package/lib/sourcemap.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// todo support mapping for html (stuff above script tag)
|
|
2
|
+
const MagicString = require('magic-string');
|
|
3
|
+
const { stringToBase64 } = require("./helpers");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
function mkMapping(str, filePath) {
|
|
7
|
+
const s = new MagicString(str);
|
|
8
|
+
const map = s.generateMap({
|
|
9
|
+
source: filePath,
|
|
10
|
+
file: filePath + '.map',
|
|
11
|
+
includeContent: true
|
|
12
|
+
});
|
|
13
|
+
return JSON.parse(map.toString()).mappings;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createSourceMap(str, filePath) {
|
|
17
|
+
try {
|
|
18
|
+
filePath = filePath.replaceAll(filePath.split("src")[0], "./");
|
|
19
|
+
} catch (err) {
|
|
20
|
+
filePath = "./src/" + path.basename(filePath);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const map = {
|
|
24
|
+
version: 3,
|
|
25
|
+
file: filePath,
|
|
26
|
+
mappings: mkMapping(str, filePath),
|
|
27
|
+
sources: ["olum://appName/" + filePath], // todo replace appName
|
|
28
|
+
sourcesContent: [str],
|
|
29
|
+
names: ["olum"]
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return stringToBase64(JSON.stringify(map));
|
|
33
|
+
}
|
|
34
|
+
module.exports = createSourceMap;
|
package/lib/styles.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const { getLineInfo } = require("./lineCounter");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
function handleStyles(body, counter, filePath) {
|
|
5
|
+
let name = path.basename(filePath).replace(/\.html/i, "");
|
|
6
|
+
name = name.slice(0, 1).toLowerCase() + name.slice(1); // make compName camel case
|
|
7
|
+
|
|
8
|
+
const elms = new Array().slice.call(body.querySelectorAll("*"));
|
|
9
|
+
elms.forEach(item => {
|
|
10
|
+
if (item.hasAttribute(":style")) {
|
|
11
|
+
let attrValue = item.getAttribute(":style");
|
|
12
|
+
if (attrValue) {
|
|
13
|
+
attrValue = attrValue.trim();
|
|
14
|
+
// if (attrValue.startsWith("{") && attrValue.endsWith("}")) {
|
|
15
|
+
// attrValue = attrValue.slice(1,-1);
|
|
16
|
+
// console.log(attrValue);
|
|
17
|
+
|
|
18
|
+
// added special delimiters for json in attribute to overcome double quotes issue -[[- , -]]-
|
|
19
|
+
let chain = "-[[-${JSON.stringify(" + attrValue + ")}-]]-";
|
|
20
|
+
|
|
21
|
+
chain = chain.replace(/{/g, "<-olum->"); // using <-olum-> (<-olum->) as placeholder for "{"
|
|
22
|
+
// console.log({chain});
|
|
23
|
+
item.setAttribute("data-o-style", chain);
|
|
24
|
+
item.removeAttribute(":style");
|
|
25
|
+
// console.log(item.outerHTML + "\n----------");
|
|
26
|
+
// }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = handleStyles;
|
package/lib/toJs.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
|
|
3
|
+
function toJs(statement) {
|
|
4
|
+
return statement.replace(/(from)(\s+)?.*/g, (str) => {
|
|
5
|
+
let _path = str.replace(/from|'|"/g, "").trim();
|
|
6
|
+
const extname = path.extname(_path);
|
|
7
|
+
const newPath = extname ? _path.replace(extname, "") : _path;
|
|
8
|
+
return 'from "' + newPath + '.js";';
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = toJs;
|
package/lib/tokens.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Sentinels for compiler-generated ("structural") template interpolations.
|
|
2
|
+
//
|
|
3
|
+
// #2 (escape-by-default): the htmlParser wraps every remaining `{expr}` it finds in `olum.esc(...)`.
|
|
4
|
+
// But control-flow handlers (<if>/<for>/<show>) ALSO produce `{...}` interpolations whose result is
|
|
5
|
+
// raw HTML (a ternary returning a template string, a `.map().join('')`, etc.) and must NOT be escaped.
|
|
6
|
+
// To tell the two apart, those handlers emit these sentinels instead of literal braces; htmlParser
|
|
7
|
+
// converts the sentinels straight to `${...}` (unescaped) AFTER it has escaped the user braces.
|
|
8
|
+
//
|
|
9
|
+
// Plain ASCII so they survive a jsdom parse/serialize round-trip unchanged, and regex-safe so they
|
|
10
|
+
// can be swapped with a simple global replace.
|
|
11
|
+
module.exports = {
|
|
12
|
+
RAW_OPEN: "OLUM_RAW_OPEN",
|
|
13
|
+
RAW_CLOSE: "OLUM_RAW_CLOSE",
|
|
14
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "olum-compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A module for compiling/bundling olumjs projects",
|
|
5
5
|
"main": "bin/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -25,43 +25,11 @@
|
|
|
25
25
|
"node": ">=14"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"chokidar": "^3.5.3",
|
|
28
29
|
"cross-spawn": "^7.0.3",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"babel-loader": "^8.2.2",
|
|
35
|
-
"babel-polyfill": "^6.26.0",
|
|
36
|
-
"babel-preset-es2015": "^6.24.1",
|
|
37
|
-
"clean-webpack-plugin": "^3.0.0",
|
|
38
|
-
"colors": "^1.4.0",
|
|
39
|
-
"commander": "^7.2.0",
|
|
40
|
-
"copy-webpack-plugin": "^9.0.1",
|
|
41
|
-
"css-loader": "^5.0.1",
|
|
42
|
-
"cssnano": "^5.0.6",
|
|
43
|
-
"decompress": "^4.2.1",
|
|
44
|
-
"fs-extra": "^9.1.0",
|
|
45
|
-
"gulp": "^4.0.0",
|
|
46
|
-
"gulp-watch": "^5.0.1",
|
|
47
|
-
"gulp-connect": "^5.7.0",
|
|
48
|
-
"html-webpack-plugin": "^5.3.1",
|
|
49
|
-
"inquirer": "^8.1.2",
|
|
50
|
-
"postcss": "^8.3.5",
|
|
51
|
-
"sass": "^1.32.8",
|
|
52
|
-
"sass-loader": "^10.1.0",
|
|
53
|
-
"style-loader": "^2.0.0",
|
|
54
|
-
"terser-webpack-plugin": "^5.1.1",
|
|
55
|
-
"webpack": "^5.11.0",
|
|
56
|
-
"webpack-cli": "^4.5.0",
|
|
57
|
-
"webpack-pwa-manifest": "^4.3.0",
|
|
58
|
-
"workbox-webpack-plugin": "^6.1.5",
|
|
59
|
-
"olum-devtool": "latest",
|
|
60
|
-
"string-replace-loader": "^3.0.3"
|
|
61
|
-
},
|
|
62
|
-
"browserslist": [
|
|
63
|
-
"last 2 version",
|
|
64
|
-
"> 1%",
|
|
65
|
-
"not dead"
|
|
66
|
-
]
|
|
30
|
+
"esprima": "^4.0.1",
|
|
31
|
+
"jsdom": "^19.0.0",
|
|
32
|
+
"magic-string": "^0.26.7",
|
|
33
|
+
"ws": "^8.5.0"
|
|
34
|
+
}
|
|
67
35
|
}
|
package/babel.config.json
DELETED