olum-compiler 1.2.0 → 1.6.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/.claude/settings.local.json +3 -1
- package/bin/build.js +14 -8
- package/bin/bundle.js +108 -0
- package/bin/compiler.js +25 -7
- package/bin/copySrc.js +73 -25
- package/bin/dev.js +54 -34
- package/bin/index.js +6 -4
- package/bin/server.js +58 -34
- package/bin/watcher.js +15 -11
- package/bin/ws.js +16 -11
- package/lib/attr.js +1 -25
- package/lib/boolAttr.js +32 -15
- package/lib/changes.txt +0 -0
- package/lib/chokidar.js +52 -101
- package/lib/colors.js +1 -7
- package/lib/compPlaceholder.js +5 -3
- package/lib/compiler.js +11 -8
- package/lib/coreLibs.js +2 -7
- package/lib/cross-spawn.js +45 -59
- package/lib/cssParser.js +61 -37
- package/lib/domEvents.js +6 -2
- package/lib/events.js +75 -77
- package/lib/for.js +56 -28
- package/lib/getImports.js +35 -17
- package/lib/getTempPath.js +8 -4
- package/lib/helpers.js +289 -184
- package/lib/html.js +3 -10
- package/lib/htmlParser.js +45 -45
- package/lib/if.js +40 -20
- package/lib/importMap.js +49 -24
- package/lib/jsParser.js +8 -13
- package/lib/lineCounter.js +18 -5
- package/lib/liveProps.js +266 -0
- package/lib/log.js +30 -0
- package/lib/magic-string.js +45 -52
- package/lib/models.js +8 -7
- package/lib/parser.js +82 -43
- package/lib/placeholder.js +73 -58
- package/lib/regex.js +4 -3
- package/lib/show.js +16 -8
- package/lib/sourcemap.js +23 -24
- package/lib/styles.js +7 -13
- package/lib/textarea.js +3 -6
- package/lib/toJs.js +1 -1
- package/lib/tokens.js +10 -22
- package/lib/transition.js +86 -0
- package/lib/websocket.js +68 -62
- package/package.json +1 -1
- package/tests/compiler.test.js +450 -172
package/bin/build.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
const bundle = require("./bundle");
|
|
2
|
+
const compile = require("./compiler");
|
|
3
|
+
const copySrc = require("./copySrc");
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
(async function () {
|
|
6
|
+
const bootAt = Date.now();
|
|
7
|
+
try {
|
|
8
|
+
await copySrc();
|
|
9
|
+
await compile();
|
|
10
|
+
await bundle(bootAt);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.error(error);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
}
|
|
15
|
+
})();
|
package/bin/bundle.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const colors = require("../lib/colors");
|
|
4
|
+
const spawn = require("../lib/cross-spawn");
|
|
5
|
+
const { useLocalCore, localLibs } = require("../lib/coreLibs");
|
|
6
|
+
|
|
7
|
+
const cwd = process.cwd();
|
|
8
|
+
const public = path.resolve(__dirname, "../public");
|
|
9
|
+
const root = cwd.replace(/\/module$/, "");
|
|
10
|
+
const outDir = path.resolve(root, "dist");
|
|
11
|
+
|
|
12
|
+
function getLogs() {
|
|
13
|
+
const logFilePath = path.resolve(__dirname, "../lib/log.txt");
|
|
14
|
+
if (fs.existsSync(logFilePath)) {
|
|
15
|
+
return fs.readFileSync(logFilePath).toString();
|
|
16
|
+
} else {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function copyStatic(from, to, absorbed) {
|
|
22
|
+
for (const name of fs.readdirSync(from)) {
|
|
23
|
+
const src = path.join(from, name);
|
|
24
|
+
const dest = path.join(to, name);
|
|
25
|
+
if (fs.statSync(src).isDirectory()) {
|
|
26
|
+
copyStatic(src, dest, absorbed);
|
|
27
|
+
} else if (!absorbed.has(src) && !fs.existsSync(dest)) {
|
|
28
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
29
|
+
fs.copyFileSync(src, dest);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function absorbedFiles(buildResult) {
|
|
35
|
+
const absorbed = new Set();
|
|
36
|
+
for (const res of [buildResult].flat()) {
|
|
37
|
+
for (const item of res.output || []) {
|
|
38
|
+
for (const orig of item.originalFileNames || [])
|
|
39
|
+
absorbed.add(path.resolve(public, orig));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const html = fs.readFileSync(path.join(public, "index.html"), "utf8");
|
|
44
|
+
const attrRegex = /(?:href|src)=["']([^"']+)["']/g;
|
|
45
|
+
let match;
|
|
46
|
+
while ((match = attrRegex.exec(html))) {
|
|
47
|
+
const url = match[1];
|
|
48
|
+
if (/^(https?:)?\/\//.test(url)) continue;
|
|
49
|
+
absorbed.add(path.resolve(public, url.replace(/^\//, "")));
|
|
50
|
+
}
|
|
51
|
+
return absorbed;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ensureVite() {
|
|
55
|
+
if (fs.existsSync(path.join(root, "node_modules/vite/package.json"))) return;
|
|
56
|
+
console.log(
|
|
57
|
+
colors(
|
|
58
|
+
"white",
|
|
59
|
+
"vite not found — installing it into the project root (one-time)...",
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const result = spawn.sync(
|
|
64
|
+
"npm",
|
|
65
|
+
["install", "--save-dev", "--include=dev", "vite"],
|
|
66
|
+
{ cwd: root, stdio: "inherit" },
|
|
67
|
+
);
|
|
68
|
+
if (result.status !== 0)
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Failed to install vite — run `npm i -D vite` in " +
|
|
71
|
+
root +
|
|
72
|
+
" then re-run the build.",
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function bundle(bootAt) {
|
|
77
|
+
const logs = getLogs();
|
|
78
|
+
if (logs) {
|
|
79
|
+
console.error(
|
|
80
|
+
colors("red", "Bundle skipped — fix the compiler errors above first."),
|
|
81
|
+
);
|
|
82
|
+
process.exitCode = 1;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
ensureVite();
|
|
87
|
+
const { build } = await import("vite");
|
|
88
|
+
|
|
89
|
+
const alias = {};
|
|
90
|
+
if (useLocalCore) {
|
|
91
|
+
for (const [pkgName, corePath] of Object.entries(localLibs)) {
|
|
92
|
+
alias[pkgName] = path.resolve(__dirname, ".." + corePath);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const buildResult = await build({
|
|
97
|
+
root: public,
|
|
98
|
+
resolve: { alias },
|
|
99
|
+
build: {
|
|
100
|
+
outDir,
|
|
101
|
+
emptyOutDir: true,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
copyStatic(public, outDir, absorbedFiles(buildResult));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = bundle;
|
package/bin/compiler.js
CHANGED
|
@@ -3,21 +3,39 @@ const path = require("path");
|
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const colors = require("../lib/colors");
|
|
5
5
|
|
|
6
|
-
let num =
|
|
7
|
-
module.exports = function () {
|
|
6
|
+
let num = -1;
|
|
7
|
+
module.exports = function (file, startedAt) {
|
|
8
8
|
num++;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const t0 = startedAt || Date.now();
|
|
10
|
+
const done = () =>
|
|
11
|
+
num &&
|
|
12
|
+
console.log(
|
|
13
|
+
colors("white", "Build no."),
|
|
14
|
+
colors("yellow", num),
|
|
15
|
+
colors("white", "in"),
|
|
16
|
+
colors("green", Date.now() - t0 + "ms"),
|
|
17
|
+
);
|
|
18
|
+
return new Promise((resolve) => {
|
|
11
19
|
const logFilePath = path.resolve(__dirname, "../lib/log.txt");
|
|
12
20
|
fs.writeFileSync(logFilePath, "");
|
|
13
|
-
|
|
21
|
+
|
|
14
22
|
const compilerPath = path.resolve(__dirname, "../lib/compiler.js");
|
|
23
|
+
if (file) {
|
|
24
|
+
try {
|
|
25
|
+
require(compilerPath)(file);
|
|
26
|
+
} catch (err) {
|
|
27
|
+
console.error(err);
|
|
28
|
+
}
|
|
29
|
+
resolve();
|
|
30
|
+
return done();
|
|
31
|
+
}
|
|
15
32
|
const result = spawn.sync("node", [compilerPath], { stdio: "inherit" });
|
|
16
33
|
if (result.signal) {
|
|
17
|
-
if (result.signal === "SIGKILL" || result.signal === "SIGTERM")
|
|
34
|
+
if (result.signal === "SIGKILL" || result.signal === "SIGTERM")
|
|
35
|
+
console.log("Failed to run compiler @ " + compilerPath);
|
|
18
36
|
process.exit(1);
|
|
19
37
|
}
|
|
20
38
|
resolve();
|
|
21
|
-
|
|
39
|
+
done();
|
|
22
40
|
});
|
|
23
41
|
};
|
package/bin/copySrc.js
CHANGED
|
@@ -1,35 +1,83 @@
|
|
|
1
|
-
// todo check changed files only to be copied, use this project https://github.com/eissapk/diff
|
|
2
1
|
const path = require("path");
|
|
3
2
|
const fs = require("fs");
|
|
4
|
-
const { copy,
|
|
3
|
+
const { copy, compileRoutes } = require("../lib/helpers");
|
|
5
4
|
const importMap = require("../lib/importMap");
|
|
6
|
-
const entryPoint = path.resolve(process.cwd(), "src").replace(/\/module/, "");
|
|
7
|
-
const entryPoint2 = path.resolve(process.cwd(), "public").replace(/\/module/, "");
|
|
8
5
|
|
|
9
|
-
|
|
6
|
+
const cwd = process.cwd();
|
|
7
|
+
const entryPoint = path.resolve(cwd.replace(/\/module$/, ""), "src");
|
|
8
|
+
const entryPoint2 = path.resolve(cwd.replace(/\/module$/, ""), "public");
|
|
9
|
+
|
|
10
|
+
const srcDest = path.resolve(__dirname, "../src");
|
|
11
|
+
const publicDest = path.resolve(__dirname, "../public");
|
|
12
|
+
const indexHtmlDest = path.resolve(__dirname, "../public/index.html");
|
|
13
|
+
const mainJsPathDest = path.resolve(__dirname, "../src/main.js");
|
|
14
|
+
|
|
15
|
+
function injectIndexHtml(indexHtmlPath) {
|
|
16
|
+
let indexHtmlContent = fs.readFileSync(indexHtmlPath).toString();
|
|
17
|
+
|
|
18
|
+
const importMapTag =
|
|
19
|
+
process.env.NODE_ENV === "production" ? "" : `${importMap(entryPoint)}\n`;
|
|
20
|
+
indexHtmlContent = indexHtmlContent.replace(
|
|
21
|
+
/<\/body>/,
|
|
22
|
+
`\n<div id="app"></div>\n${importMapTag}<script defer type="module" src="../src/main.js"></script>\n</body>`,
|
|
23
|
+
);
|
|
24
|
+
fs.writeFileSync(indexHtmlPath, indexHtmlContent);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function handleMainJs() {
|
|
28
|
+
if (fs.existsSync(mainJsPathDest)) return;
|
|
29
|
+
const mainJsContent = compileRoutes(entryPoint);
|
|
30
|
+
|
|
31
|
+
fs.writeFileSync(mainJsPathDest, mainJsContent);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function syncFile(event, file) {
|
|
35
|
+
if (!fs.existsSync(mainJsPathDest) || !fs.existsSync(indexHtmlDest))
|
|
36
|
+
return null;
|
|
37
|
+
const inSrc = file.startsWith(entryPoint + path.sep);
|
|
38
|
+
const inPublic = !inSrc && file.startsWith(entryPoint2 + path.sep);
|
|
39
|
+
if (!inSrc && !inPublic) return null;
|
|
40
|
+
const dest = inSrc
|
|
41
|
+
? path.join(srcDest, path.relative(entryPoint, file))
|
|
42
|
+
: path.join(publicDest, path.relative(entryPoint2, file));
|
|
43
|
+
if (event.startsWith("unlink")) {
|
|
44
|
+
fs.rmSync(dest, { recursive: true, force: true });
|
|
45
|
+
if (inSrc && dest.endsWith(".html"))
|
|
46
|
+
fs.rmSync(dest.replace(/\.html$/, ".js"), { force: true });
|
|
47
|
+
} else {
|
|
48
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
49
|
+
fs.copyFileSync(file, dest);
|
|
50
|
+
if (dest === indexHtmlDest) injectIndexHtml(dest);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const affectsRoutes =
|
|
54
|
+
inSrc &&
|
|
55
|
+
event !== "change" &&
|
|
56
|
+
(dest.endsWith(".html") ||
|
|
57
|
+
event === "unlinkDir" ||
|
|
58
|
+
dest === mainJsPathDest);
|
|
59
|
+
if (affectsRoutes && !fs.existsSync(path.join(entryPoint, "main.js")))
|
|
60
|
+
fs.writeFileSync(mainJsPathDest, compileRoutes(entryPoint));
|
|
61
|
+
return dest;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = function copySrc(event, file) {
|
|
10
65
|
return new Promise((resolve, reject) => {
|
|
11
66
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
let indexHtmlContent = fs.readFileSync(indexHtmlPath).toString();
|
|
25
|
-
// auto bind div#app placeholder, import map and mainjs script to index.html
|
|
26
|
-
// (import map must precede the module script so bare imports resolve)
|
|
27
|
-
indexHtmlContent = indexHtmlContent.replace(/<\/body>/, `\n<div id="app"></div>\n${importMap(entryPoint)}\n<script defer type="module" src="../src/main.js"></script>\n</body>`);
|
|
28
|
-
fs.writeFileSync(indexHtmlPath, indexHtmlContent);
|
|
29
|
-
});
|
|
30
|
-
resolve();
|
|
67
|
+
if (event) {
|
|
68
|
+
const dest = syncFile(event, file);
|
|
69
|
+
if (dest) return resolve(dest);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fs.rmSync(publicDest, { recursive: true, force: true });
|
|
73
|
+
fs.rmSync(srcDest, { recursive: true, force: true });
|
|
74
|
+
|
|
75
|
+
copy(entryPoint, srcDest, () => handleMainJs());
|
|
76
|
+
copy(entryPoint2, publicDest, () => injectIndexHtml(indexHtmlDest));
|
|
77
|
+
|
|
78
|
+
resolve(null);
|
|
31
79
|
} catch (err) {
|
|
32
|
-
reject();
|
|
80
|
+
reject(err);
|
|
33
81
|
}
|
|
34
82
|
});
|
|
35
83
|
};
|
package/bin/dev.js
CHANGED
|
@@ -3,38 +3,46 @@ const watch = require("./watcher");
|
|
|
3
3
|
const compile = require("./compiler");
|
|
4
4
|
const copySrc = require("./copySrc");
|
|
5
5
|
const WebSocket = require("../lib/websocket");
|
|
6
|
+
const colors = require("../lib/colors");
|
|
6
7
|
const path = require("path");
|
|
7
8
|
const fs = require("fs");
|
|
8
9
|
|
|
9
10
|
let instance;
|
|
10
11
|
const entryPoint = path.resolve(__dirname, "../public");
|
|
11
12
|
serve(entryPoint).then(({ server, wsPort, PORT, log, setWsPort }) => {
|
|
12
|
-
|
|
13
|
-
copySrc()
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// Listen for adds, edits and deletes. Editors that save atomically
|
|
20
|
-
// (temp file + rename) surface edits as unlink+add rather than change,
|
|
21
|
-
// so watching only "change" misses new files and many edits.
|
|
22
|
-
watch.on("add", handleChange);
|
|
23
|
-
watch.on("change", handleChange);
|
|
24
|
-
watch.on("unlink", handleChange);
|
|
25
|
-
|
|
26
|
-
(function startWs(port) {
|
|
27
|
-
// const wss = new WebSocket.Server({ port });
|
|
28
|
-
const wss = new WebSocket.Server({ server }); // using the same server port
|
|
29
|
-
wss.on("listening", () => setWsPort(port)); // tell server.js the real ws port to inject
|
|
30
|
-
wss.on("error", (e) => { if (e.code === "EADDRINUSE") startWs(port + 1); });
|
|
31
|
-
wss.on("connection", (ws) => {
|
|
32
|
-
instance = ws;
|
|
33
|
-
ws.send(JSON.stringify({ msg: "connected" }));
|
|
13
|
+
const bootAt = Date.now();
|
|
14
|
+
copySrc()
|
|
15
|
+
.then(() => {
|
|
16
|
+
compile(undefined, bootAt).then(() => {
|
|
17
|
+
server.on("error", (e) => {
|
|
18
|
+
if (e.code === "EADDRINUSE") server.listen(++PORT);
|
|
34
19
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
server.listen(PORT, () =>
|
|
21
|
+
console.log(
|
|
22
|
+
log.replace(/port \d+/, "port " + PORT),
|
|
23
|
+
colors("white", `(ready in ${Date.now() - bootAt}ms)`),
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
["add", "change", "unlink", "unlinkDir"].forEach((event) =>
|
|
28
|
+
watch.on(event, (file) => handleChange(event, file)),
|
|
29
|
+
);
|
|
30
|
+
require("../lib/compiler");
|
|
31
|
+
|
|
32
|
+
(function startWs(port) {
|
|
33
|
+
const wss = new WebSocket.Server({ server });
|
|
34
|
+
wss.on("listening", () => setWsPort(port));
|
|
35
|
+
wss.on("error", (e) => {
|
|
36
|
+
if (e.code === "EADDRINUSE") startWs(port + 1);
|
|
37
|
+
});
|
|
38
|
+
wss.on("connection", (ws) => {
|
|
39
|
+
instance = ws;
|
|
40
|
+
ws.send(JSON.stringify({ msg: "connected" }));
|
|
41
|
+
});
|
|
42
|
+
})(wsPort);
|
|
43
|
+
});
|
|
44
|
+
})
|
|
45
|
+
.catch(console.error);
|
|
38
46
|
});
|
|
39
47
|
|
|
40
48
|
function getLogs() {
|
|
@@ -46,15 +54,27 @@ function getLogs() {
|
|
|
46
54
|
}
|
|
47
55
|
}
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
const send = (data) => instance && instance.send(JSON.stringify(data));
|
|
58
|
+
|
|
59
|
+
function notify() {
|
|
60
|
+
const logs = getLogs();
|
|
61
|
+
logs ? send({ msg: "warn", data: logs }) : send({ msg: "reload" });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const isComp = (dest) =>
|
|
65
|
+
dest.endsWith(".html") &&
|
|
66
|
+
!path.basename(dest).startsWith("_") &&
|
|
67
|
+
dest.startsWith(path.resolve(__dirname, "../src") + path.sep);
|
|
68
|
+
|
|
69
|
+
function handleChange(event, file) {
|
|
70
|
+
const t0 = Date.now();
|
|
71
|
+
copySrc(event, file)
|
|
72
|
+
.then((dest) => {
|
|
73
|
+
if (dest === null) return compile(undefined, t0).then(notify);
|
|
74
|
+
if (file.endsWith("css")) return send({ msg: "css" });
|
|
75
|
+
if (!event.startsWith("unlink") && isComp(dest))
|
|
76
|
+
return compile(dest, t0).then(notify);
|
|
77
|
+
send({ msg: "reload" });
|
|
58
78
|
})
|
|
59
79
|
.catch(console.error);
|
|
60
80
|
}
|
package/bin/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
const path = require("path");
|
|
3
2
|
const spawn = require("../lib/cross-spawn");
|
|
4
3
|
const script = process.argv[2];
|
|
@@ -7,12 +6,15 @@ if (script == "dev") process.env.NODE_ENV = "development";
|
|
|
7
6
|
else if (script == "build") process.env.NODE_ENV = "production";
|
|
8
7
|
|
|
9
8
|
if (typeof script !== undefined && ["dev", "build"].includes(script)) {
|
|
10
|
-
const result = spawn.sync("node", [path.resolve(__dirname, script + ".js")], {
|
|
9
|
+
const result = spawn.sync("node", [path.resolve(__dirname, script + ".js")], {
|
|
10
|
+
stdio: "inherit",
|
|
11
|
+
});
|
|
11
12
|
|
|
12
13
|
if (result.signal) {
|
|
13
|
-
if (result.signal === "SIGKILL" || result.signal === "SIGTERM")
|
|
14
|
+
if (result.signal === "SIGKILL" || result.signal === "SIGTERM")
|
|
15
|
+
console.log("Failed to run " + script + " script!");
|
|
14
16
|
process.exit(1);
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
process.exit(result.status);
|
|
18
|
-
}
|
|
20
|
+
}
|
package/bin/server.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const http = require("http");
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const jsdom = require("jsdom");
|
|
6
6
|
const colors = require("../lib/colors");
|
|
7
7
|
const mimes = require("./mimes.json");
|
|
8
|
-
let wsPort = 8090;
|
|
8
|
+
let wsPort = 8090;
|
|
9
9
|
|
|
10
10
|
function handleWsFile() {
|
|
11
11
|
let file = fs.readFileSync(path.resolve(__dirname, "./ws.js")).toString();
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
file = "<script>" + file + "</script>";
|
|
14
14
|
return file;
|
|
15
15
|
}
|
|
@@ -18,22 +18,17 @@ function getPath(entry, url) {
|
|
|
18
18
|
entry = entry.endsWith("/") ? entry : entry + "/";
|
|
19
19
|
url = url.startsWith("/") ? url.slice(1) : url;
|
|
20
20
|
|
|
21
|
-
const resolve = candidate =>
|
|
21
|
+
const resolve = (candidate) =>
|
|
22
22
|
[
|
|
23
23
|
path.resolve(entry, candidate),
|
|
24
24
|
path.resolve(entry, "../", candidate),
|
|
25
25
|
path.resolve(process.cwd(), candidate),
|
|
26
|
-
path.resolve(entry, "../../", candidate),
|
|
27
|
-
].find(p => fs.existsSync(p) && fs.statSync(p).isFile()) || null;
|
|
26
|
+
path.resolve(entry, "../../", candidate),
|
|
27
|
+
].find((p) => fs.existsSync(p) && fs.statSync(p).isFile()) || null;
|
|
28
28
|
|
|
29
|
-
// Exact match first (preserves the original behaviour).
|
|
30
29
|
const direct = resolve(url);
|
|
31
30
|
if (direct) return direct;
|
|
32
31
|
|
|
33
|
-
// Assets are referenced relatively in index.html (e.g. "./main.css"), so when
|
|
34
|
-
// a nested client-side route like /blog/x is open the browser requests them as
|
|
35
|
-
// /blog/main.css. Progressively drop leading route segments so the asset still
|
|
36
|
-
// resolves to its real location (/blog/main.css -> main.css).
|
|
37
32
|
if (path.extname(url)) {
|
|
38
33
|
const segments = url.split("/");
|
|
39
34
|
for (let i = 1; i < segments.length; i++) {
|
|
@@ -44,22 +39,15 @@ function getPath(entry, url) {
|
|
|
44
39
|
|
|
45
40
|
return null;
|
|
46
41
|
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
*
|
|
50
|
-
* @param {*} entry must be an absolute path
|
|
51
|
-
* @param {*} port optional port, default is 8000
|
|
52
|
-
*/
|
|
42
|
+
|
|
53
43
|
function serve(entry, port) {
|
|
54
44
|
return new Promise((resolve, reject) => {
|
|
55
45
|
const PORT = Number(port || process.env.PORT || 3000);
|
|
56
|
-
const domain = `http://localhost:${PORT}`;
|
|
57
46
|
|
|
58
47
|
const server = http.createServer(handler);
|
|
59
48
|
function handler(req, res) {
|
|
60
|
-
const urlPath = req.url.split("?")[0];
|
|
49
|
+
const urlPath = req.url.split("?")[0];
|
|
61
50
|
|
|
62
|
-
// serve the (ws-injected) index.html — used for "/" and as the history-mode SPA fallback
|
|
63
51
|
function serveIndex() {
|
|
64
52
|
let indexPath = entry + "/index.html";
|
|
65
53
|
if (!fs.existsSync(indexPath)) indexPath = entry + "/index.htm";
|
|
@@ -74,38 +62,74 @@ function serve(entry, port) {
|
|
|
74
62
|
const html = dom.querySelector("html");
|
|
75
63
|
const wsContent = handleWsFile();
|
|
76
64
|
dom.body.insertAdjacentHTML("beforeend", wsContent);
|
|
77
|
-
res.writeHead(200, {
|
|
65
|
+
res.writeHead(200, {
|
|
66
|
+
"content-type": "text/html",
|
|
67
|
+
"cache-control": "no-cache",
|
|
68
|
+
});
|
|
78
69
|
res.end("<!DOCTYPE html>" + html.outerHTML);
|
|
79
70
|
});
|
|
80
71
|
}
|
|
81
72
|
|
|
82
73
|
if (urlPath === "/") return serveIndex();
|
|
83
74
|
|
|
84
|
-
// serve an existing static file with its mime type
|
|
85
75
|
const finalPath = getPath(entry, urlPath);
|
|
86
76
|
if (finalPath && fs.statSync(finalPath).isFile()) {
|
|
87
77
|
const ext = path.extname(finalPath).toLowerCase();
|
|
88
|
-
const obj = mimes.find(item => ext === item.ext.toLowerCase());
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
78
|
+
const obj = mimes.find((item) => ext === item.ext.toLowerCase());
|
|
79
|
+
const mime = obj ? obj.mime : "application/octet-stream";
|
|
80
|
+
const size = fs.statSync(finalPath).size;
|
|
81
|
+
|
|
82
|
+
const range = /^bytes=(\d*)-(\d*)$/.exec(req.headers.range || "");
|
|
83
|
+
if (range && (range[1] || range[2])) {
|
|
84
|
+
const start = range[1]
|
|
85
|
+
? parseInt(range[1], 10)
|
|
86
|
+
: size - parseInt(range[2], 10);
|
|
87
|
+
const end =
|
|
88
|
+
range[1] && range[2]
|
|
89
|
+
? Math.min(parseInt(range[2], 10), size - 1)
|
|
90
|
+
: size - 1;
|
|
91
|
+
if (start >= size || start < 0 || start > end) {
|
|
92
|
+
res.writeHead(416, { "Content-Range": "bytes */" + size });
|
|
93
|
+
return res.end();
|
|
94
|
+
}
|
|
95
|
+
res.writeHead(206, {
|
|
96
|
+
"Content-Type": mime,
|
|
97
|
+
"Content-Range": "bytes " + start + "-" + end + "/" + size,
|
|
98
|
+
"Content-Length": end - start + 1,
|
|
99
|
+
"Accept-Ranges": "bytes",
|
|
100
|
+
"Cache-Control": "no-cache",
|
|
101
|
+
});
|
|
102
|
+
fs.createReadStream(finalPath, { start, end }).pipe(res);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
res.writeHead(200, {
|
|
107
|
+
"Content-Type": mime,
|
|
108
|
+
"Content-Length": size,
|
|
109
|
+
"Accept-Ranges": "bytes",
|
|
110
|
+
"Cache-Control": "no-cache",
|
|
111
|
+
});
|
|
92
112
|
fs.createReadStream(finalPath).pipe(res);
|
|
93
113
|
return;
|
|
94
114
|
}
|
|
95
115
|
|
|
96
|
-
// history-mode SPA fallback: a request with no file extension is a client-side route
|
|
97
|
-
// (e.g. /card) — serve index.html and let the olum router handle it. Anything with an
|
|
98
|
-
// extension is a genuinely missing asset, so return a real 404 instead of hanging.
|
|
99
116
|
if (!path.extname(urlPath)) return serveIndex();
|
|
100
117
|
|
|
101
118
|
res.writeHead(404, { "content-type": "text/plain" });
|
|
102
119
|
res.end("404 Not Found: " + urlPath);
|
|
103
120
|
}
|
|
104
|
-
const log = colors("cyan", "Serving ") + colors("green",
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
121
|
+
const log = colors("cyan", "Serving ") + colors("green", "port " + PORT);
|
|
122
|
+
|
|
123
|
+
const setWsPort = (port) => {
|
|
124
|
+
wsPort = port;
|
|
125
|
+
};
|
|
126
|
+
resolve({
|
|
127
|
+
server: server,
|
|
128
|
+
wsPort: wsPort,
|
|
129
|
+
PORT: PORT,
|
|
130
|
+
log: log,
|
|
131
|
+
setWsPort: setWsPort,
|
|
132
|
+
});
|
|
109
133
|
});
|
|
110
134
|
}
|
|
111
135
|
|
package/bin/watcher.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
const chokidar = require("../lib/chokidar");
|
|
2
2
|
const path = require("path");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const entryPoint = path.resolve(
|
|
7
|
-
const entryPoint2 = path.resolve(
|
|
8
|
-
|
|
9
|
-
// module.exports = chokidar.watch(entryPoint, {ignored: ignoreRegex, persistent: true });
|
|
10
|
-
// ignoreInitial: don't fire "add" for every existing file on startup (initial
|
|
11
|
-
// compile is handled separately in dev.js). awaitWriteFinish: coalesce the
|
|
12
|
-
// temp-file/rename churn from atomic saves into a single event.
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
|
|
5
|
+
const cwd = process.cwd();
|
|
6
|
+
const entryPoint = path.resolve(cwd.replace(/\/module$/, ""), "src");
|
|
7
|
+
const entryPoint2 = path.resolve(cwd.replace(/\/module$/, ""), "public");
|
|
8
|
+
|
|
13
9
|
module.exports = chokidar.watch([entryPoint, entryPoint2], {
|
|
14
10
|
persistent: true,
|
|
15
11
|
ignoreInitial: true,
|
|
16
12
|
awaitWriteFinish: { stabilityThreshold: 150, pollInterval: 50 },
|
|
17
|
-
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const changesLog = path.resolve(__dirname, "../lib/changes.txt");
|
|
16
|
+
fs.writeFileSync(changesLog, "");
|
|
17
|
+
["add", "change", "unlink", "addDir", "unlinkDir"].forEach((event) =>
|
|
18
|
+
module.exports.on(event, (file) =>
|
|
19
|
+
fs.appendFileSync(changesLog, `${event} ${file}\n`),
|
|
20
|
+
),
|
|
21
|
+
);
|