olum-compiler 1.7.0 → 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/lib/coreLibs.js +4 -1
- package/lib/cssParser.js +2 -8
- package/lib/htmlParser.js +2 -5
- 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 +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/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/htmlParser.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const { parseDOM } = require("./dom");
|
|
2
|
-
const {
|
|
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,
|
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 = parseDOM(content);
|
|
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
package/lib/clean.js
DELETED