olum-compiler 0.6.0 → 0.7.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 +34 -9
- package/bin/server.js +25 -14
- package/lib/helpers.js +122 -0
- package/package.json +1 -1
package/bin/copySrc.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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, "
|
|
7
|
-
const entryPoint2 = path.resolve(__dirname, "
|
|
6
|
+
const entryPoint = path.resolve(__dirname, "../../src");
|
|
7
|
+
const entryPoint2 = path.resolve(__dirname, "../../public");
|
|
8
8
|
module.exports = function copySrc() {
|
|
9
9
|
return new Promise((resolve, reject) => {
|
|
10
10
|
try {
|
|
@@ -13,13 +13,38 @@ 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
|
|
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 imports = generateImports(manifest).replace(/.html/g, ".js"); // fix extension by replacing .html with .js since components are js modules
|
|
38
|
+
const config = `const config = { mode: "history", root: "/", ${has404Page? `err: "/404",` : "" } routes: routes };\nconst router = new Router(config);\nnew Olum().$("#app").use(router);`;
|
|
39
|
+
routes = generateRoutes(manifest);
|
|
40
|
+
// console.log(libImports);
|
|
41
|
+
// console.log(imports);
|
|
42
|
+
// console.log(routes);
|
|
43
|
+
// console.log(config);
|
|
44
|
+
|
|
45
|
+
// automated main.js -- new concept
|
|
17
46
|
const mainJsPath = path.resolve(__dirname, "../src/main.js");
|
|
18
|
-
|
|
19
|
-
let mainJsContent = fs.readFileSync(mainJsPath).toString();
|
|
20
|
-
mainJsContent = rewriteImports(mainJsContent);
|
|
21
|
-
fs.writeFileSync(mainJsPath, mainJsContent);
|
|
22
|
-
}
|
|
47
|
+
fs.writeFileSync(mainJsPath, `${libImports}\n${imports}\n${routes}\n${config}`);
|
|
23
48
|
});
|
|
24
49
|
copy(entryPoint2, path.resolve(__dirname, "../public"));
|
|
25
50
|
resolve();
|
package/bin/server.js
CHANGED
|
@@ -39,13 +39,17 @@ function serve(entry, port) {
|
|
|
39
39
|
|
|
40
40
|
const server = http.createServer(handler);
|
|
41
41
|
function handler(req, res) {
|
|
42
|
-
|
|
42
|
+
const urlPath = req.url.split("?")[0]; // strip query string before resolving
|
|
43
|
+
|
|
44
|
+
// serve the (ws-injected) index.html — used for "/" and as the history-mode SPA fallback
|
|
45
|
+
function serveIndex() {
|
|
43
46
|
let indexPath = entry + "/index.html";
|
|
44
47
|
if (!fs.existsSync(indexPath)) indexPath = entry + "/index.htm";
|
|
45
48
|
|
|
46
49
|
fs.readFile(indexPath, (err, content) => {
|
|
47
50
|
if (err) {
|
|
48
|
-
|
|
51
|
+
res.writeHead(500, { "content-type": "text/plain" });
|
|
52
|
+
res.end("index.html not found");
|
|
49
53
|
return reject(err);
|
|
50
54
|
}
|
|
51
55
|
const dom = new jsdom.JSDOM(content.toString()).window.document;
|
|
@@ -56,19 +60,26 @@ function serve(entry, port) {
|
|
|
56
60
|
res.end("<!DOCTYPE html>" + html.outerHTML);
|
|
57
61
|
});
|
|
58
62
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
63
|
+
|
|
64
|
+
if (urlPath === "/") return serveIndex();
|
|
65
|
+
|
|
66
|
+
// serve an existing static file with its mime type
|
|
67
|
+
const finalPath = getPath(entry, urlPath);
|
|
68
|
+
if (finalPath && fs.statSync(finalPath).isFile()) {
|
|
69
|
+
const ext = path.extname(finalPath).toLowerCase();
|
|
70
|
+
const obj = mimes.find(item => ext === item.ext.toLowerCase());
|
|
71
|
+
res.writeHead(200, { "Content-Type": obj ? obj.mime : "application/octet-stream" });
|
|
72
|
+
fs.createReadStream(finalPath).pipe(res);
|
|
73
|
+
return;
|
|
71
74
|
}
|
|
75
|
+
|
|
76
|
+
// history-mode SPA fallback: a request with no file extension is a client-side route
|
|
77
|
+
// (e.g. /card) — serve index.html and let the olum router handle it. Anything with an
|
|
78
|
+
// extension is a genuinely missing asset, so return a real 404 instead of hanging.
|
|
79
|
+
if (!path.extname(urlPath)) return serveIndex();
|
|
80
|
+
|
|
81
|
+
res.writeHead(404, { "content-type": "text/plain" });
|
|
82
|
+
res.end("404 Not Found: " + urlPath);
|
|
72
83
|
}
|
|
73
84
|
const log = colors("cyan", "Serving ") + colors("green", domain);
|
|
74
85
|
resolve({ server: server, wsPort: wsPort, PORT: PORT, log: log });
|
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;
|