astrobit 0.1.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/dist/client.d.ts +3 -0
- package/dist/client.js +14 -0
- package/dist/integration.d.ts +5 -0
- package/dist/integration.js +133 -0
- package/dist/server.d.ts +9 -0
- package/dist/server.js +13 -0
- package/env.d.ts +11 -0
- package/package.json +40 -0
package/dist/client.d.ts
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// client.ts
|
|
2
|
+
var client_default = (element) => {
|
|
3
|
+
return async (Component, props) => {
|
|
4
|
+
if (element.innerHTML.trim() && typeof Component.hydrate === "function") {
|
|
5
|
+
Component.hydrate(element, props);
|
|
6
|
+
} else {
|
|
7
|
+
element.innerHTML = "";
|
|
8
|
+
Component.mount(element, props);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export {
|
|
13
|
+
client_default as default
|
|
14
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// vite-plugin.ts
|
|
2
|
+
import { spawn } from "child_process";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
var VIRTUAL_PREFIX = "virtual:moonbit:";
|
|
6
|
+
var STUB_MODULE = `export default { __moonbit: true, mount: () => {}, render: () => '', hydrate: () => {} }`;
|
|
7
|
+
function findWorkspaceRoot(startDir) {
|
|
8
|
+
let dir = startDir;
|
|
9
|
+
while (dir !== path.dirname(dir)) {
|
|
10
|
+
if (fs.existsSync(path.join(dir, "moon.mod.json"))) return dir;
|
|
11
|
+
dir = path.dirname(dir);
|
|
12
|
+
}
|
|
13
|
+
return startDir;
|
|
14
|
+
}
|
|
15
|
+
function computeBuiltJsPath(mbtFsPath, workspaceRoot) {
|
|
16
|
+
const pkgDir = path.dirname(mbtFsPath);
|
|
17
|
+
const pkgName = path.basename(pkgDir);
|
|
18
|
+
const relDir = path.relative(workspaceRoot, pkgDir);
|
|
19
|
+
return path.join(workspaceRoot, "_build", "js", "debug", "build", relDir, `${pkgName}.js`);
|
|
20
|
+
}
|
|
21
|
+
function toFsPath(id, importer, projectRoot) {
|
|
22
|
+
if (id.startsWith(".") && importer && !importer.startsWith("\0")) {
|
|
23
|
+
return path.resolve(path.dirname(importer), id);
|
|
24
|
+
}
|
|
25
|
+
if (path.isAbsolute(id) && fs.existsSync(id)) return id;
|
|
26
|
+
const rel = id.startsWith("/") ? id.slice(1) : id;
|
|
27
|
+
return path.join(projectRoot, rel);
|
|
28
|
+
}
|
|
29
|
+
function loadMbt(mbtFsPath, workspaceRoot) {
|
|
30
|
+
const builtJs = computeBuiltJsPath(mbtFsPath, workspaceRoot);
|
|
31
|
+
if (!fs.existsSync(builtJs)) {
|
|
32
|
+
console.warn(`[astrobit] Build output not found for ${mbtFsPath}. Run moon build first.`);
|
|
33
|
+
return STUB_MODULE;
|
|
34
|
+
}
|
|
35
|
+
return [
|
|
36
|
+
`import * as moonbit from ${JSON.stringify(builtJs)}`,
|
|
37
|
+
`export default {`,
|
|
38
|
+
` __moonbit: true,`,
|
|
39
|
+
` ...(moonbit.mount ? { mount: (el, props) => moonbit.mount(el, props) } : {}),`,
|
|
40
|
+
` ...(moonbit.render ? { render: (props) => moonbit.render(props) } : {}),`,
|
|
41
|
+
` ...(moonbit.hydrate ? { hydrate: (el, props) => moonbit.hydrate(el, props) } : {}),`,
|
|
42
|
+
`}`
|
|
43
|
+
].join("\n");
|
|
44
|
+
}
|
|
45
|
+
function moonBuild(cwd) {
|
|
46
|
+
return new Promise((resolve2) => {
|
|
47
|
+
const proc = spawn("moon", ["build"], { cwd, stdio: "inherit" });
|
|
48
|
+
proc.on("close", () => resolve2());
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function moonbitVitePlugin() {
|
|
52
|
+
let workspaceRoot = "";
|
|
53
|
+
let projectRoot = "";
|
|
54
|
+
return {
|
|
55
|
+
name: "vite-plugin-moonbit",
|
|
56
|
+
enforce: "pre",
|
|
57
|
+
configResolved(config) {
|
|
58
|
+
projectRoot = config.root;
|
|
59
|
+
workspaceRoot = findWorkspaceRoot(config.root);
|
|
60
|
+
},
|
|
61
|
+
resolveId(id, importer) {
|
|
62
|
+
if (id.startsWith(VIRTUAL_PREFIX)) return id;
|
|
63
|
+
if (!id.endsWith(".mbt")) return;
|
|
64
|
+
const fsPath = toFsPath(id, importer, projectRoot);
|
|
65
|
+
return VIRTUAL_PREFIX + fsPath;
|
|
66
|
+
},
|
|
67
|
+
load(id) {
|
|
68
|
+
if (id.startsWith(VIRTUAL_PREFIX)) {
|
|
69
|
+
const mbtFsPath = id.slice(VIRTUAL_PREFIX.length);
|
|
70
|
+
return loadMbt(mbtFsPath, workspaceRoot);
|
|
71
|
+
}
|
|
72
|
+
const cleanId = id.split("?")[0];
|
|
73
|
+
if (cleanId.endsWith(".mbt")) {
|
|
74
|
+
const mbtFsPath = toFsPath(cleanId, void 0, projectRoot);
|
|
75
|
+
return loadMbt(mbtFsPath, workspaceRoot);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
transform(_code, id) {
|
|
79
|
+
if (id.startsWith(VIRTUAL_PREFIX)) return;
|
|
80
|
+
const cleanId = id.split("?")[0];
|
|
81
|
+
if (!cleanId.endsWith(".mbt")) return;
|
|
82
|
+
const mbtFsPath = toFsPath(cleanId, void 0, projectRoot);
|
|
83
|
+
return { code: loadMbt(mbtFsPath, workspaceRoot), map: null };
|
|
84
|
+
},
|
|
85
|
+
async handleHotUpdate({ file, server }) {
|
|
86
|
+
if (!file.endsWith(".mbt")) return;
|
|
87
|
+
await moonBuild(workspaceRoot);
|
|
88
|
+
for (const [, mod] of server.moduleGraph.idToModuleMap) {
|
|
89
|
+
if (mod.id?.startsWith(VIRTUAL_PREFIX)) {
|
|
90
|
+
server.moduleGraph.invalidateModule(mod);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
server.ws.send({ type: "full-reload" });
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// integration.ts
|
|
100
|
+
function astrobit() {
|
|
101
|
+
return {
|
|
102
|
+
name: "astrobit",
|
|
103
|
+
hooks: {
|
|
104
|
+
"astro:config:setup": ({ addRenderer, updateConfig }) => {
|
|
105
|
+
addRenderer({
|
|
106
|
+
name: "astrobit",
|
|
107
|
+
clientEntrypoint: new URL("./client.js", import.meta.url).href,
|
|
108
|
+
serverEntrypoint: new URL("./server.js", import.meta.url).href
|
|
109
|
+
});
|
|
110
|
+
updateConfig({ vite: { plugins: [moonbitVitePlugin()] } });
|
|
111
|
+
},
|
|
112
|
+
"astro:config:done": ({ injectTypes }) => {
|
|
113
|
+
injectTypes({
|
|
114
|
+
filename: "astrobit.d.ts",
|
|
115
|
+
content: `declare module '*.mbt' {
|
|
116
|
+
interface MoonBitComponent {
|
|
117
|
+
(props: Record<string, unknown>): unknown
|
|
118
|
+
__moonbit: true
|
|
119
|
+
mount?: (element: Element, props: Record<string, unknown>) => void
|
|
120
|
+
render?: (props: Record<string, unknown>) => string
|
|
121
|
+
hydrate?: (element: Element, props: Record<string, unknown>) => void
|
|
122
|
+
}
|
|
123
|
+
const component: MoonBitComponent
|
|
124
|
+
export default component
|
|
125
|
+
}`
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
export {
|
|
132
|
+
astrobit as default
|
|
133
|
+
};
|
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// server.ts
|
|
2
|
+
var server_default = {
|
|
3
|
+
name: "astrobit",
|
|
4
|
+
check(Component) {
|
|
5
|
+
return Component?.__moonbit === true;
|
|
6
|
+
},
|
|
7
|
+
async renderToStaticMarkup(Component, props) {
|
|
8
|
+
return { html: Component.render?.(props) ?? "<div></div>" };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
server_default as default
|
|
13
|
+
};
|
package/env.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare module '*.mbt' {
|
|
2
|
+
interface MoonBitComponent {
|
|
3
|
+
(props: Record<string, unknown>): unknown
|
|
4
|
+
__moonbit: true
|
|
5
|
+
mount?: (element: Element, props: Record<string, unknown>) => void
|
|
6
|
+
render?: (props: Record<string, unknown>) => string
|
|
7
|
+
hydrate?: (element: Element, props: Record<string, unknown>) => void
|
|
8
|
+
}
|
|
9
|
+
const component: MoonBitComponent
|
|
10
|
+
export default component
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astrobit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/integration.js",
|
|
6
|
+
"types": "./dist/integration.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/integration.js",
|
|
10
|
+
"types": "./dist/integration.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./env": {
|
|
13
|
+
"types": "./env.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"env.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"astro": "^6.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
29
|
+
"astro": "^6.0.0",
|
|
30
|
+
"tsup": "^8.5.1",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
},
|
|
33
|
+
"description": "A thin framework for running MoonBit components as Astro islands",
|
|
34
|
+
"keywords": ["astro", "moonbit", "island", "framework", "astro-integration"],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/SouichiroTsujimoto/astrobit"
|
|
38
|
+
},
|
|
39
|
+
"license": "MIT"
|
|
40
|
+
}
|