bakit 2.0.0-alpha.3 → 2.0.0-alpha.30

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/dist/cli.js ADDED
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+ import { resolve, join } from 'path';
3
+ import { existsSync } from 'fs';
4
+ import { config } from 'dotenv';
5
+ import { program } from 'commander';
6
+ import { fork } from 'child_process';
7
+ import chokidar from 'chokidar';
8
+ import { loadConfig, RPC } from 'bakit';
9
+
10
+ var DevProcessManager = class {
11
+ constructor(options) {
12
+ this.options = options;
13
+ }
14
+ rpc = null;
15
+ restartTimer = null;
16
+ start() {
17
+ console.log("Starting bakit in dev mode..."), this.startChild(), this.startWatcher();
18
+ }
19
+ startChild() {
20
+ if (this.rpc)
21
+ return;
22
+ let { entryFile } = this.options, child = fork(this.options.entryFile, {
23
+ execArgv: ["--import", "bakit/register"],
24
+ stdio: "inherit",
25
+ env: {
26
+ ...process.env,
27
+ NODE_ENV: "development",
28
+ BAKIT_ENTRY_FILE: entryFile
29
+ }
30
+ });
31
+ this.rpc = new RPC(child), this.rpc.on("restart", (fileUpdated) => this.scheduleRestart(fileUpdated));
32
+ }
33
+ restartChild() {
34
+ if (!this.rpc)
35
+ return this.startChild();
36
+ let child = this.rpc.transport;
37
+ child.once("exit", () => {
38
+ this.rpc = null, this.startChild();
39
+ }), child.kill("SIGTERM");
40
+ }
41
+ startWatcher() {
42
+ let { entryDirectory } = this.options, watcher = chokidar.watch(entryDirectory, {
43
+ ignoreInitial: true,
44
+ awaitWriteFinish: {
45
+ stabilityThreshold: 200,
46
+ pollInterval: 50
47
+ }
48
+ });
49
+ watcher.on("change", (path) => this.onFileUpdate("fileChange", path)), watcher.on("unlink", (path) => this.onFileUpdate("fileRemove", path));
50
+ }
51
+ onFileUpdate(type, path) {
52
+ try {
53
+ this.rpc?.send(type, resolve(path));
54
+ } catch {
55
+ this.scheduleRestart(true);
56
+ }
57
+ }
58
+ scheduleRestart(fileUpdated = false) {
59
+ this.restartTimer && clearTimeout(this.restartTimer), this.restartTimer = setTimeout(() => {
60
+ fileUpdated && console.log("File changes detected, restarting..."), this.restartChild(), this.restartTimer = null;
61
+ }, 150);
62
+ }
63
+ };
64
+ program.name("bakit");
65
+ program.command("dev").action(async () => {
66
+ config({ path: [".env.local", ".env"], quiet: true });
67
+ let config$1 = await loadConfig(), entryDirectory = resolve(config$1.entryDirectory);
68
+ new DevProcessManager({
69
+ entryFile: getEntryFile(entryDirectory),
70
+ entryDirectory
71
+ }).start();
72
+ });
73
+ function getEntryFile(entryDirectory) {
74
+ let index = join(entryDirectory, "index");
75
+ return existsSync(index + ".ts") ? index : index + ".js";
76
+ }
77
+ program.parse();
package/dist/hooks.js ADDED
@@ -0,0 +1,82 @@
1
+ import { existsSync } from 'fs';
2
+ import { readFile } from 'fs/promises';
3
+ import { Module } from 'module';
4
+ import { dirname, resolve as resolve$1, basename } from 'path';
5
+ import { fileURLToPath, pathToFileURL } from 'url';
6
+ import { RPC } from 'bakit';
7
+
8
+ // src/lib/loader/hooks.ts
9
+ var EXTENSIONS = [".js", ".ts"], rpc, versions, esbuild;
10
+ function isDevelopment() {
11
+ return process.env.NODE_ENV === "development";
12
+ }
13
+ async function initialize({ port }) {
14
+ rpc = new RPC(port), versions = /* @__PURE__ */ new Map(), rpc.on("unload", onUnload), isDevelopment() && (esbuild = await import('esbuild'));
15
+ }
16
+ async function resolve(specifier, context, nextResolve) {
17
+ if (shouldSkip(specifier))
18
+ return nextResolve(specifier, context);
19
+ let url = specifier, parentURL = context.parentURL?.split("?")[0], baseDir = parentURL ? dirname(fileURLToPath(parentURL)) : process.cwd();
20
+ if (specifier.startsWith(".")) {
21
+ let absPath = resolve$1(baseDir, specifier);
22
+ if (!existsSync(absPath) && absPath.endsWith(".js")) {
23
+ let tsPath = absPath.slice(0, -3) + ".ts";
24
+ existsSync(tsPath) && (absPath = tsPath);
25
+ }
26
+ url = pathToFileURL(absPath).href;
27
+ }
28
+ let urlObj = new URL(url);
29
+ if (isDevelopment()) {
30
+ let filePath = fileURLToPath(urlObj), version = createVersion(filePath);
31
+ urlObj.searchParams.set("hmr", version), parentURL && rpc.send("dependencyAdd", {
32
+ parentURL,
33
+ url
34
+ });
35
+ }
36
+ return {
37
+ url: urlObj.href,
38
+ shortCircuit: true,
39
+ format: "module"
40
+ };
41
+ }
42
+ async function load(url, context, nextLoad) {
43
+ if (shouldSkip(url))
44
+ return nextLoad(url, context);
45
+ try {
46
+ let cleanURL = url.split("?")[0], filePath = fileURLToPath(cleanURL ?? "");
47
+ if (filePath.endsWith(".ts") && esbuild) {
48
+ let raw = await readFile(filePath, "utf8");
49
+ return {
50
+ source: (await esbuild.transform(raw, {
51
+ platform: "node",
52
+ sourcefile: filePath,
53
+ sourcemap: "inline",
54
+ loader: "ts"
55
+ })).code,
56
+ format: "module",
57
+ shortCircuit: !0
58
+ };
59
+ }
60
+ } catch {
61
+ }
62
+ return nextLoad(url, context);
63
+ }
64
+ function createVersion(filename) {
65
+ let version = versions.get(filename);
66
+ return version || (version = Date.now().toString(), versions.set(filename, version)), version;
67
+ }
68
+ function shouldSkip(specifier) {
69
+ if (Module.isBuiltin(specifier) || specifier.includes("/node_modules/"))
70
+ return true;
71
+ if (specifier.startsWith(".") || specifier.startsWith("file://")) {
72
+ let filePath = specifier.startsWith("file://") ? fileURLToPath(specifier) : specifier, filename = basename(filePath);
73
+ return !EXTENSIONS.some((ext) => filename.endsWith(ext));
74
+ }
75
+ return true;
76
+ }
77
+ function onUnload(id, path) {
78
+ let deleted = versions.delete(resolve$1(path));
79
+ rpc.success(id, deleted);
80
+ }
81
+
82
+ export { initialize, load, resolve };