chocola 1.3.10 → 1.4.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/compiler/component-processor.js +82 -19
- package/compiler/pipeline.js +2 -0
- package/package.json +1 -1
|
@@ -5,16 +5,60 @@ import chalk from "chalk";
|
|
|
5
5
|
import beautify from "js-beautify";
|
|
6
6
|
|
|
7
7
|
function scopeCss(cssString, cssId) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
function findMatchingBrace(s, openIndex) {
|
|
9
|
+
let depth = 0;
|
|
10
|
+
for (let i = openIndex; i < s.length; i++) {
|
|
11
|
+
const ch = s[i];
|
|
12
|
+
if (ch === '{') depth++;
|
|
13
|
+
else if (ch === '}') {
|
|
14
|
+
depth--;
|
|
15
|
+
if (depth === 0) return i;
|
|
16
|
+
}
|
|
11
17
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
return s.length - 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function processBlock(str) {
|
|
22
|
+
str = str.replaceAll(":root", `.${cssId}`);
|
|
23
|
+
|
|
24
|
+
let out = "";
|
|
25
|
+
let i = 0;
|
|
26
|
+
while (i < str.length) {
|
|
27
|
+
const braceIndex = str.indexOf('{', i);
|
|
28
|
+
if (braceIndex === -1) {
|
|
29
|
+
out += str.slice(i);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
const header = str.substring(i, braceIndex);
|
|
33
|
+
const endBrace = findMatchingBrace(str, braceIndex);
|
|
34
|
+
const inner = str.substring(braceIndex + 1, endBrace);
|
|
35
|
+
const innerScoped = processBlock(inner);
|
|
36
|
+
|
|
37
|
+
if (header.trim().startsWith("@")) {
|
|
38
|
+
out += header + "{" + innerScoped + "}";
|
|
39
|
+
} else {
|
|
40
|
+
const selectors = header
|
|
41
|
+
.split(",")
|
|
42
|
+
.map(s => s.trim())
|
|
43
|
+
.filter(Boolean);
|
|
44
|
+
const scopedHeader = selectors.length > 0
|
|
45
|
+
? selectors.map(sel => {
|
|
46
|
+
const s = sel.trim();
|
|
47
|
+
if (s.startsWith("." + cssId)) {
|
|
48
|
+
return s;
|
|
49
|
+
}
|
|
50
|
+
return `.${cssId} ${s}`;
|
|
51
|
+
}).join(", ")
|
|
52
|
+
: header;
|
|
53
|
+
out += scopedHeader + "{" + innerScoped + "}";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
i = endBrace + 1;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return processBlock(cssString);
|
|
18
62
|
}
|
|
19
63
|
|
|
20
64
|
function interpolateNode(node, ctxProxy) {
|
|
@@ -31,7 +75,6 @@ function interpolateNode(node, ctxProxy) {
|
|
|
31
75
|
}
|
|
32
76
|
}
|
|
33
77
|
|
|
34
|
-
|
|
35
78
|
/**
|
|
36
79
|
* Processes a single component element and inserts it into the DOM
|
|
37
80
|
* @param {Element} element
|
|
@@ -50,10 +93,16 @@ export function processComponentElement(
|
|
|
50
93
|
runtimeMap,
|
|
51
94
|
cssScopes,
|
|
52
95
|
cssScopesMap,
|
|
53
|
-
scopedStyles
|
|
96
|
+
scopedStyles,
|
|
97
|
+
renderChain = []
|
|
54
98
|
) {
|
|
55
99
|
const tagName = element.tagName.toLowerCase();
|
|
56
100
|
const compName = tagName + ".js";
|
|
101
|
+
const instance = loadedComponents.get(compName);
|
|
102
|
+
|
|
103
|
+
if (!instance || instance === undefined) return false;
|
|
104
|
+
if (renderChain && renderChain.includes(compName)) return false;
|
|
105
|
+
|
|
57
106
|
const ctx = extractContextFromElement(element);
|
|
58
107
|
const srcInnerHtml = element.innerHTML;
|
|
59
108
|
|
|
@@ -62,17 +111,21 @@ export function processComponentElement(
|
|
|
62
111
|
get(target, key) { return target[key]; }
|
|
63
112
|
});
|
|
64
113
|
|
|
65
|
-
const instance = loadedComponents.get(compName);
|
|
66
|
-
if (!instance || instance === undefined) return false;
|
|
67
|
-
|
|
68
114
|
if (instance.body) {
|
|
69
115
|
let body = instance.body;
|
|
70
116
|
|
|
71
117
|
const fragment = JSDOM.fragment(body);
|
|
118
|
+
|
|
119
|
+
const slotFragment = JSDOM.fragment(srcInnerHtml);
|
|
120
|
+
Array.from(fragment.querySelectorAll("slot")).forEach(slot => {
|
|
121
|
+
slot.replaceWith(slotFragment);
|
|
122
|
+
});
|
|
123
|
+
|
|
72
124
|
const children = Array.from(fragment.querySelectorAll("*"));
|
|
73
125
|
|
|
74
126
|
children.forEach(child => {
|
|
75
127
|
const reservedAttrs = ["if", "del-if"];
|
|
128
|
+
|
|
76
129
|
Array.from(child.attributes).forEach(attribute => {
|
|
77
130
|
if (!attribute || attribute === undefined) return;
|
|
78
131
|
if (reservedAttrs.includes(attribute.name)) return;
|
|
@@ -82,6 +135,19 @@ export function processComponentElement(
|
|
|
82
135
|
);
|
|
83
136
|
});
|
|
84
137
|
|
|
138
|
+
processComponentElement(
|
|
139
|
+
child,
|
|
140
|
+
loadedComponents,
|
|
141
|
+
runtimeChunks,
|
|
142
|
+
compIdColl,
|
|
143
|
+
letterState,
|
|
144
|
+
runtimeMap,
|
|
145
|
+
cssScopes,
|
|
146
|
+
cssScopesMap,
|
|
147
|
+
scopedStyles,
|
|
148
|
+
renderChain.concat(compName)
|
|
149
|
+
);
|
|
150
|
+
|
|
85
151
|
["if", "del-if"].forEach(statement => {
|
|
86
152
|
const statAtt = child.getAttribute(statement);
|
|
87
153
|
if (!statAtt) return;
|
|
@@ -139,17 +205,14 @@ export function processComponentElement(
|
|
|
139
205
|
}
|
|
140
206
|
}
|
|
141
207
|
|
|
142
|
-
const slotFragment = JSDOM.fragment(srcInnerHtml);
|
|
143
|
-
Array.from(fragment.querySelectorAll("slot")).forEach(slot => {
|
|
144
|
-
slot.replaceWith(slotFragment);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
208
|
let style = instance.styles && instance.styles.toString();
|
|
148
209
|
if (style) {
|
|
149
210
|
let cssId = cssScopesMap && cssScopesMap.get(compName);
|
|
150
211
|
if (!cssId) {
|
|
151
212
|
cssId = genRandomId(cssScopes, 8, true);
|
|
152
213
|
cssScopesMap.set(compName, cssId);
|
|
214
|
+
}
|
|
215
|
+
if (fragment.firstChild && fragment.firstChild.nodeType === 1) {
|
|
153
216
|
fragment.firstChild.classList.add(cssId);
|
|
154
217
|
}
|
|
155
218
|
style = scopeCss(style, cssId);
|
package/compiler/pipeline.js
CHANGED
|
@@ -103,6 +103,7 @@ export async function getSrcIndex(srcPath) {
|
|
|
103
103
|
export async function processStylesheet(link, rootDir, srcDir, outDirPath, fileIds) {
|
|
104
104
|
try {
|
|
105
105
|
const href = link.href;
|
|
106
|
+
if (href.startsWith("http://") || href.startsWith("https://")) return;
|
|
106
107
|
const stylesheetPath = path.join(rootDir, srcDir, href);
|
|
107
108
|
const css = await fs.readFile(stylesheetPath, { encoding: "utf8" });
|
|
108
109
|
const cssFileName = "css-" + genRandomId(fileIds, 6) + ".css";
|
|
@@ -233,6 +234,7 @@ export async function copyResources(rootDir, scopesCss, globalCss, srcDir, outDi
|
|
|
233
234
|
const cssAssets = [...getCssAssets(scopesCss), ...getCssAssets(globalCss)];
|
|
234
235
|
for (const assetPath of cssAssets) {
|
|
235
236
|
try {
|
|
237
|
+
if (assetPath.startsWith('http:') || assetPath.startsWith('https:')) continue;
|
|
236
238
|
const srcPath = path.join(rootDir, srcDir, assetPath);
|
|
237
239
|
const destPath = path.join(outDirPath, assetPath);
|
|
238
240
|
|