olum-compiler 1.6.3 → 1.7.1
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 +8 -1
- package/bin/server.js +2 -2
- package/lib/changes.txt +0 -2
- package/lib/coreLibs.js +4 -1
- package/lib/cssParser.js +2 -8
- package/lib/dom.js +216 -0
- package/lib/htmlParser.js +4 -7
- package/lib/importMap.js +11 -10
- package/lib/jsParser.js +3 -21
- package/lib/parser.js +7 -6
- package/lib/regex.js +0 -5
- package/lib/sfc.js +69 -0
- package/package.json +5 -1
- package/tests/compiler.test.js +1 -1
- package/lib/clean.js +0 -4
package/bin/bundle.js
CHANGED
|
@@ -2,7 +2,7 @@ const path = require("path");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const colors = require("../lib/colors");
|
|
4
4
|
const spawn = require("../lib/cross-spawn");
|
|
5
|
-
const { useLocalCore, localLibs } = require("../lib/coreLibs");
|
|
5
|
+
const { useLocalCore, localLibs, optionalLibs } = require("../lib/coreLibs");
|
|
6
6
|
|
|
7
7
|
const cwd = process.cwd();
|
|
8
8
|
const public = path.resolve(__dirname, "../public");
|
|
@@ -93,6 +93,12 @@ async function bundle(bootAt) {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
const external = useLocalCore
|
|
97
|
+
? []
|
|
98
|
+
: optionalLibs.filter(
|
|
99
|
+
(pkgName) => !fs.existsSync(path.join(root, "node_modules", pkgName)),
|
|
100
|
+
);
|
|
101
|
+
|
|
96
102
|
const buildResult = await build({
|
|
97
103
|
root: public,
|
|
98
104
|
resolve: { alias },
|
|
@@ -102,6 +108,7 @@ async function bundle(bootAt) {
|
|
|
102
108
|
|
|
103
109
|
assetsInlineLimit: 0,
|
|
104
110
|
rollupOptions: {
|
|
111
|
+
external,
|
|
105
112
|
output: {
|
|
106
113
|
assetFileNames(info) {
|
|
107
114
|
const orig = (info.originalFileNames || [])[0];
|
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
|
|
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 =
|
|
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
package/lib/coreLibs.js
CHANGED
|
@@ -4,6 +4,9 @@ const localLibs = {
|
|
|
4
4
|
olum: "/core/olum.js",
|
|
5
5
|
"olum-router": "/core/router.js",
|
|
6
6
|
"olum-store": "/core/store.js",
|
|
7
|
+
"olum-transition": "/core/transition.js",
|
|
7
8
|
};
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
const optionalLibs = ["olum-store", "olum-transition"];
|
|
11
|
+
|
|
12
|
+
module.exports = { useLocalCore, localLibs, optionalLibs };
|
package/lib/cssParser.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const { style } = require("./regex");
|
|
2
|
-
|
|
3
1
|
function escapeRe(str) {
|
|
4
2
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
5
3
|
}
|
|
@@ -116,12 +114,8 @@ function scopeCSS(css, scopeAttr, compName) {
|
|
|
116
114
|
return out;
|
|
117
115
|
}
|
|
118
116
|
|
|
119
|
-
function cssParser(
|
|
120
|
-
|
|
121
|
-
let content = Array.isArray(arr) && arr.length ? arr[arr.length - 1] : "";
|
|
122
|
-
content = content.replace(/(<style.*?>)/, "");
|
|
123
|
-
content = content.replace(/(<\/style>)$/, "");
|
|
124
|
-
content = content.trim();
|
|
117
|
+
function cssParser(content, scopeAttr, compName) {
|
|
118
|
+
content = (content || "").trim();
|
|
125
119
|
if (scopeAttr && compName && content)
|
|
126
120
|
content = scopeCSS(content, scopeAttr, compName);
|
|
127
121
|
return "__style__() { return `" + content + "`;},\n";
|
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,7 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
const {
|
|
1
|
+
const { parseDOM } = require("./dom");
|
|
2
|
+
const { selfClosingTag, normalTag } = require("./regex");
|
|
3
3
|
const placeholder = require("./placeholder");
|
|
4
|
-
const clean = require("./clean");
|
|
5
4
|
const handleIfStatement = require("./if");
|
|
6
5
|
const handleForStatement = require("./for");
|
|
7
6
|
const handleShowStatement = require("./show");
|
|
@@ -23,9 +22,7 @@ const {
|
|
|
23
22
|
unshieldTags,
|
|
24
23
|
} = require("./tokens");
|
|
25
24
|
|
|
26
|
-
function htmlparser(
|
|
27
|
-
let content = clean(str);
|
|
28
|
-
content = content.replace(script, "").replace(style, "");
|
|
25
|
+
function htmlparser(content, counter, filePath, parentMethods, propsAliases) {
|
|
29
26
|
content = placeholder(
|
|
30
27
|
content,
|
|
31
28
|
selfClosingTag,
|
|
@@ -43,7 +40,7 @@ function htmlparser(str, counter, filePath, parentMethods, propsAliases) {
|
|
|
43
40
|
|
|
44
41
|
const anonHandlers = [];
|
|
45
42
|
|
|
46
|
-
const body =
|
|
43
|
+
const body = parseDOM(shieldTags(content)).body;
|
|
47
44
|
handleHtml(body);
|
|
48
45
|
handleLog(body, counter, filePath);
|
|
49
46
|
handleTransition(body, counter, filePath);
|
package/lib/importMap.js
CHANGED
|
@@ -3,7 +3,7 @@ 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, optionalLibs } = require("./coreLibs");
|
|
7
7
|
|
|
8
8
|
function pickEntry(exp) {
|
|
9
9
|
if (typeof exp === "string") return exp;
|
|
@@ -110,15 +110,16 @@ function importMap(srcDir) {
|
|
|
110
110
|
Object.keys(localLibs).forEach((pkgName) => {
|
|
111
111
|
const entry = resolvePackage(rootDir, pkgName);
|
|
112
112
|
if (!entry) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
if (!optionalLibs.includes(pkgName))
|
|
114
|
+
console.warn(
|
|
115
|
+
colors(
|
|
116
|
+
"yellow",
|
|
117
|
+
"Can't resolve \"" +
|
|
118
|
+
pkgName +
|
|
119
|
+
'" — install it at the project root: npm i ' +
|
|
120
|
+
pkgName,
|
|
121
|
+
),
|
|
122
|
+
);
|
|
122
123
|
return;
|
|
123
124
|
}
|
|
124
125
|
imports[pkgName] = entry;
|
package/lib/jsParser.js
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const clean = require("./clean");
|
|
5
|
-
function jsParser(str) {
|
|
6
|
-
str = clean(str);
|
|
7
|
-
const arr = str.match(script);
|
|
8
|
-
let content = Array.isArray(arr) && arr.length ? arr[0] : "";
|
|
9
|
-
|
|
10
|
-
const attrsObj = {};
|
|
11
|
-
if (content !== "") {
|
|
12
|
-
const dom = new jsdom.JSDOM(content).window.document;
|
|
13
|
-
const scriptElm = dom.querySelector("script");
|
|
14
|
-
const attrs = scriptElm.getAttributeNames();
|
|
15
|
-
if (attrs.length)
|
|
16
|
-
attrs.forEach((key) => (attrsObj[key] = scriptElm.getAttribute(key)));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
content = content.replace(/(<script.*>)/, "");
|
|
20
|
-
content = content.replace(/(<\/script>)$/, "");
|
|
1
|
+
function jsParser(sfc) {
|
|
2
|
+
let { js: content, jsAttrs } = sfc;
|
|
21
3
|
content = content.replace(/onMount\(/, "var mounted = onMount(");
|
|
22
4
|
|
|
23
5
|
content = content.replace(/const state/g, "var state");
|
|
24
|
-
return { js: content, jsAttrs
|
|
6
|
+
return { js: content, jsAttrs };
|
|
25
7
|
}
|
|
26
8
|
|
|
27
9
|
module.exports = jsParser;
|
package/lib/parser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const isDev = true;
|
|
2
|
+
const splitSFC = require("./sfc");
|
|
2
3
|
const htmlParser = require("./htmlParser");
|
|
3
4
|
const cssParser = require("./cssParser");
|
|
4
5
|
const jsParser = require("./jsParser");
|
|
@@ -18,12 +19,12 @@ const createSourceMap = require("./sourcemap");
|
|
|
18
19
|
const liveProps = require("./liveProps");
|
|
19
20
|
|
|
20
21
|
const parser = (str, counter, filePath) => {
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
const sfc = splitSFC(str);
|
|
23
|
+
|
|
23
24
|
let breaks = "";
|
|
24
|
-
for (let i = 0; i <
|
|
25
|
+
for (let i = 0; i < sfc.scriptStartLine; i++) breaks += "//\n";
|
|
25
26
|
|
|
26
|
-
let { js, jsAttrs } = jsParser(
|
|
27
|
+
let { js, jsAttrs } = jsParser(sfc);
|
|
27
28
|
|
|
28
29
|
let parentMethods = [];
|
|
29
30
|
try {
|
|
@@ -36,7 +37,7 @@ const parser = (str, counter, filePath) => {
|
|
|
36
37
|
} catch (e) {}
|
|
37
38
|
|
|
38
39
|
let { html, comps, anonHandlers } = htmlParser(
|
|
39
|
-
|
|
40
|
+
sfc.template,
|
|
40
41
|
counter,
|
|
41
42
|
filePath,
|
|
42
43
|
parentMethods,
|
|
@@ -70,7 +71,7 @@ const parser = (str, counter, filePath) => {
|
|
|
70
71
|
let compName = path.basename(filePath).replace(/.html/gi, "");
|
|
71
72
|
compName = toCamelCase(compName);
|
|
72
73
|
const currentID = id().slice(1);
|
|
73
|
-
const css = cssParser(
|
|
74
|
+
const css = cssParser(sfc.css, currentID, compName);
|
|
74
75
|
|
|
75
76
|
let globalProps = [];
|
|
76
77
|
let globalMethods = [];
|
package/lib/regex.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
script: /<script[\s\S]*?>[\s\S]*?<\/script>/gi,
|
|
3
|
-
style: /<style[\s\S]*?>[\s\S]*?<\/style>/gi,
|
|
4
2
|
forOf: /(.*)( of )(.*)/g,
|
|
5
3
|
forIn: /(.*)( in )(.*)/g,
|
|
6
|
-
htmlComment: /\<\!\-\-(?:.|\n|\r)*?-->/gi,
|
|
7
|
-
multiLineComment: /(\/\*)([\s\S]*?)(\*\/)/g,
|
|
8
4
|
|
|
9
5
|
selfClosingTag: /<([A-Z](?:"[^"]*"|'[^']*'|[^"'>])*?)\/>/g,
|
|
10
6
|
normalTag: /<([A-Z](?:"[^"]*"|'[^']*'|[^"'>])*?)>([\s\S]*?)<\/[A-Z][^>]*>/g,
|
|
11
|
-
exportDefault: /(export default)(\s+)?(\{)/,
|
|
12
7
|
compExt: /\.html$/i,
|
|
13
8
|
};
|
package/lib/sfc.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const parse5 = require("parse5");
|
|
2
|
+
|
|
3
|
+
function splitSFC(source) {
|
|
4
|
+
source = String(source);
|
|
5
|
+
const doc = parse5.parse(source, {
|
|
6
|
+
sourceCodeLocationInfo: true,
|
|
7
|
+
scriptingEnabled: false,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const scripts = [],
|
|
11
|
+
styles = [],
|
|
12
|
+
comments = [];
|
|
13
|
+
(function walk(node) {
|
|
14
|
+
for (const child of node.childNodes || []) {
|
|
15
|
+
if (child.nodeName === "#comment") {
|
|
16
|
+
comments.push(child);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (child.tagName === "script") scripts.push(child);
|
|
20
|
+
else if (child.tagName === "style") styles.push(child);
|
|
21
|
+
walk(child);
|
|
22
|
+
if (child.content) walk(child.content);
|
|
23
|
+
}
|
|
24
|
+
})(doc);
|
|
25
|
+
|
|
26
|
+
const inner = (node) => {
|
|
27
|
+
const loc = node.sourceCodeLocation;
|
|
28
|
+
if (!loc) return "";
|
|
29
|
+
const start = loc.startTag ? loc.startTag.endOffset : loc.startOffset;
|
|
30
|
+
const end = loc.endTag ? loc.endTag.startOffset : loc.endOffset;
|
|
31
|
+
return source.slice(start, end);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const script = scripts[0] || null;
|
|
35
|
+
const style = styles[0] || null;
|
|
36
|
+
|
|
37
|
+
const jsAttrs = {};
|
|
38
|
+
if (script) script.attrs.forEach((a) => (jsAttrs[a.name] = a.value));
|
|
39
|
+
|
|
40
|
+
const ranges = scripts
|
|
41
|
+
.concat(styles, comments)
|
|
42
|
+
.map((n) => n.sourceCodeLocation)
|
|
43
|
+
.filter(Boolean)
|
|
44
|
+
.map((loc) => [loc.startOffset, loc.endOffset])
|
|
45
|
+
.sort((a, b) => a[0] - b[0]);
|
|
46
|
+
let template = "";
|
|
47
|
+
let pos = 0;
|
|
48
|
+
ranges.forEach(([start, end]) => {
|
|
49
|
+
if (start >= pos) {
|
|
50
|
+
template += source.slice(pos, start);
|
|
51
|
+
pos = end;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
template += source.slice(pos);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
js: script ? inner(script) : "",
|
|
58
|
+
jsAttrs,
|
|
59
|
+
|
|
60
|
+
scriptStartLine:
|
|
61
|
+
script && script.sourceCodeLocation
|
|
62
|
+
? script.sourceCodeLocation.startLine - 1
|
|
63
|
+
: 0,
|
|
64
|
+
css: style ? inner(style) : "",
|
|
65
|
+
template: template.trim(),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = splitSFC;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "olum-compiler",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
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
|
}
|
package/tests/compiler.test.js
CHANGED
|
@@ -128,7 +128,7 @@ check(
|
|
|
128
128
|
);
|
|
129
129
|
|
|
130
130
|
check(
|
|
131
|
-
"comparison operators in a text {expr} survive the
|
|
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(
|
package/lib/clean.js
DELETED