olum-compiler 1.8.3 → 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 "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.8.3",
3
+ "version": "1.8.4",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {