olum-compiler 2.1.0 → 2.1.3
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/build.js +32 -0
- package/bin/bundle.js +50 -0
- package/bin/dev.js +9 -12
- package/bin/server.js +1 -12
- package/lib/changes.txt +6 -1
- package/lib/websocket.js +2 -0
- package/package.json +1 -1
package/bin/build.js
CHANGED
|
@@ -4,6 +4,38 @@ const bundle = require("./bundle");
|
|
|
4
4
|
const compile = require("./compiler");
|
|
5
5
|
const copySrc = require("./copySrc");
|
|
6
6
|
const ssg = require("./ssg");
|
|
7
|
+
const colors = require("../lib/colors");
|
|
8
|
+
|
|
9
|
+
const lockFile = path.resolve(__dirname, "../.dev.lock");
|
|
10
|
+
const devPid = (() => {
|
|
11
|
+
try {
|
|
12
|
+
return Number(fs.readFileSync(lockFile, "utf8").trim());
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
})();
|
|
17
|
+
const devAlive = (() => {
|
|
18
|
+
try {
|
|
19
|
+
return !!devPid && !!process.kill(devPid, 0);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
return e.code === "EPERM";
|
|
22
|
+
}
|
|
23
|
+
})();
|
|
24
|
+
if (devAlive) {
|
|
25
|
+
console.error(
|
|
26
|
+
colors(
|
|
27
|
+
"red",
|
|
28
|
+
`A dev server is running (pid ${devPid}) — refusing to build.`,
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
console.error(
|
|
32
|
+
colors(
|
|
33
|
+
"white",
|
|
34
|
+
`The build would overwrite the files it serves and break the page in the browser.\nStop the dev server first, then build. # or delete ${lockFile} if that process is gone`,
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
7
39
|
|
|
8
40
|
(async function () {
|
|
9
41
|
const bootAt = Date.now();
|
package/bin/bundle.js
CHANGED
|
@@ -18,6 +18,50 @@ function getLogs() {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
function resolveBase() {
|
|
22
|
+
let conf;
|
|
23
|
+
try {
|
|
24
|
+
const pkg = JSON.parse(
|
|
25
|
+
fs.readFileSync(path.join(root, "package.json"), "utf8"),
|
|
26
|
+
);
|
|
27
|
+
conf = pkg.olum && (pkg.olum.base || pkg.olum.BASE);
|
|
28
|
+
} catch (err) {}
|
|
29
|
+
if (typeof conf !== "string" || !conf.trim()) return "/";
|
|
30
|
+
|
|
31
|
+
const value = conf.trim();
|
|
32
|
+
if (/[\s"'<>`]/.test(value)) {
|
|
33
|
+
console.error(
|
|
34
|
+
colors(
|
|
35
|
+
"red",
|
|
36
|
+
'ignoring package.json "olum.base" — expected "./", "/" or a sub-path like "/app/"',
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
return "/";
|
|
40
|
+
}
|
|
41
|
+
if (value.startsWith(".")) return "./";
|
|
42
|
+
if (/^(https?:)?\/\//.test(value)) return value.replace(/\/*$/, "/");
|
|
43
|
+
return ("/" + value.replace(/^\/+/, "")).replace(/\/*$/, "/");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function staticUrlPlugin(base) {
|
|
47
|
+
const urlAttr = /((?:src|href|poster)=\\?["'])\/([^"'`\\?#\s]+\.[a-z0-9]+)/gi;
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
name: "olum:static-urls",
|
|
51
|
+
apply: "build",
|
|
52
|
+
enforce: "post",
|
|
53
|
+
renderChunk(code) {
|
|
54
|
+
let changed = false;
|
|
55
|
+
const out = code.replace(urlAttr, (match, head, url) => {
|
|
56
|
+
if (!fs.existsSync(path.join(public, url))) return match;
|
|
57
|
+
changed = true;
|
|
58
|
+
return head + base + url;
|
|
59
|
+
});
|
|
60
|
+
return changed ? { code: out, map: null } : null;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
21
65
|
function copyStatic(from, to, absorbed) {
|
|
22
66
|
for (const name of fs.readdirSync(from)) {
|
|
23
67
|
const src = path.join(from, name);
|
|
@@ -99,9 +143,15 @@ async function bundle(bootAt) {
|
|
|
99
143
|
(pkgName) => !fs.existsSync(path.join(root, "node_modules", pkgName)),
|
|
100
144
|
);
|
|
101
145
|
|
|
146
|
+
const base = resolveBase();
|
|
147
|
+
if (base !== "/")
|
|
148
|
+
console.log(colors("white", `building with base "${base}"`));
|
|
149
|
+
|
|
102
150
|
const buildResult = await build({
|
|
103
151
|
root: public,
|
|
152
|
+
base,
|
|
104
153
|
resolve: { alias },
|
|
154
|
+
plugins: base === "/" ? [] : [staticUrlPlugin(base)],
|
|
105
155
|
|
|
106
156
|
define: { "globalThis.__OLUM_DEV__": "false" },
|
|
107
157
|
build: {
|
package/bin/dev.js
CHANGED
|
@@ -50,7 +50,7 @@ process.on("exit", () => read() === me && fs.rmSync(lockFile, { force: true }));
|
|
|
50
50
|
process.on(signal, () => process.exit(0)),
|
|
51
51
|
);
|
|
52
52
|
|
|
53
|
-
serve(entryPoint).then(({ server,
|
|
53
|
+
serve(entryPoint).then(({ server, PORT, log }) => {
|
|
54
54
|
const bootAt = Date.now();
|
|
55
55
|
copySrc()
|
|
56
56
|
.then(() => {
|
|
@@ -70,17 +70,14 @@ serve(entryPoint).then(({ server, wsPort, PORT, log, setWsPort }) => {
|
|
|
70
70
|
);
|
|
71
71
|
require("../lib/compiler");
|
|
72
72
|
|
|
73
|
-
(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
ws.send(JSON.stringify({ msg: "connected" })),
|
|
82
|
-
);
|
|
83
|
-
})(wsPort);
|
|
73
|
+
const wss = new WebSocket.Server({ server });
|
|
74
|
+
sockets = wss.clients;
|
|
75
|
+
wss.on("error", (e) => {
|
|
76
|
+
if (e.code !== "EADDRINUSE") console.error(e);
|
|
77
|
+
});
|
|
78
|
+
wss.on("connection", (ws) =>
|
|
79
|
+
ws.send(JSON.stringify({ msg: "connected" })),
|
|
80
|
+
);
|
|
84
81
|
});
|
|
85
82
|
})
|
|
86
83
|
.catch(console.error);
|
package/bin/server.js
CHANGED
|
@@ -6,11 +6,9 @@ const { parseDOM } = require("../lib/dom");
|
|
|
6
6
|
const colors = require("../lib/colors");
|
|
7
7
|
const mimes = require("./mimes.json");
|
|
8
8
|
const sitemapPath = path.resolve(__dirname, "../src/sitemap.js");
|
|
9
|
-
let wsPort = 8090;
|
|
10
9
|
|
|
11
10
|
function handleWsFile() {
|
|
12
11
|
let file = fs.readFileSync(path.resolve(__dirname, "./ws.js")).toString();
|
|
13
|
-
|
|
14
12
|
file = "<script>" + file + "</script>";
|
|
15
13
|
return file;
|
|
16
14
|
}
|
|
@@ -159,16 +157,7 @@ function serve(entry, port, mode = "development") {
|
|
|
159
157
|
}
|
|
160
158
|
const log = colors("cyan", "Serving ") + colors("green", "port " + PORT);
|
|
161
159
|
|
|
162
|
-
|
|
163
|
-
wsPort = port;
|
|
164
|
-
};
|
|
165
|
-
resolve({
|
|
166
|
-
server: server,
|
|
167
|
-
wsPort: wsPort,
|
|
168
|
-
PORT: PORT,
|
|
169
|
-
log: log,
|
|
170
|
-
setWsPort: setWsPort,
|
|
171
|
-
});
|
|
160
|
+
resolve({ server: server, PORT: PORT, log: log });
|
|
172
161
|
});
|
|
173
162
|
}
|
|
174
163
|
|
package/lib/changes.txt
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
change /home/eissapk/olum/template-compiler/src/
|
|
1
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
|
2
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
|
3
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
|
4
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
|
5
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
|
6
|
+
change /home/eissapk/olum/template-compiler/src/page.html
|
package/lib/websocket.js
CHANGED
|
@@ -290,6 +290,8 @@ class WebSocketServer extends EventEmitter {
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
handleUpgrade(req, socket, head, callback) {
|
|
293
|
+
if (socket.__olumUpgraded) return;
|
|
294
|
+
socket.__olumUpgraded = true;
|
|
293
295
|
socket.on("error", () => socket.destroy());
|
|
294
296
|
|
|
295
297
|
const key = req.headers["sec-websocket-key"];
|