olum-compiler 1.8.2 → 1.8.4

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.
@@ -0,0 +1,134 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const acorn = require("acorn");
4
+
5
+ const cache = new Map();
6
+
7
+ function pickEntry(exp) {
8
+ if (typeof exp === "string") return exp;
9
+ if (Array.isArray(exp)) {
10
+ for (const item of exp) {
11
+ const found = pickEntry(item);
12
+ if (found) return found;
13
+ }
14
+ return null;
15
+ }
16
+ if (exp && typeof exp === "object") {
17
+ for (const key of ["browser", "import", "module", "default"]) {
18
+ if (exp[key]) {
19
+ const found = pickEntry(exp[key]);
20
+ if (found) return found;
21
+ }
22
+ }
23
+ }
24
+ return null;
25
+ }
26
+
27
+ const nameOf = (node) => (node.type === "Identifier" ? node.name : node.value);
28
+
29
+ function barrelMap(rootDir, pkgName) {
30
+ const key = rootDir + "\0" + pkgName;
31
+ if (cache.has(key)) return cache.get(key);
32
+
33
+ let map = null;
34
+ try {
35
+ const dir = path.join(rootDir, "node_modules", pkgName);
36
+ const pkg = JSON.parse(
37
+ fs.readFileSync(path.join(dir, "package.json"), "utf8"),
38
+ );
39
+
40
+ let exp = pkg.exports;
41
+ if (exp && typeof exp === "object" && !Array.isArray(exp)) {
42
+ const keys = Object.keys(exp);
43
+ if (keys.some((k) => k.startsWith("."))) exp = exp["."];
44
+ }
45
+ let entry = pickEntry(exp) || pkg.module || pkg.main || "index.js";
46
+ let entryPath = path.resolve(dir, entry.replace(/^\.?\//, ""));
47
+ if (fs.existsSync(entryPath) && fs.statSync(entryPath).isDirectory())
48
+ entryPath = path.join(entryPath, "index.js");
49
+
50
+ const body = acorn.parse(fs.readFileSync(entryPath, "utf8"), {
51
+ ecmaVersion: "latest",
52
+ sourceType: "module",
53
+ }).body;
54
+
55
+ const isPureBarrel =
56
+ body.length > 0 &&
57
+ body.every(
58
+ (node) => node.type === "ExportNamedDeclaration" && node.source,
59
+ );
60
+
61
+ if (isPureBarrel) {
62
+ map = new Map();
63
+ body.forEach((node) => {
64
+ const target = path.resolve(path.dirname(entryPath), node.source.value);
65
+ const spec =
66
+ pkgName + "/" + path.relative(dir, target).split(path.sep).join("/");
67
+ node.specifiers.forEach((item) =>
68
+ map.set(nameOf(item.exported), {
69
+ spec,
70
+ imported: nameOf(item.local),
71
+ }),
72
+ );
73
+ });
74
+ }
75
+ } catch (e) {
76
+ map = null;
77
+ }
78
+
79
+ cache.set(key, map);
80
+ return map;
81
+ }
82
+
83
+ function splitBarrels(statement, rootDir) {
84
+ const root = (rootDir || process.cwd()).replace(/\/module$/, "");
85
+
86
+ let node;
87
+ try {
88
+ const body = acorn.parse(statement, {
89
+ ecmaVersion: "latest",
90
+ sourceType: "module",
91
+ }).body;
92
+ if (body.length !== 1 || body[0].type !== "ImportDeclaration")
93
+ return statement;
94
+ node = body[0];
95
+ } catch (e) {
96
+ return statement;
97
+ }
98
+
99
+ const source = node.source.value;
100
+ if (/^[./]/.test(source) || /^https?:/.test(source)) return statement;
101
+
102
+ const segments = source.split("/");
103
+ const pkgName = source.startsWith("@")
104
+ ? segments.slice(0, 2).join("/")
105
+ : segments[0];
106
+ if (pkgName !== source) return statement;
107
+ if (!node.specifiers.length) return statement;
108
+ if (!node.specifiers.every((item) => item.type === "ImportSpecifier"))
109
+ return statement;
110
+
111
+ const map = barrelMap(root, pkgName);
112
+ if (!map) return statement;
113
+
114
+ const lines = [];
115
+ for (const item of node.specifiers) {
116
+ const hit = map.get(nameOf(item.imported));
117
+ if (!hit) return statement;
118
+ const local = item.local.name;
119
+ lines.push(
120
+ hit.imported === "default"
121
+ ? "import " + local + ' from "' + hit.spec + '";'
122
+ : "import { " +
123
+ hit.imported +
124
+ " as " +
125
+ local +
126
+ ' } from "' +
127
+ hit.spec +
128
+ '";',
129
+ );
130
+ }
131
+ return lines.join("\n");
132
+ }
133
+
134
+ module.exports = splitBarrels;
package/lib/parser.js CHANGED
@@ -6,6 +6,7 @@ const cssParser = require("./cssParser");
6
6
  const jsParser = require("./jsParser");
7
7
  const getImports = require("./getImports");
8
8
  const toJs = require("./toJs");
9
+ const splitBarrels = require("./barrelSplit");
9
10
  const path = require("path");
10
11
  const {
11
12
  isFullArr,
@@ -131,7 +132,8 @@ const parser = (str, counter, filePath) => {
131
132
  } catch (e) {}
132
133
  }
133
134
 
134
- if (imports) imports = imports.map((item) => toJs(item.statement));
135
+ if (imports)
136
+ imports = imports.map((item) => splitBarrels(toJs(item.statement)));
135
137
 
136
138
  function mkComponentsList(comps) {
137
139
  if (!imports || !isFullArr(imports)) return "";
@@ -1,6 +1,5 @@
1
1
  const ESC = "<-olum->";
2
2
 
3
- // Component names can contain `$`, which is special inside a regex.
4
3
  const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5
4
 
6
5
  function literalExpr(raw) {
@@ -104,39 +103,20 @@ function buildPropsAttr(attrsStr, parentMethods, propsAliases) {
104
103
  "})).replace(/'/g,'%27')}";
105
104
  attr += ' data-o-props="-[[-' + expr + '-]]-"';
106
105
  }
107
- if (srcEntries.length)
106
+ if (srcEntries.length) {
108
107
  attr += ' data-o-props-src="' + srcEntries.join("|") + '"';
108
+
109
+ attr += ' data-o-props-owner="-[[-$' + ESC + '_storeKey}-]]-"';
110
+ }
109
111
  return attr;
110
112
  }
111
113
 
112
- /* ---------------------------------------------------------------------------
113
- * Component tags
114
- *
115
- * A component is a PascalCase tag -- <Card>...</Card> or <Card />. Everything
116
- * else is plain HTML and is copied through untouched.
117
- *
118
- * Tags are scanned left to right instead of being matched with one global
119
- * regex, because a regex cannot pair <Card> with its OWN </Card>: it stops at
120
- * the first uppercase close tag it meets. That breaks the moment a component
121
- * wraps another one --
122
- *
123
- * <Spinner><Spinner></Spinner></Spinner> nested under the same name
124
- * <KbdGroup><Kbd/><Kbd/></KbdGroup> more than one child
125
- * <Card><CardHeader/></Card> child name starts with the parent's
126
- * ------------------------------------------------------------------------- */
127
-
128
- // Attributes between the tag name and its `>`. Quoted values are consumed
129
- // whole so that a `>` inside one doesn't end the tag early.
130
114
  const ATTRS = "(?:\"[^\"]*\"|'[^']*'|[^\"'>])*?";
131
115
 
132
- // Names must be valid JS identifiers -- parser.js emits them straight into the
133
- // component map as `Card: Card`.
134
116
  const nextComponent = /<([A-Z][\w$]*)(?=[\s/>])/g;
135
117
 
136
118
  const tagCache = new Map();
137
119
 
138
- // `head` matches an open tag at a known offset; `open` and `close` search
139
- // forward from one. Group 1 of `head`/`open` is set when the tag self-closes.
140
120
  function tagsFor(name) {
141
121
  let tags = tagCache.get(name);
142
122
  if (!tags) {
@@ -152,9 +132,6 @@ function tagsFor(name) {
152
132
  return tags;
153
133
  }
154
134
 
155
- // Locates the `</name>` closing the tag that ends at `from`, depth-tracking
156
- // same-name tags on the way so a component pairs with its own close tag.
157
- // Returns null when there is none, i.e. the markup is unbalanced.
158
135
  function findClose(content, name, from) {
159
136
  const { open, close } = tagsFor(name);
160
137
  let depth = 1;
@@ -167,7 +144,7 @@ function findClose(content, name, from) {
167
144
  if (!nextClose) return null;
168
145
 
169
146
  if (nextOpen && nextOpen.index < nextClose.index) {
170
- if (!nextOpen[1]) depth++; // a nested twin, and not self-closing
147
+ if (!nextOpen[1]) depth++;
171
148
  i = nextOpen.index + nextOpen[0].length;
172
149
  continue;
173
150
  }
@@ -199,7 +176,6 @@ function placeholder(content, parentMethods, propsAliases) {
199
176
  head.lastIndex = start;
200
177
  const open = head.exec(content);
201
178
 
202
- // `<Card` with no `>` after it isn't a tag -- step over the `<` and go on.
203
179
  if (!open) {
204
180
  out += content.slice(i, start + 1);
205
181
  i = start + 1;
@@ -218,8 +194,6 @@ function placeholder(content, parentMethods, propsAliases) {
218
194
 
219
195
  const close = findClose(content, name, i);
220
196
  if (!close) {
221
- // Unbalanced: emit the open tag verbatim and keep scanning its content,
222
- // so the components inside it are still converted.
223
197
  out += open[0];
224
198
  continue;
225
199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.8.2",
3
+ "version": "1.8.4",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -537,9 +537,7 @@ check(
537
537
  `import Button from "./Button";\nimport Badge from "./Badge";`,
538
538
  ),
539
539
  (out) =>
540
- /<olum name="Button" data-o-props[\s\S]*>Click<\/olum>/.test(
541
- tmpl(out),
542
- ) &&
540
+ /<olum name="Button" data-o-props[\s\S]*>Click<\/olum>/.test(tmpl(out)) &&
543
541
  /<olum name="Badge"><\/olum>/.test(tmpl(out)) &&
544
542
  /<olum name="Button">Second<\/olum>/.test(tmpl(out)) &&
545
543
  parses(out),