olum-compiler 1.7.1 → 1.8.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 +24 -2
- package/bin/dev.js +53 -14
- package/bin/server.js +26 -0
- package/bin/ws.js +59 -53
- package/lib/parser.js +32 -0
- package/lib/usage.js +39 -0
- package/package.json +1 -1
package/bin/copySrc.js
CHANGED
|
@@ -2,11 +2,20 @@ const path = require("path");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const { copy, compileRoutes } = require("../lib/helpers");
|
|
4
4
|
const importMap = require("../lib/importMap");
|
|
5
|
+
const usage = require("../lib/usage");
|
|
5
6
|
|
|
6
7
|
const cwd = process.cwd();
|
|
7
8
|
const entryPoint = path.resolve(cwd.replace(/\/module$/, ""), "src");
|
|
8
9
|
const entryPoint2 = path.resolve(cwd.replace(/\/module$/, ""), "public");
|
|
9
10
|
|
|
11
|
+
const devtoolSrc = path.resolve(
|
|
12
|
+
cwd.replace(/\/module$/, ""),
|
|
13
|
+
"node_modules/olum-devtool/dist",
|
|
14
|
+
);
|
|
15
|
+
const devtoolScript = path.resolve(devtoolSrc, "./script.js");
|
|
16
|
+
const devtoolEnabled =
|
|
17
|
+
process.env.NODE_ENV === "development" && fs.existsSync(devtoolScript);
|
|
18
|
+
|
|
10
19
|
const srcDest = path.resolve(__dirname, "../src");
|
|
11
20
|
const publicDest = path.resolve(__dirname, "../public");
|
|
12
21
|
const indexHtmlDest = path.resolve(__dirname, "../public/index.html");
|
|
@@ -14,12 +23,15 @@ const mainJsPathDest = path.resolve(__dirname, "../src/main.js");
|
|
|
14
23
|
|
|
15
24
|
function injectIndexHtml(indexHtmlPath) {
|
|
16
25
|
let indexHtmlContent = fs.readFileSync(indexHtmlPath).toString();
|
|
26
|
+
const devtoolScriptContent = devtoolEnabled
|
|
27
|
+
? "<script>" + fs.readFileSync(devtoolScript).toString() + "</script>\n"
|
|
28
|
+
: "";
|
|
17
29
|
|
|
18
30
|
const importMapTag =
|
|
19
31
|
process.env.NODE_ENV === "production" ? "" : `${importMap(entryPoint)}\n`;
|
|
20
32
|
indexHtmlContent = indexHtmlContent.replace(
|
|
21
33
|
/<\/body>/,
|
|
22
|
-
`\n<div id="app"></div>\n${importMapTag}<script defer type="module" src="../src/main.js"></script>\n</body>`,
|
|
34
|
+
`\n<div id="app"></div>\n${importMapTag}<script defer type="module" src="../src/main.js"></script>\n${devtoolScriptContent}</body>`,
|
|
23
35
|
);
|
|
24
36
|
fs.writeFileSync(indexHtmlPath, indexHtmlContent);
|
|
25
37
|
}
|
|
@@ -42,8 +54,10 @@ function syncFile(event, file) {
|
|
|
42
54
|
: path.join(publicDest, path.relative(entryPoint2, file));
|
|
43
55
|
if (event.startsWith("unlink")) {
|
|
44
56
|
fs.rmSync(dest, { recursive: true, force: true });
|
|
45
|
-
if (inSrc && dest.endsWith(".html"))
|
|
57
|
+
if (inSrc && dest.endsWith(".html")) {
|
|
46
58
|
fs.rmSync(dest.replace(/\.html$/, ".js"), { force: true });
|
|
59
|
+
usage.drop(dest);
|
|
60
|
+
}
|
|
47
61
|
} else {
|
|
48
62
|
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
49
63
|
fs.copyFileSync(file, dest);
|
|
@@ -75,6 +89,14 @@ module.exports = function copySrc(event, file) {
|
|
|
75
89
|
copy(entryPoint, srcDest, () => handleMainJs());
|
|
76
90
|
copy(entryPoint2, publicDest, () => injectIndexHtml(indexHtmlDest));
|
|
77
91
|
|
|
92
|
+
if (devtoolEnabled) {
|
|
93
|
+
fs.mkdirSync(publicDest + "/__olum_devtool", {
|
|
94
|
+
recursive: true,
|
|
95
|
+
force: true,
|
|
96
|
+
});
|
|
97
|
+
copy(devtoolSrc, publicDest + "/__olum_devtool");
|
|
98
|
+
}
|
|
99
|
+
|
|
78
100
|
resolve(null);
|
|
79
101
|
} catch (err) {
|
|
80
102
|
reject(err);
|
package/bin/dev.js
CHANGED
|
@@ -7,8 +7,49 @@ const colors = require("../lib/colors");
|
|
|
7
7
|
const path = require("path");
|
|
8
8
|
const fs = require("fs");
|
|
9
9
|
|
|
10
|
-
let
|
|
10
|
+
let sockets;
|
|
11
11
|
const entryPoint = path.resolve(__dirname, "../public");
|
|
12
|
+
|
|
13
|
+
const lockFile = path.resolve(__dirname, "../.dev.lock");
|
|
14
|
+
const me = String(process.pid);
|
|
15
|
+
const read = () => {
|
|
16
|
+
try {
|
|
17
|
+
return fs.readFileSync(lockFile, "utf8").trim();
|
|
18
|
+
} catch (e) {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const alive = (pid) => {
|
|
23
|
+
try {
|
|
24
|
+
return !!process.kill(pid, 0);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return e.code === "EPERM";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const holder = Number(read());
|
|
31
|
+
if (holder && holder !== process.pid && alive(holder)) {
|
|
32
|
+
console.error(
|
|
33
|
+
colors(
|
|
34
|
+
"red",
|
|
35
|
+
`A dev server for this project is already running (pid ${holder}).`,
|
|
36
|
+
),
|
|
37
|
+
);
|
|
38
|
+
console.error(
|
|
39
|
+
colors(
|
|
40
|
+
"white",
|
|
41
|
+
`Stop it first — two of them share one build output and will overwrite each other's work.\n kill ${holder} # or delete ${lockFile} if that process is gone`,
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fs.writeFileSync(lockFile, me);
|
|
48
|
+
process.on("exit", () => read() === me && fs.rmSync(lockFile, { force: true }));
|
|
49
|
+
["SIGINT", "SIGTERM", "SIGHUP"].forEach((signal) =>
|
|
50
|
+
process.on(signal, () => process.exit(0)),
|
|
51
|
+
);
|
|
52
|
+
|
|
12
53
|
serve(entryPoint).then(({ server, wsPort, PORT, log, setWsPort }) => {
|
|
13
54
|
const bootAt = Date.now();
|
|
14
55
|
copySrc()
|
|
@@ -31,34 +72,32 @@ serve(entryPoint).then(({ server, wsPort, PORT, log, setWsPort }) => {
|
|
|
31
72
|
|
|
32
73
|
(function startWs(port) {
|
|
33
74
|
const wss = new WebSocket.Server({ server });
|
|
75
|
+
sockets = wss.clients;
|
|
34
76
|
wss.on("listening", () => setWsPort(port));
|
|
35
77
|
wss.on("error", (e) => {
|
|
36
78
|
if (e.code === "EADDRINUSE") startWs(port + 1);
|
|
37
79
|
});
|
|
38
|
-
wss.on("connection", (ws) =>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
80
|
+
wss.on("connection", (ws) =>
|
|
81
|
+
ws.send(JSON.stringify({ msg: "connected" })),
|
|
82
|
+
);
|
|
42
83
|
})(wsPort);
|
|
43
84
|
});
|
|
44
85
|
})
|
|
45
86
|
.catch(console.error);
|
|
46
87
|
});
|
|
47
88
|
|
|
89
|
+
const send = (data) =>
|
|
90
|
+
sockets && sockets.forEach((ws) => ws.send(JSON.stringify(data)));
|
|
91
|
+
|
|
92
|
+
const logFile = path.resolve(__dirname, "../lib/log.txt");
|
|
48
93
|
function getLogs() {
|
|
49
|
-
|
|
50
|
-
if (fs.existsSync(logFilePath)) {
|
|
51
|
-
return fs.readFileSync(logFilePath).toString();
|
|
52
|
-
} else {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
94
|
+
return fs.existsSync(logFile) ? fs.readFileSync(logFile).toString() : null;
|
|
55
95
|
}
|
|
56
96
|
|
|
57
|
-
const send = (data) => instance && instance.send(JSON.stringify(data));
|
|
58
|
-
|
|
59
97
|
function notify() {
|
|
60
98
|
const logs = getLogs();
|
|
61
|
-
|
|
99
|
+
|
|
100
|
+
send(logs && logs.trim() ? { msg: "warn", data: logs } : { msg: "reload" });
|
|
62
101
|
}
|
|
63
102
|
|
|
64
103
|
const isComp = (dest) =>
|
package/bin/server.js
CHANGED
|
@@ -72,8 +72,34 @@ function serve(entry, port, mode = "development") {
|
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
function serveDevtools() {
|
|
76
|
+
const devtoolsPath = path.join(entry, "__olum_devtool", "index.html");
|
|
77
|
+
|
|
78
|
+
fs.readFile(devtoolsPath, (err, content) => {
|
|
79
|
+
if (err) {
|
|
80
|
+
res.writeHead(404, { "content-type": "text/plain" });
|
|
81
|
+
res.end("404 Not Found: " + urlPath);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const dom = parseDOM(content.toString());
|
|
85
|
+
const html = dom.querySelector("html");
|
|
86
|
+
if (mode == "development") {
|
|
87
|
+
const wsContent = handleWsFile();
|
|
88
|
+
dom.body.insertAdjacentHTML("beforeend", wsContent);
|
|
89
|
+
}
|
|
90
|
+
res.writeHead(200, {
|
|
91
|
+
"content-type": "text/html",
|
|
92
|
+
"cache-control": "no-cache",
|
|
93
|
+
});
|
|
94
|
+
res.end("<!DOCTYPE html>" + html.outerHTML);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
75
98
|
if (urlPath === "/") return serveIndex();
|
|
76
99
|
|
|
100
|
+
if (urlPath === "/__olum_devtool" || urlPath === "/__olum_devtool/")
|
|
101
|
+
return serveDevtools();
|
|
102
|
+
|
|
77
103
|
const finalPath = getPath(entry, urlPath);
|
|
78
104
|
if (finalPath && fs.statSync(finalPath).isFile()) {
|
|
79
105
|
const ext = path.extname(finalPath).toLowerCase();
|
package/bin/ws.js
CHANGED
|
@@ -1,61 +1,67 @@
|
|
|
1
1
|
(() => {
|
|
2
|
-
if ("WebSocket" in window)
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let attempts = 0;
|
|
7
|
-
const wsProtocol = location.protocol === "https:" ? "wss:" : "ws:";
|
|
2
|
+
if (!("WebSocket" in window))
|
|
3
|
+
return console.error(
|
|
4
|
+
"Upgrade your browser. This Browser is doesn't support WebSocket for Live-Reloading.",
|
|
5
|
+
);
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const url = `${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}`;
|
|
8
|
+
const esc = (str) => str.replace(/>/g, ">").replace(/</g, "<");
|
|
9
|
+
const retry = 500;
|
|
10
|
+
const maxAttempts = 30;
|
|
11
|
+
let attempts = 0;
|
|
12
|
+
let dropped = false;
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const stderr = obj.data.trim();
|
|
19
|
-
const arr = stderr.split("<<Olum Warning>>");
|
|
20
|
-
const markup = arr
|
|
21
|
-
.map((item) => {
|
|
22
|
-
if (item.trim() !== "")
|
|
23
|
-
return `<pre style="border-bottom: 1px solid #ccc; padding-bottom:10px;">${parse(item)}</pre>`;
|
|
24
|
-
})
|
|
25
|
-
.join("");
|
|
26
|
-
document.body.innerHTML = markup;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
14
|
+
const show = (data) => {
|
|
15
|
+
const blocks = data
|
|
16
|
+
.split("<<Olum Warning>>")
|
|
17
|
+
.map((item) => item.trim())
|
|
18
|
+
.filter(Boolean);
|
|
19
|
+
if (!blocks.length) return;
|
|
29
20
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
const box =
|
|
22
|
+
document.getElementById("olum-warning") ||
|
|
23
|
+
document.body.appendChild(document.createElement("div"));
|
|
24
|
+
box.id = "olum-warning";
|
|
25
|
+
box.style.cssText =
|
|
26
|
+
"position:fixed;top:0;left:0;right:0;z-index:2147483001;max-height:60vh;overflow:auto;padding:12px 16px;" +
|
|
27
|
+
"background:#1c1917;color:#fde68a;box-shadow:0 2px 12px rgba(0,0,0,.35);font:12px/1.6 ui-monospace,SFMono-Regular,Menlo,monospace";
|
|
28
|
+
box.innerHTML =
|
|
29
|
+
'<button style="float:right;border:0;background:none;color:inherit;font:inherit;cursor:pointer">dismiss ✕</button>' +
|
|
30
|
+
blocks
|
|
31
|
+
.map(
|
|
32
|
+
(item) =>
|
|
33
|
+
`<pre style="margin:0 0 8px;white-space:pre-wrap">${esc(item)}</pre>`,
|
|
34
|
+
)
|
|
35
|
+
.join("");
|
|
36
|
+
box.querySelector("button").onclick = () => box.remove();
|
|
37
|
+
};
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
const enabled = () => {
|
|
40
|
+
const key = "IS_FIRST_TIME_OLUM_HOT_RELOAD";
|
|
41
|
+
if (!sessionStorage || sessionStorage.getItem(key)) return;
|
|
42
|
+
sessionStorage.setItem(key, true);
|
|
43
|
+
console.log("Olum hot-reload enabled.");
|
|
44
|
+
};
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
const attach = (socket) => {
|
|
47
|
+
socket.onopen = () => (dropped ? location.reload() : (attempts = 0));
|
|
48
|
+
socket.onmessage = (event) => {
|
|
49
|
+
const { msg, data } = JSON.parse(event.data);
|
|
50
|
+
if (msg === "connected") enabled();
|
|
51
|
+
else if (msg === "reload") location.reload();
|
|
52
|
+
else if (msg === "css")
|
|
53
|
+
document
|
|
54
|
+
.querySelectorAll("link[rel='stylesheet']")
|
|
55
|
+
.forEach((sheet) => sheet.replaceWith(sheet));
|
|
56
|
+
else if (msg === "warn") show(data);
|
|
57
|
+
};
|
|
58
|
+
socket.onclose = () => {
|
|
59
|
+
dropped = true;
|
|
60
|
+
if (++attempts > maxAttempts)
|
|
61
|
+
return console.error("Could not reconnect to dev server.");
|
|
62
|
+
setTimeout(() => attach(new WebSocket(url)), retry);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
43
65
|
|
|
44
|
-
|
|
45
|
-
const reloadIfCanConnect = () => {
|
|
46
|
-
attempts++;
|
|
47
|
-
if (attempts > maxAttempts)
|
|
48
|
-
return console.error("Could not reconnect to dev server.");
|
|
49
|
-
socket = new WebSocket(socketUrl);
|
|
50
|
-
socket.addEventListener("error", () =>
|
|
51
|
-
setTimeout(reloadIfCanConnect, interval),
|
|
52
|
-
);
|
|
53
|
-
};
|
|
54
|
-
reloadIfCanConnect();
|
|
55
|
-
});
|
|
56
|
-
} else {
|
|
57
|
-
console.error(
|
|
58
|
-
"Upgrade your browser. This Browser is doesn't support WebSocket for Live-Reloading.",
|
|
59
|
-
);
|
|
60
|
-
}
|
|
66
|
+
attach(new WebSocket(url));
|
|
61
67
|
})();
|
package/lib/parser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const isDev = true;
|
|
2
|
+
|
|
2
3
|
const splitSFC = require("./sfc");
|
|
3
4
|
const htmlParser = require("./htmlParser");
|
|
4
5
|
const cssParser = require("./cssParser");
|
|
@@ -17,6 +18,7 @@ const {
|
|
|
17
18
|
} = require("./helpers");
|
|
18
19
|
const createSourceMap = require("./sourcemap");
|
|
19
20
|
const liveProps = require("./liveProps");
|
|
21
|
+
const usage = require("./usage");
|
|
20
22
|
|
|
21
23
|
const parser = (str, counter, filePath) => {
|
|
22
24
|
const sfc = splitSFC(str);
|
|
@@ -108,6 +110,27 @@ const parser = (str, counter, filePath) => {
|
|
|
108
110
|
const scopedProps = handleScope(globalProps, "props");
|
|
109
111
|
const scopedMethods = handleScope(globalMethods, "methods");
|
|
110
112
|
|
|
113
|
+
if (process.env.NODE_ENV === "development") {
|
|
114
|
+
try {
|
|
115
|
+
const children = [];
|
|
116
|
+
(comps || []).forEach((name) => {
|
|
117
|
+
const hit = (imports || []).find((item) =>
|
|
118
|
+
new RegExp("\\b" + name + "\\b").test(item.statement),
|
|
119
|
+
);
|
|
120
|
+
const spec =
|
|
121
|
+
hit && (hit.statement.match(/from\s+["'](.+?)["']/) || [])[1];
|
|
122
|
+
if (!spec || !spec.startsWith(".")) return;
|
|
123
|
+
const child = usage.relative(
|
|
124
|
+
path
|
|
125
|
+
.resolve(path.dirname(filePath), spec)
|
|
126
|
+
.replace(/\.(js|html)$/, "") + ".html",
|
|
127
|
+
);
|
|
128
|
+
if (!children.includes(child)) children.push(child);
|
|
129
|
+
});
|
|
130
|
+
usage.record(filePath, children);
|
|
131
|
+
} catch (e) {}
|
|
132
|
+
}
|
|
133
|
+
|
|
111
134
|
if (imports) imports = imports.map((item) => toJs(item.statement));
|
|
112
135
|
|
|
113
136
|
function mkComponentsList(comps) {
|
|
@@ -149,6 +172,7 @@ const parser = (str, counter, filePath) => {
|
|
|
149
172
|
methods: ${scopedMethods},
|
|
150
173
|
props: ${scopedProps},
|
|
151
174
|
compName: "${compName}",
|
|
175
|
+
${process.env.NODE_ENV === "development" ? `file: ${JSON.stringify(usage.relative(filePath))},` : ""}
|
|
152
176
|
deps: ${templateDeps ? JSON.stringify(templateDeps) : "null"},
|
|
153
177
|
components: {${mkComponentsList(comps)}},
|
|
154
178
|
get getElm() {
|
|
@@ -177,6 +201,14 @@ const parser = (str, counter, filePath) => {
|
|
|
177
201
|
el: el_${currentID},
|
|
178
202
|
methodsRef: methods_${currentID},
|
|
179
203
|
${isDev ? `stateProps: ${hasState ? "state" : "null"},` : ""}
|
|
204
|
+
${
|
|
205
|
+
isDev
|
|
206
|
+
? `localsRef: {${globalProps
|
|
207
|
+
.filter((name) => name !== "state")
|
|
208
|
+
.map((name) => `get ${name}(){return ${name}}`)
|
|
209
|
+
.join(",")}},`
|
|
210
|
+
: ""
|
|
211
|
+
}
|
|
180
212
|
hooks: {
|
|
181
213
|
mounted: typeof mounted !== "undefined" ? mounted : null,
|
|
182
214
|
unMounted: null,
|
package/lib/usage.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
const srcDest = path.resolve(__dirname, "../src");
|
|
5
|
+
const mapFile = path.join(srcDest, "usage.json");
|
|
6
|
+
|
|
7
|
+
const relative = (file) =>
|
|
8
|
+
"src/" + path.relative(srcDest, file).split(path.sep).join("/");
|
|
9
|
+
|
|
10
|
+
const read = () => {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(fs.readFileSync(mapFile, "utf8"));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const write = (map) => {
|
|
19
|
+
try {
|
|
20
|
+
fs.writeFileSync(mapFile, JSON.stringify(map, null, 2));
|
|
21
|
+
} catch (e) {}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = {
|
|
25
|
+
relative,
|
|
26
|
+
record(file, children) {
|
|
27
|
+
const map = read();
|
|
28
|
+
map[relative(file)] = children;
|
|
29
|
+
write(map);
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
drop(file) {
|
|
33
|
+
const map = read();
|
|
34
|
+
const key = relative(file);
|
|
35
|
+
if (!(key in map)) return;
|
|
36
|
+
delete map[key];
|
|
37
|
+
write(map);
|
|
38
|
+
},
|
|
39
|
+
};
|