olum-compiler 0.6.1 → 0.7.1

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,10 +1,13 @@
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, rewriteImports } = require("../lib/helpers");
4
+ const { copy, remove, rewriteImports, ls, generateRouteManifest, generateImports, generateRoutes } = require("../lib/helpers");
5
5
 
6
- const entryPoint = path.resolve(__dirname, "../../../src");
7
- const entryPoint2 = path.resolve(__dirname, "../../../public");
6
+ // const entryPoint = path.resolve(__dirname, "../../src");
7
+ // const entryPoint2 = path.resolve(__dirname, "../../public");
8
+ // fix for new router file based
9
+ const entryPoint = path.resolve(process.cwd(), "src");
10
+ const entryPoint2 = path.resolve(process.cwd(), "public");
8
11
  module.exports = function copySrc() {
9
12
  return new Promise((resolve, reject) => {
10
13
  try {
@@ -13,13 +16,38 @@ module.exports = function copySrc() {
13
16
  if (fs.existsSync(path.resolve(__dirname, "../src"))) remove(path.resolve(__dirname, "../src"));
14
17
  // copy
15
18
  copy(entryPoint, path.resolve(__dirname, "../src"), () => {
16
- // modify main.js file import paths
19
+ // modify main.js file import paths -- old concept for main.js that was wriiten manullly but user
20
+ // const mainJsPath = path.resolve(__dirname, "../src/main.js");
21
+ // if (fs.existsSync(mainJsPath)) {
22
+ // let mainJsContent = fs.readFileSync(mainJsPath).toString();
23
+ // mainJsContent = rewriteImports(mainJsContent);
24
+ // fs.writeFileSync(mainJsPath, mainJsContent);
25
+ // }
26
+
27
+ // clean routes
28
+ let routes = ls(entryPoint, { absolute: true });
29
+ routes = routes.filter((item) => {
30
+ const extension = path.extname(item);
31
+ const isDir = fs.lstatSync(item).isDirectory();
32
+ const isHtml = fs.lstatSync(item).isFile() && extension == ".html";
33
+ if (isDir || isHtml && !item.includes("components")) return item;
34
+ });
35
+ // generate routes manifest
36
+ const manifest = generateRouteManifest(routes, entryPoint);
37
+ let has404Page = false;
38
+ if (manifest['not-found']) has404Page = true;
39
+ const libImports = `import Olum from "/node_modules/olum/dist/olum.js";\nimport Router from "/node_modules/olum-router/dist/router.js";`;
40
+ const imports = generateImports(manifest).replace(/.html/g, ".js"); // fix extension by replacing .html with .js since components are js modules
41
+ const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
42
+ routes = generateRoutes(manifest);
43
+ // console.log(libImports);
44
+ // console.log(imports);
45
+ // console.log(routes);
46
+ // console.log(config);
47
+
48
+ // automated main.js -- new concept
17
49
  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
- }
50
+ fs.writeFileSync(mainJsPath, `${libImports}\n${imports}\n${routes}\n${config}`);
23
51
  });
24
52
  copy(entryPoint2, path.resolve(__dirname, "../public"));
25
53
  resolve();
package/lib/helpers.js CHANGED
@@ -544,6 +544,125 @@ function rewriteImports(code) {
544
544
  );
545
545
  }
546
546
 
547
+ // handle routing based on files
548
+ function generateRouteManifest(paths, rootDir) {
549
+ const routes = {};
550
+
551
+ for (const absolutePath of paths) {
552
+ const relative = path.relative(rootDir, absolutePath).replace(/\\/g, "/");
553
+
554
+ // Global not-found
555
+ if (relative === "not-found.html") {
556
+ routes["not-found"] = {
557
+ page: "not-found.html",
558
+ };
559
+ continue;
560
+ }
561
+
562
+ // Only page files create routes
563
+ if (!relative.endsWith("/page.html") && relative !== "page.html") {
564
+ continue;
565
+ }
566
+
567
+ // Remove "/page.html" or "page.html"
568
+ const routePath = relative
569
+ .replace(/\/page\.html$/, "")
570
+ .replace(/^page\.html$/, "");
571
+
572
+ const segments = routePath
573
+ .split("/")
574
+ .filter(Boolean);
575
+
576
+ const params = [];
577
+
578
+ const route = "/" + segments
579
+ .map(segment => {
580
+ const match = segment.match(/^\[(.+)\]$/);
581
+
582
+ if (match) {
583
+ params.push(match[1]);
584
+ return `:${match[1]}`;
585
+ }
586
+
587
+ return segment;
588
+ })
589
+ .join("/");
590
+
591
+ routes[route === "/" ? "/" : route] = {
592
+ page: relative,
593
+ ...(params.length && { params }),
594
+ };
595
+ }
596
+
597
+ return routes;
598
+ }
599
+
600
+ function toComponentName(file) {
601
+ if (file === "page.html") return "App";
602
+ if (file === "not-found.html") return "NotFound";
603
+
604
+ return file
605
+ .replace(/\/page\.html$/, "")
606
+ .split("/")
607
+ .map(segment => {
608
+ // [slug] -> Slug
609
+ if (/^\[.+\]$/.test(segment)) {
610
+ segment = segment.slice(1, -1);
611
+ }
612
+
613
+ return segment
614
+ .split("-")
615
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
616
+ .join("");
617
+ })
618
+ .join("");
619
+ }
620
+
621
+ function generateImports(manifest) {
622
+ return Object.values(manifest)
623
+ .map(({ page }) => {
624
+ const component = toComponentName(page);
625
+ return `import ${component} from "./${page}";`;
626
+ })
627
+ .join("\n");
628
+ }
629
+
630
+ function generateRoutes(manifest) {
631
+ const routes = [];
632
+
633
+ // Root first
634
+ if (manifest["/"]) {
635
+ routes.push({
636
+ path: "/",
637
+ comp: toComponentName(manifest["/"].page),
638
+ });
639
+ }
640
+
641
+ // Other routes
642
+ for (const [route, config] of Object.entries(manifest)) {
643
+ if (route === "/" || route === "not-found") continue;
644
+
645
+ routes.push({
646
+ path: route,
647
+ comp: toComponentName(config.page),
648
+ });
649
+ }
650
+
651
+ // 404 last
652
+ if (manifest["not-found"]) {
653
+ routes.push({
654
+ path: "/404",
655
+ comp: toComponentName(manifest["not-found"].page),
656
+ });
657
+ }
658
+
659
+ return `export const routes = [
660
+ ${routes
661
+ .map(({ path, comp }) => ` { path: "${path}", comp: ${comp} }`)
662
+ .join(",\n")}
663
+ ];`;
664
+ }
665
+
547
666
  module.exports.isNum = isNum;
548
667
  module.exports.id = id;
549
668
  module.exports.isUpper = isUpper;
@@ -566,3 +685,6 @@ module.exports.getGlobalMethods = getGlobalMethods;
566
685
  module.exports.getGlobalProps = getGlobalProps;
567
686
  module.exports.minifyJS = minifyJS;
568
687
  module.exports.rewriteImports = rewriteImports;
688
+ module.exports.generateRouteManifest = generateRouteManifest;
689
+ module.exports.generateImports = generateImports;
690
+ module.exports.generateRoutes = generateRoutes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {