olum-compiler 1.9.3 → 1.9.5

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 CHANGED
@@ -102,6 +102,8 @@ async function bundle(bootAt) {
102
102
  const buildResult = await build({
103
103
  root: public,
104
104
  resolve: { alias },
105
+
106
+ define: { "globalThis.__OLUM_DEV__": "false" },
105
107
  build: {
106
108
  outDir,
107
109
  emptyOutDir: true,
package/bin/copySrc.js CHANGED
@@ -31,9 +31,14 @@ function injectIndexHtml(indexHtmlPath) {
31
31
 
32
32
  const importMapTag =
33
33
  process.env.NODE_ENV === "production" ? "" : `${importMap(entryPoint)}\n`;
34
+
35
+ const devFlagTag =
36
+ process.env.NODE_ENV === "production"
37
+ ? ""
38
+ : `<script>globalThis.__OLUM_DEV__ = true;</script>\n`;
34
39
  indexHtmlContent = indexHtmlContent.replace(
35
40
  /<\/body>/,
36
- `\n<div id="app"></div>\n${importMapTag}<script defer type="module" src="../src/main.js"></script>\n${devtoolScriptContent}</body>`,
41
+ `\n<div id="app"></div>\n${devFlagTag}${importMapTag}<script defer type="module" src="../src/main.js"></script>\n${devtoolScriptContent}</body>`,
37
42
  );
38
43
  fs.writeFileSync(indexHtmlPath, indexHtmlContent);
39
44
  }
package/lib/changes.txt CHANGED
@@ -1,8 +0,0 @@
1
- change /tmp/olum-dev-test-4y7l2M/src/page.html
2
- change /tmp/olum-dev-test-4y7l2M/public/main.css
3
- addDir /tmp/olum-dev-test-4y7l2M/src/blog
4
- add /tmp/olum-dev-test-4y7l2M/src/blog/page.html
5
- unlink /tmp/olum-dev-test-4y7l2M/src/blog/page.html
6
- unlinkDir /tmp/olum-dev-test-4y7l2M/src/blog
7
- change /tmp/olum-dev-test-4y7l2M/src/util.js
8
- change /tmp/olum-dev-test-4y7l2M/src/page.html
package/lib/helpers.js CHANGED
@@ -312,7 +312,7 @@ function createScopeHandler({
312
312
  if (hasState && isFullArr(stateProps)) {
313
313
  let fragments = stateProps.map((prop) => prop + ": state." + prop);
314
314
  arr.push(
315
- `get state(){return window.olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
315
+ `get state(){return olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
316
316
  );
317
317
  }
318
318
  return arr;
@@ -365,7 +365,7 @@ function createScopeHandler({
365
365
  (prop) => prop + ": state." + prop,
366
366
  );
367
367
  arr.push(
368
- `get state(){return window.olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
368
+ `get state(){return olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
369
369
  );
370
370
  }
371
371
  }
@@ -407,7 +407,7 @@ function createScopeHandler({
407
407
  (prop) => prop + ": state." + prop,
408
408
  );
409
409
  arr.push(
410
- `get state(){return window.olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
410
+ `get state(){return olum.proxyHandlerForScope(${"{" + fragments.join(",") + "}"}, state);}`,
411
411
  );
412
412
  }
413
413
  }
package/lib/parser.js CHANGED
@@ -1,5 +1,3 @@
1
- const isDev = true;
2
-
3
1
  const splitSFC = require("./sfc");
4
2
  const htmlParser = require("./htmlParser");
5
3
  const cssParser = require("./cssParser");
@@ -21,8 +19,10 @@ const createSourceMap = require("./sourcemap");
21
19
  const liveProps = require("./liveProps");
22
20
  const usage = require("./usage");
23
21
 
22
+ const RUNTIME_IMPORT = `import __olumPkg from "olum";const olum=__olumPkg.__olum||window.olum;`;
23
+
24
24
  const parser = (str, counter, filePath) => {
25
- const sfc = splitSFC(str);
25
+ const sfc = splitSFC(str, filePath);
26
26
 
27
27
  let breaks = "";
28
28
  for (let i = 0; i < sfc.scriptStartLine; i++) breaks += "//\n";
@@ -202,15 +202,11 @@ const parser = (str, counter, filePath) => {
202
202
  __OLUM__: olum_${currentID},
203
203
  el: el_${currentID},
204
204
  methodsRef: methods_${currentID},
205
- ${isDev ? `stateProps: ${hasState ? "state" : "null"},` : ""}
206
- ${
207
- isDev
208
- ? `localsRef: {${globalProps
209
- .filter((name) => name !== "state")
210
- .map((name) => `get ${name}(){return ${name}}`)
211
- .join(",")}},`
212
- : ""
213
- }
205
+ stateProps: ${hasState ? "state" : "null"},
206
+ localsRef: {${globalProps
207
+ .filter((name) => name !== "state")
208
+ .map((name) => `get ${name}(){return ${name}}`)
209
+ .join(",")}},
214
210
  hooks: {
215
211
  mounted: typeof mounted !== "undefined" ? mounted : null,
216
212
  unMounted: null,
@@ -218,7 +214,7 @@ const parser = (str, counter, filePath) => {
218
214
  isUnMounted: false,
219
215
  }
220
216
  };
221
- };`;
217
+ };${RUNTIME_IMPORT}`;
222
218
 
223
219
  try {
224
220
  out = liveProps(out);
package/lib/sfc.js CHANGED
@@ -1,7 +1,99 @@
1
1
  const parse5 = require("parse5");
2
+ const colors = require("./colors");
2
3
 
3
- function splitSFC(source) {
4
- source = String(source);
4
+ const RAW_TEXT_TAGS = ["script", "style", "textarea", "title"];
5
+
6
+ const TAG_BODY = "(?:\"[^\"]*\"|'[^']*'|[^>\"'])*?";
7
+ const SELF_CLOSING_RAW_TEXT = new RegExp(
8
+ "<(" + RAW_TEXT_TAGS.join("|") + ")(" + TAG_BODY + ")\\/\\s*>",
9
+ "gi",
10
+ );
11
+
12
+ const expandSelfClosingRawText = (source) =>
13
+ source.replace(
14
+ SELF_CLOSING_RAW_TEXT,
15
+ (_m, name, body) => "<" + name + body + "></" + name + ">",
16
+ );
17
+
18
+ function findTagEnd(text, from) {
19
+ let quote = null;
20
+ for (let i = from; i < text.length; i++) {
21
+ const char = text[i];
22
+ if (quote) {
23
+ if (char === quote) quote = null;
24
+ } else if (char === '"' || char === "'") quote = char;
25
+ else if (char === ">") return i;
26
+ }
27
+ return -1;
28
+ }
29
+
30
+ function scanRawTextBlocks(text, names) {
31
+ const blocks = [];
32
+ const open = new RegExp("<(" + names.join("|") + ")(?=[\\s/>])", "gi");
33
+ let match;
34
+ while ((match = open.exec(text)) !== null) {
35
+ const start = match.index;
36
+ const tagEnd = findTagEnd(text, start);
37
+ if (tagEnd === -1) break;
38
+ const innerStart = tagEnd + 1;
39
+ const close = new RegExp("<\\/" + match[1] + "\\s*>", "i");
40
+ const closeMatch = text.slice(innerStart).match(close);
41
+ const innerEnd = closeMatch ? innerStart + closeMatch.index : text.length;
42
+ const inner = text.slice(innerStart, innerEnd);
43
+
44
+ if (!closeMatch && new RegExp(open.source, "i").test(inner)) {
45
+ open.lastIndex = innerStart;
46
+ blocks.push({
47
+ name: match[1].toLowerCase(),
48
+ inner: "",
49
+ start,
50
+ end: innerStart,
51
+ spurious: true,
52
+ });
53
+ continue;
54
+ }
55
+ const end = closeMatch ? innerEnd + closeMatch[0].length : text.length;
56
+ blocks.push({ name: match[1].toLowerCase(), inner, start, end });
57
+ open.lastIndex = end;
58
+ }
59
+ return blocks;
60
+ }
61
+
62
+ function neutralizeStrayRawText(source) {
63
+ const stray = scanRawTextBlocks(source, ["script", "style"]).filter(
64
+ (b) => b.spurious,
65
+ );
66
+ if (!stray.length) return source;
67
+ let out = "";
68
+ let cursor = 0;
69
+ stray.forEach((block) => {
70
+ out +=
71
+ source.slice(cursor, block.start) +
72
+ "&lt;" +
73
+ source.slice(block.start + 1, block.end);
74
+ cursor = block.end;
75
+ });
76
+ return out + source.slice(cursor);
77
+ }
78
+
79
+ function warn(filePath, line, msg) {
80
+ console.warn(
81
+ colors("yellow.bold", "\n<<Olum Warning>>\n") +
82
+ colors(
83
+ "yellow",
84
+ " File: " +
85
+ (filePath || "unknown") +
86
+ "\n Line: " +
87
+ line +
88
+ "\n " +
89
+ msg +
90
+ "\n",
91
+ ),
92
+ );
93
+ }
94
+
95
+ function splitSFC(source, filePath) {
96
+ source = neutralizeStrayRawText(expandSelfClosingRawText(String(source)));
5
97
  const doc = parse5.parse(source, {
6
98
  sourceCodeLocationInfo: true,
7
99
  scriptingEnabled: false,
@@ -53,15 +145,48 @@ function splitSFC(source) {
53
145
  });
54
146
  template += source.slice(pos);
55
147
 
148
+ let js = script ? inner(script) : "";
149
+ let css = style ? inner(style) : "";
150
+ let scriptStartLine =
151
+ script && script.sourceCodeLocation
152
+ ? script.sourceCodeLocation.startLine - 1
153
+ : 0;
154
+
155
+ const leaked = scanRawTextBlocks(template, ["script", "style"]);
156
+ if (leaked.length) {
157
+ const sourceLine = (block) => {
158
+ const at = source.indexOf(template.slice(block.start, block.end));
159
+ return at === -1 ? 0 : source.slice(0, at).split("\n").length;
160
+ };
161
+ warn(
162
+ filePath,
163
+ sourceLine(leaked[0]),
164
+ "A <" +
165
+ leaked[0].name +
166
+ "> tag stayed inside the markup, so an element above it is still open: an unterminated attribute quote, a literal <script> typed as text, or an unclosed <textarea>/<title>. The tag was pulled out anyway — fix the markup above it.",
167
+ );
168
+ let cleaned = "";
169
+ let cursor = 0;
170
+ leaked.forEach((block) => {
171
+ cleaned += template.slice(cursor, block.start);
172
+ cursor = block.end;
173
+ if (block.name === "style" && !css.trim()) css = block.inner;
174
+ if (block.name === "script" && !js.trim()) {
175
+ js = block.inner;
176
+ const line = sourceLine(block);
177
+ if (line) scriptStartLine = line - 1;
178
+ }
179
+ });
180
+ cleaned += template.slice(cursor);
181
+ template = cleaned;
182
+ }
183
+
56
184
  return {
57
- js: script ? inner(script) : "",
185
+ js,
58
186
  jsAttrs,
59
187
 
60
- scriptStartLine:
61
- script && script.sourceCodeLocation
62
- ? script.sourceCodeLocation.startLine - 1
63
- : 0,
64
- css: style ? inner(style) : "",
188
+ scriptStartLine,
189
+ css,
65
190
  template: template.trim(),
66
191
  };
67
192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.9.3",
3
+ "version": "1.9.5",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -22,6 +22,10 @@ function compile(template) {
22
22
  function parses(out) {
23
23
  const body = out
24
24
  .replace(/^\s*import .*$/gm, "")
25
+ .replace(
26
+ /import __olumPkg from "olum";const olum=__olumPkg\.__olum\|\|window\.olum;/g,
27
+ "",
28
+ )
25
29
  .replace(/^\s*export\s+default\s+/m, "const __x=");
26
30
  new Function(body);
27
31
  return true;
@@ -1297,6 +1301,71 @@ mapOf(
1297
1301
  (out) => Object.keys(out).length === 0,
1298
1302
  );
1299
1303
 
1304
+ section("§26 <style> extraction vs malformed markup");
1305
+
1306
+ const CSS_BLOCK = `<style>\n main#home {\n padding: 20px;\n }\n</style>`;
1307
+ function styleOf(out) {
1308
+ const m = out.match(/__style__\(\) \{ return `([\s\S]*?)`;\}/);
1309
+ return m ? m[1] : "";
1310
+ }
1311
+ function styleSplit(name, template) {
1312
+ check(name, comp(template + "\n" + CSS_BLOCK), (out) => {
1313
+ const html = tmpl(out);
1314
+ return (
1315
+ !/<style/i.test(html) &&
1316
+ !/olum\.esc\(\s*padding/.test(html) &&
1317
+ /padding: 20px/.test(styleOf(out)) &&
1318
+ parses(out)
1319
+ );
1320
+ });
1321
+ }
1322
+
1323
+ const realWarn = console.warn;
1324
+ console.warn = () => {};
1325
+
1326
+ styleSplit(
1327
+ "keeps a plain <style> out of the markup",
1328
+ `<main id="home">\n</main>`,
1329
+ );
1330
+ styleSplit(
1331
+ "survives a self-closing <textarea />",
1332
+ `<main id="home">\n <textarea />\n</main>`,
1333
+ );
1334
+ styleSplit(
1335
+ "survives a self-closing <title />",
1336
+ `<main id="home">\n <title />\n</main>`,
1337
+ );
1338
+ styleSplit(
1339
+ "survives a self-closing <script src=... />",
1340
+ `<script src="a.js" />\n<main id="home"></main>`,
1341
+ );
1342
+ styleSplit(
1343
+ "survives an unterminated attribute quote",
1344
+ `<main class="home>\n</main>`,
1345
+ );
1346
+ styleSplit(
1347
+ "survives an unclosed <textarea>",
1348
+ `<main id="home">\n <textarea>hi\n</main>`,
1349
+ );
1350
+ styleSplit(
1351
+ "survives a literal <script> written in text",
1352
+ `<main id="home"><code><script></code></main>`,
1353
+ );
1354
+
1355
+ check(
1356
+ "a <style> inside a JS template literal stays in the script",
1357
+ "<script>\n" +
1358
+ STATE +
1359
+ '\nconst t = `<style>x</style>`;\n</script>\n<main id="home"></main>\n' +
1360
+ CSS_BLOCK,
1361
+ (out) =>
1362
+ /padding: 20px/.test(styleOf(out)) &&
1363
+ !/<style/i.test(tmpl(out)) &&
1364
+ parses(out),
1365
+ );
1366
+
1367
+ console.warn = realWarn;
1368
+
1300
1369
  console.log("\n========================");
1301
1370
  const summary = `${passed} passed, ${failed} failed`;
1302
1371
  console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
@@ -1314,7 +1383,7 @@ if (failed) {
1314
1383
  console.log("");
1315
1384
  }
1316
1385
 
1317
- const EXPECTED_CHECKS = 110;
1386
+ const EXPECTED_CHECKS = 118;
1318
1387
  const total = passed + failed;
1319
1388
  if (total !== EXPECTED_CHECKS) {
1320
1389
  console.log(