olum-compiler 1.0.1 → 1.1.0

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,7 +1,7 @@
1
1
  // todo check changed files only to be copied, use this project https://github.com/eissapk/diff
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
- const { copy, remove, ls, generateRouteManifest, generateImports, generateRoutes } = require("../lib/helpers");
4
+ const { copy, remove, compileRoutes } = require("../lib/helpers");
5
5
  const entryPoint = path.resolve(process.cwd(), "src").replace(/\/module/, "");
6
6
  const entryPoint2 = path.resolve(process.cwd(), "public").replace(/\/module/, "");
7
7
 
@@ -13,39 +13,10 @@ module.exports = function copySrc() {
13
13
  if (fs.existsSync(path.resolve(__dirname, "../src"))) remove(path.resolve(__dirname, "../src"));
14
14
  // copy
15
15
  copy(entryPoint, path.resolve(__dirname, "../src"), () => {
16
- // modify main.js file import paths -- old concept for main.js that was wriiten manullly but user
17
- // const mainJsPath = path.resolve(__dirname, "../src/main.js");
18
- // if (fs.existsSync(mainJsPath)) {
19
- // let mainJsContent = fs.readFileSync(mainJsPath).toString();
20
- // mainJsContent = rewriteImports(mainJsContent);
21
- // fs.writeFileSync(mainJsPath, mainJsContent);
22
- // }
23
-
24
- // clean routes
25
- let routes = ls(entryPoint, { absolute: true });
26
- routes = routes.filter((item) => {
27
- const extension = path.extname(item);
28
- const isDir = fs.lstatSync(item).isDirectory();
29
- const isHtml = fs.lstatSync(item).isFile() && extension == ".html";
30
- if (isDir || isHtml && !item.includes("components")) return item;
31
- });
32
- // generate routes manifest
33
- const manifest = generateRouteManifest(routes, entryPoint);
34
- let has404Page = false;
35
- if (manifest['not-found']) has404Page = true;
36
- const libImports = `import Olum from "/node_modules/olum/dist/olum.js";\nimport Router from "/node_modules/olum-router/dist/router.js";`;
37
- // const libImports = `import Olum from "../core/olum.js";\nimport Router from "../core/router.js";`; // for testing
38
- const imports = generateImports(manifest).replace(/.html/g, ".js"); // fix extension by replacing .html with .js since components are js modules
39
- const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
40
- routes = generateRoutes(manifest);
41
- // console.log(libImports);
42
- // console.log(imports);
43
- // console.log(routes);
44
- // console.log(config);
45
-
16
+ const mainJsContent = compileRoutes(entryPoint);
46
17
  // automated main.js -- new concept
47
18
  const mainJsPath = path.resolve(__dirname, "../src/main.js");
48
- fs.writeFileSync(mainJsPath, `${libImports}\n${imports}\n${routes}\n${config}`);
19
+ fs.writeFileSync(mainJsPath, mainJsContent);
49
20
  });
50
21
  copy(entryPoint2, path.resolve(__dirname, "../public"), () => {
51
22
  const indexHtmlPath = path.resolve(__dirname, "../public/index.html");
package/lib/helpers.js CHANGED
@@ -18,6 +18,19 @@ const stringToBase64 = (str) => Buffer.from(str).toString('base64');
18
18
  const base64ToString = (base64) => Buffer.from(base64, 'base64').toString('ascii');
19
19
  const acorn = require("acorn");
20
20
 
21
+ const cleanParentheses = (filePath, type="name") => {
22
+ if (type == "name") {
23
+ return filePath.replace(/\([^)]*\)/g, "")
24
+ } else if (type == "url"){ // clean slash after it also e.g. (main)/
25
+ return filePath.replace(/\([^)]*\)\//g, "")
26
+ }
27
+ return filePath;
28
+ }
29
+
30
+ const cleanNumbersPrefix = (name) => {
31
+ return name.replace(/\d+-?/g, "")
32
+ }
33
+
21
34
  function isUpper(char) {
22
35
  const upperChar = char.toUpperCase();
23
36
  const regex = new RegExp(upperChar);
@@ -565,9 +578,10 @@ function generateRouteManifest(paths, rootDir) {
565
578
  }
566
579
 
567
580
  // Remove "/page.html" or "page.html"
568
- const routePath = relative
581
+ let routePath = relative
569
582
  .replace(/\/page\.html$/, "")
570
583
  .replace(/^page\.html$/, "");
584
+ routePath = cleanParentheses(routePath, "url")
571
585
 
572
586
  const segments = routePath
573
587
  .split("/")
@@ -598,6 +612,7 @@ function generateRouteManifest(paths, rootDir) {
598
612
  }
599
613
 
600
614
  function toComponentName(file) {
615
+ file = cleanParentheses(file, "name");
601
616
  if (file === "page.html") return "App";
602
617
  if (file === "not-found.html") return "NotFound";
603
618
 
@@ -621,8 +636,8 @@ function toComponentName(file) {
621
636
  function generateImports(manifest) {
622
637
  return Object.values(manifest)
623
638
  .map(({ page }) => {
624
- const component = toComponentName(page);
625
- return `import ${component} from "./${page}";`;
639
+ const component = cleanNumbersPrefix(toComponentName(page));
640
+ return `import ${component} from "./${page.replace(/.html$/, ".js")}";`;
626
641
  })
627
642
  .join("\n");
628
643
  }
@@ -658,7 +673,7 @@ function generateRoutes(manifest) {
658
673
 
659
674
  return `export const routes = [
660
675
  ${routes
661
- .map(({ path, comp }) => ` { path: "${path}", comp: ${comp} }`)
676
+ .map(({ path, comp }) => ` { path: "${cleanNumbersPrefix(path)}", comp: ${cleanNumbersPrefix(comp)} }`)
662
677
  .join(",\n")}
663
678
  ];`;
664
679
  }
@@ -674,6 +689,30 @@ const libsMap = {
674
689
  }
675
690
  }
676
691
 
692
+ const compileRoutes = (entryPoint) => {
693
+ let routes = ls(entryPoint, { absolute: true });
694
+ routes = routes.filter((item) => {
695
+ const extension = path.extname(item);
696
+ const isDir = fs.lstatSync(item).isDirectory();
697
+ const isHtml = fs.lstatSync(item).isFile() && extension == ".html";
698
+ if (isDir || isHtml && !item.includes("components")) return item;
699
+ });
700
+ // generate routes manifest
701
+ const manifest = generateRouteManifest(routes, entryPoint);
702
+ let has404Page = false;
703
+ if (manifest['not-found']) has404Page = true;
704
+ const libImports = `import Olum from "/node_modules/olum/dist/olum.js";\nimport Router from "/node_modules/olum-router/dist/router.js";`;
705
+ // const libImports = `import Olum from "../core/olum.js";\nimport Router from "../core/router.js";`; // for testing
706
+ const imports = generateImports(manifest)
707
+ const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
708
+ routes = generateRoutes(manifest);
709
+ // console.log(libImports);
710
+ // console.log(imports);
711
+ // console.log(routes);
712
+ // console.log(config);
713
+ return `${libImports}\n${imports}\n${routes}\n${config}`;
714
+ }
715
+
677
716
  module.exports.isNum = isNum;
678
717
  module.exports.id = id;
679
718
  module.exports.isUpper = isUpper;
@@ -700,3 +739,4 @@ module.exports.generateRouteManifest = generateRouteManifest;
700
739
  module.exports.generateImports = generateImports;
701
740
  module.exports.generateRoutes = generateRoutes;
702
741
  module.exports.libsMap = libsMap;
742
+ module.exports.compileRoutes = compileRoutes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {