olum-compiler 1.9.1 → 1.9.2

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/copySrc.js CHANGED
@@ -1,12 +1,14 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs");
3
- const { copy, compileRoutes } = require("../lib/helpers");
3
+ const { copy, compileRoutes, ls, isFile } = require("../lib/helpers");
4
4
  const importMap = require("../lib/importMap");
5
5
  const usage = require("../lib/usage");
6
+ const splitJsBarrels = require("../lib/jsBarrels");
6
7
 
7
8
  const cwd = process.cwd();
8
- const entryPoint = path.resolve(cwd.replace(/\/module$/, ""), "src");
9
- const entryPoint2 = path.resolve(cwd.replace(/\/module$/, ""), "public");
9
+ const projectRoot = cwd.replace(/\/module$/, "");
10
+ const entryPoint = path.resolve(projectRoot, "src");
11
+ const entryPoint2 = path.resolve(projectRoot, "public");
10
12
 
11
13
  const devtoolSrc = path.resolve(
12
14
  cwd.replace(/\/module$/, ""),
@@ -36,6 +38,13 @@ function injectIndexHtml(indexHtmlPath) {
36
38
  fs.writeFileSync(indexHtmlPath, indexHtmlContent);
37
39
  }
38
40
 
41
+ function splitCopiedJs() {
42
+ ls(srcDest, { absolute: true }).forEach((file) => {
43
+ if (isFile(file) && file.endsWith(".js"))
44
+ splitJsBarrels.file(file, projectRoot);
45
+ });
46
+ }
47
+
39
48
  function handleMainJs() {
40
49
  if (fs.existsSync(mainJsPathDest)) return;
41
50
  const mainJsContent = compileRoutes(entryPoint);
@@ -61,6 +70,7 @@ function syncFile(event, file) {
61
70
  } else {
62
71
  fs.mkdirSync(path.dirname(dest), { recursive: true });
63
72
  fs.copyFileSync(file, dest);
73
+ if (inSrc && dest.endsWith(".js")) splitJsBarrels.file(dest, projectRoot);
64
74
  if (dest === indexHtmlDest) injectIndexHtml(dest);
65
75
  }
66
76
 
@@ -86,7 +96,10 @@ module.exports = function copySrc(event, file) {
86
96
  fs.rmSync(publicDest, { recursive: true, force: true });
87
97
  fs.rmSync(srcDest, { recursive: true, force: true });
88
98
 
89
- copy(entryPoint, srcDest, () => handleMainJs());
99
+ copy(entryPoint, srcDest, () => {
100
+ splitCopiedJs();
101
+ handleMainJs();
102
+ });
90
103
  copy(entryPoint2, publicDest, () => injectIndexHtml(indexHtmlDest));
91
104
 
92
105
  if (devtoolEnabled) {
@@ -0,0 +1,39 @@
1
+ const fs = require("fs");
2
+ const acorn = require("acorn");
3
+ const splitBarrels = require("./barrelSplit");
4
+
5
+ const hasBareImport = (code) => /from\s*["'][^./"']/.test(code);
6
+
7
+ function splitJsBarrels(code, rootDir) {
8
+ if (!hasBareImport(code)) return code;
9
+
10
+ let body;
11
+ try {
12
+ body = acorn.parse(code, {
13
+ ecmaVersion: "latest",
14
+ sourceType: "module",
15
+ }).body;
16
+ } catch (e) {
17
+ return code;
18
+ }
19
+
20
+ const nodes = body.filter((node) => node.type === "ImportDeclaration");
21
+
22
+ for (let i = nodes.length - 1; i >= 0; i--) {
23
+ const { start, end } = nodes[i];
24
+ const statement = code.slice(start, end);
25
+ const split = splitBarrels(statement, rootDir);
26
+ if (split !== statement)
27
+ code = code.slice(0, start) + split + code.slice(end);
28
+ }
29
+ return code;
30
+ }
31
+
32
+ function splitFile(file, rootDir) {
33
+ const code = fs.readFileSync(file, "utf8");
34
+ const split = splitJsBarrels(code, rootDir);
35
+ if (split !== code) fs.writeFileSync(file, split);
36
+ }
37
+
38
+ module.exports = splitJsBarrels;
39
+ module.exports.file = splitFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.9.1",
3
+ "version": "1.9.2",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {