@sixb/app 0.1.5 → 0.1.6
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/README.md +21 -4
- package/dist/build.d.ts +19 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/codegen.d.ts +8 -2
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/dev-reload.d.ts +3 -0
- package/dist/dev-reload.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +873 -106
- package/dist/index.js.map +8 -7
- package/package.json +4 -4
- package/src/build.ts +185 -44
- package/src/codegen.ts +530 -63
- package/src/createCustomApp.ts +292 -23
- package/src/dev-reload.ts +54 -0
- package/src/index.ts +1 -0
- package/src/scanner.ts +8 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Custom app toolkit for Sixb projects — route scanning, dev serving, and production builds",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sixb",
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "bun ../../scripts/build-package.ts",
|
|
46
|
-
"test:e2e": "bun test ./tests/build-browser.e2e.ts",
|
|
46
|
+
"test:e2e": "bun ../../scripts/ci-guard.ts --stall 60 --max 180 bun test ./tests/build-browser.e2e.ts ./tests/shared-shell.e2e.ts",
|
|
47
47
|
"typecheck": "tsc --noEmit",
|
|
48
48
|
"prepack": "bun run build"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@sixb/agent-ui": "^0.1.
|
|
52
|
-
"@sixb/core": "^0.1.
|
|
51
|
+
"@sixb/agent-ui": "^0.1.7",
|
|
52
|
+
"@sixb/core": "^0.1.8",
|
|
53
53
|
"@sixb/ui": "^0.1.1",
|
|
54
54
|
"@tailwindcss/cli": "^4.2.2",
|
|
55
55
|
"tailwindcss": "^4.1.18"
|
package/src/build.ts
CHANGED
|
@@ -12,12 +12,17 @@ const STATIC_ASSET_ORIGIN = "https://sixb-static.invalid"
|
|
|
12
12
|
const brotliCompress = promisify(brotliCompressCallback)
|
|
13
13
|
const gzip = promisify(gzipCallback)
|
|
14
14
|
const PRECOMPRESSION_CONCURRENCY = 4
|
|
15
|
+
export const SHARED_APP_SHELL_FILE_NAME = "shared-index.html"
|
|
15
16
|
|
|
16
17
|
export interface BuildAppOptions {
|
|
17
18
|
/** Path to the generated index.html entry point */
|
|
18
19
|
entryPath: string
|
|
19
20
|
/** Browser TypeScript entry generated alongside `entryPath`. */
|
|
20
21
|
scriptEntryPath: string
|
|
22
|
+
/** Isolated shared-link HTML shell. Must be paired with `sharedScriptEntryPath`. */
|
|
23
|
+
sharedEntryPath?: string
|
|
24
|
+
/** Browser TypeScript entry loaded by the shared-link shell. */
|
|
25
|
+
sharedScriptEntryPath?: string
|
|
21
26
|
/** Generated manifest copied to the stable `/app.webmanifest` output path. */
|
|
22
27
|
manifestPath?: string
|
|
23
28
|
/** Output directory, defaults to `.sixb/dist/app` */
|
|
@@ -30,6 +35,11 @@ export interface BuildAppResult {
|
|
|
30
35
|
logs?: string[]
|
|
31
36
|
}
|
|
32
37
|
|
|
38
|
+
export interface BuildSharedAppDevResult {
|
|
39
|
+
readonly assetPaths: readonly string[]
|
|
40
|
+
readonly html: string
|
|
41
|
+
}
|
|
42
|
+
|
|
33
43
|
export interface BuildAuthExperienceOptions {
|
|
34
44
|
/** Generated HTML shell containing the auth bootstrap placeholder. */
|
|
35
45
|
entryPath: string
|
|
@@ -44,36 +54,18 @@ export interface BuildAuthExperienceOptions {
|
|
|
44
54
|
*/
|
|
45
55
|
export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
|
|
46
56
|
const outdir = options.outdir ?? join(process.cwd(), ".sixb", "dist", "app")
|
|
57
|
+
if (Boolean(options.sharedEntryPath) !== Boolean(options.sharedScriptEntryPath)) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
"[SixbCustomApp] sharedEntryPath and sharedScriptEntryPath must be provided together."
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
47
63
|
// The outdir is build-owned. Clear it so hashed chunks from previous builds
|
|
48
64
|
// don't accumulate (and get served) forever.
|
|
49
65
|
await rm(outdir, { recursive: true, force: true })
|
|
50
66
|
await mkdir(outdir, { recursive: true })
|
|
51
67
|
|
|
52
|
-
const result = await
|
|
53
|
-
// Build the script entry directly rather than Bun's HTML entry. Bun cannot currently preserve
|
|
54
|
-
// a large dynamic-import graph reliably when splitting an HTML entry: one of the lazy chunks
|
|
55
|
-
// can be written into the shell's script tag. Building TypeScript directly also lets Shiki's
|
|
56
|
-
// hundreds of grammars remain off the initial custom-app path.
|
|
57
|
-
entrypoints: [options.scriptEntryPath],
|
|
58
|
-
outdir,
|
|
59
|
-
target: "browser",
|
|
60
|
-
conditions: ["bun"],
|
|
61
|
-
publicPath: "/",
|
|
62
|
-
minify: true,
|
|
63
|
-
splitting: true,
|
|
64
|
-
naming: {
|
|
65
|
-
entry: "app-[hash].[ext]",
|
|
66
|
-
chunk: "chunk-[name]-[hash].[ext]",
|
|
67
|
-
asset: "asset-[name]-[hash].[ext]",
|
|
68
|
-
},
|
|
69
|
-
plugins: [extensionlessSourceImportPlugin],
|
|
70
|
-
// React is bundled into this browser output, so without pinning NODE_ENV the production build
|
|
71
|
-
// of a user's app ships React's development build: dev-only checks on every render, and the
|
|
72
|
-
// warnings that go with them. This is also the only knob that selects the production JSX
|
|
73
|
-
// runtime — an ambient NODE_ENV does not.
|
|
74
|
-
define: { "process.env.NODE_ENV": '"production"' },
|
|
75
|
-
sourcemap: "external",
|
|
76
|
-
})
|
|
68
|
+
const result = await buildBrowserEntry(options.scriptEntryPath, outdir, "app")
|
|
77
69
|
|
|
78
70
|
if (!result.success) {
|
|
79
71
|
return {
|
|
@@ -83,16 +75,7 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
|
|
|
83
75
|
}
|
|
84
76
|
}
|
|
85
77
|
|
|
86
|
-
const
|
|
87
|
-
(output) => output.kind === "entry-point" && output.type.startsWith("text/javascript")
|
|
88
|
-
)
|
|
89
|
-
const stylesheets = result.outputs.filter((output) => output.type.startsWith("text/css"))
|
|
90
|
-
if (!script || stylesheets.length > 1) {
|
|
91
|
-
throw new Error(
|
|
92
|
-
`[SixbCustomApp] Expected one browser entry and at most one stylesheet, found ${script ? 1 : 0} entries and ${stylesheets.length} stylesheets.`
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
|
|
78
|
+
const normalEntry = resolveBrowserEntry(result.outputs, "browser")
|
|
96
79
|
const sourceHtml = restoreAppStaticUrls(await readFile(options.entryPath, "utf-8"))
|
|
97
80
|
const sourceEntryPattern =
|
|
98
81
|
/\s*<script\s+type=["']module["']\s+src=["']\.\/main\.tsx["']><\/script>/
|
|
@@ -101,24 +84,176 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
|
|
|
101
84
|
}
|
|
102
85
|
|
|
103
86
|
const assetTags = [
|
|
104
|
-
...stylesheets.map(
|
|
87
|
+
...normalEntry.stylesheets.map(
|
|
105
88
|
(stylesheet) => `<link rel="stylesheet" crossorigin href="/${basename(stylesheet.path)}">`
|
|
106
89
|
),
|
|
107
|
-
`<script type="module" crossorigin src="/${basename(script.path)}"></script>`,
|
|
90
|
+
`<script type="module" crossorigin src="/${basename(normalEntry.script.path)}"></script>`,
|
|
108
91
|
].join("")
|
|
109
92
|
const html = withLoadingShell(sourceHtml)
|
|
110
93
|
.replace(sourceEntryPattern, "")
|
|
111
94
|
.replace("</head>", ` ${assetTags}</head>`)
|
|
112
95
|
await writeFile(join(outdir, "index.html"), html, "utf-8")
|
|
113
96
|
|
|
97
|
+
const outputs = [...result.outputs]
|
|
98
|
+
if (options.sharedEntryPath && options.sharedScriptEntryPath) {
|
|
99
|
+
// Keep these builds sequential. Bun can mis-resolve package-relative imports when two large
|
|
100
|
+
// browser entry graphs are built together, while content hashes safely deduplicate their
|
|
101
|
+
// shared chunks in one output directory.
|
|
102
|
+
const sharedResult = await buildBrowserEntry(options.sharedScriptEntryPath, outdir, "shared")
|
|
103
|
+
if (!sharedResult.success) {
|
|
104
|
+
return {
|
|
105
|
+
success: false,
|
|
106
|
+
outdir,
|
|
107
|
+
logs: sharedResult.logs.map(String),
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const sharedEntry = resolveBrowserEntry(sharedResult.outputs, "shared browser")
|
|
112
|
+
const sharedHtml = await renderSharedAppHtml(options.sharedEntryPath, sharedEntry)
|
|
113
|
+
await writeFile(join(outdir, SHARED_APP_SHELL_FILE_NAME), sharedHtml, "utf-8")
|
|
114
|
+
outputs.push(...sharedResult.outputs)
|
|
115
|
+
}
|
|
116
|
+
|
|
114
117
|
if (options.manifestPath) {
|
|
115
118
|
await copyFile(options.manifestPath, join(outdir, "app.webmanifest"))
|
|
116
119
|
}
|
|
117
|
-
await precompressBuildAssets(
|
|
120
|
+
await precompressBuildAssets(outputs)
|
|
118
121
|
|
|
119
122
|
return { success: true, outdir }
|
|
120
123
|
}
|
|
121
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Builds the shared shell like production for development, without Bun's HTML/HMR transform.
|
|
127
|
+
* Bun 1.3 injects a script before an inline loader, which would execute application code while the
|
|
128
|
+
* share secret is still in the document URL. The ordinary app keeps HTML-bundle HMR.
|
|
129
|
+
*/
|
|
130
|
+
export async function buildSharedAppDev(options: {
|
|
131
|
+
readonly entryPath: string
|
|
132
|
+
readonly outdir: string
|
|
133
|
+
readonly scriptEntryPath: string
|
|
134
|
+
}): Promise<BuildSharedAppDevResult> {
|
|
135
|
+
await rm(options.outdir, { recursive: true, force: true })
|
|
136
|
+
await mkdir(options.outdir, { recursive: true })
|
|
137
|
+
|
|
138
|
+
let result: Bun.BuildOutput
|
|
139
|
+
try {
|
|
140
|
+
result = await buildBrowserEntry(options.scriptEntryPath, options.outdir, "shared-dev", true)
|
|
141
|
+
} catch (error) {
|
|
142
|
+
await rm(options.outdir, { recursive: true, force: true })
|
|
143
|
+
throw error
|
|
144
|
+
}
|
|
145
|
+
if (!result.success) {
|
|
146
|
+
await rm(options.outdir, { recursive: true, force: true })
|
|
147
|
+
throw new Error(
|
|
148
|
+
`[SixbCustomApp] Failed to build the shared development shell: ${result.logs.map(String).join("\n")}`
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const entry = resolveBrowserEntry(result.outputs, "shared development browser")
|
|
153
|
+
return {
|
|
154
|
+
assetPaths: result.outputs.map((output) => output.path),
|
|
155
|
+
html: await renderSharedAppHtml(options.entryPath, entry),
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function buildBrowserEntry(
|
|
160
|
+
entrypoint: string,
|
|
161
|
+
outdir: string,
|
|
162
|
+
entryName: "app" | "shared" | "shared-dev",
|
|
163
|
+
development = false
|
|
164
|
+
): Promise<Bun.BuildOutput> {
|
|
165
|
+
return await Bun.build({
|
|
166
|
+
// Build the script entry directly rather than Bun's HTML entry. Bun cannot currently preserve
|
|
167
|
+
// a large dynamic-import graph reliably when splitting an HTML entry: one of the lazy chunks
|
|
168
|
+
// can be written into the shell's script tag. Building TypeScript directly also lets Shiki's
|
|
169
|
+
// hundreds of grammars remain off the initial custom-app path.
|
|
170
|
+
entrypoints: [entrypoint],
|
|
171
|
+
outdir,
|
|
172
|
+
target: "browser",
|
|
173
|
+
conditions: ["bun"],
|
|
174
|
+
publicPath: "/",
|
|
175
|
+
minify: !development,
|
|
176
|
+
splitting: true,
|
|
177
|
+
naming: {
|
|
178
|
+
entry: `${entryName}-[hash].[ext]`,
|
|
179
|
+
chunk: "chunk-[name]-[hash].[ext]",
|
|
180
|
+
asset: "asset-[name]-[hash].[ext]",
|
|
181
|
+
},
|
|
182
|
+
plugins: [extensionlessSourceImportPlugin],
|
|
183
|
+
// React is bundled into this browser output, so without pinning NODE_ENV the production build
|
|
184
|
+
// of a user's app ships React's development build: dev-only checks on every render, and the
|
|
185
|
+
// warnings that go with them. This is also the only knob that selects the production JSX
|
|
186
|
+
// runtime — an ambient NODE_ENV does not.
|
|
187
|
+
define: { "process.env.NODE_ENV": development ? '"development"' : '"production"' },
|
|
188
|
+
sourcemap: development ? "inline" : "external",
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function renderSharedAppHtml(
|
|
193
|
+
entryPath: string,
|
|
194
|
+
entry: {
|
|
195
|
+
readonly script: Bun.BuildArtifact
|
|
196
|
+
readonly stylesheets: readonly Bun.BuildArtifact[]
|
|
197
|
+
}
|
|
198
|
+
): Promise<string> {
|
|
199
|
+
const sourceHtml = restoreAppStaticUrls(await readFile(entryPath, "utf-8"))
|
|
200
|
+
const sourceEntryPattern =
|
|
201
|
+
/const \{ startSharedApp \} = await import\((["'])\.\/shared-main\.tsx\1\)\s+startSharedApp\(bootstrap\)/
|
|
202
|
+
if (!sourceEntryPattern.test(sourceHtml)) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
"[SixbCustomApp] Generated shared app shell is missing its ./shared-main.tsx import."
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The share secret initially lives in location.hash. Static scripts, stylesheets, and preloads
|
|
209
|
+
// are forbidden. The inline loader removes the fragment, imports the authority bootstrap, then
|
|
210
|
+
// that bootstrap invokes loadAppStyles only after session establishment and canonicalization.
|
|
211
|
+
const stylesheetUrls = entry.stylesheets.map((stylesheet) => `/${basename(stylesheet.path)}`)
|
|
212
|
+
const bootstrap = [
|
|
213
|
+
`const { startSharedApp } = await import(${JSON.stringify(`/${basename(entry.script.path)}`)})`,
|
|
214
|
+
...(stylesheetUrls.length > 0
|
|
215
|
+
? [
|
|
216
|
+
" startSharedApp(bootstrap, {",
|
|
217
|
+
" async loadAppStyles() {",
|
|
218
|
+
" await Promise.all(",
|
|
219
|
+
` ${JSON.stringify(stylesheetUrls)}.map((href) => new Promise((resolve) => {`,
|
|
220
|
+
' const link = document.createElement("link")',
|
|
221
|
+
' link.rel = "stylesheet"',
|
|
222
|
+
' link.crossOrigin = "anonymous"',
|
|
223
|
+
' link.addEventListener("load", resolve, { once: true })',
|
|
224
|
+
' link.addEventListener("error", resolve, { once: true })',
|
|
225
|
+
" link.href = href",
|
|
226
|
+
" document.head.append(link)",
|
|
227
|
+
" }))",
|
|
228
|
+
" )",
|
|
229
|
+
" },",
|
|
230
|
+
" })",
|
|
231
|
+
]
|
|
232
|
+
: [" startSharedApp(bootstrap)"]),
|
|
233
|
+
].join("\n")
|
|
234
|
+
|
|
235
|
+
return withLoadingShell(sourceHtml).replace(sourceEntryPattern, bootstrap)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function resolveBrowserEntry(
|
|
239
|
+
outputs: readonly Bun.BuildArtifact[],
|
|
240
|
+
label: string
|
|
241
|
+
): {
|
|
242
|
+
readonly script: Bun.BuildArtifact
|
|
243
|
+
readonly stylesheets: readonly Bun.BuildArtifact[]
|
|
244
|
+
} {
|
|
245
|
+
const script = outputs.find(
|
|
246
|
+
(output) => output.kind === "entry-point" && output.type.startsWith("text/javascript")
|
|
247
|
+
)
|
|
248
|
+
const stylesheets = outputs.filter((output) => output.type.startsWith("text/css"))
|
|
249
|
+
if (!script || stylesheets.length > 1) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
`[SixbCustomApp] Expected one ${label} entry and at most one stylesheet, found ${script ? 1 : 0} entries and ${stylesheets.length} stylesheets.`
|
|
252
|
+
)
|
|
253
|
+
}
|
|
254
|
+
return { script, stylesheets }
|
|
255
|
+
}
|
|
256
|
+
|
|
122
257
|
/** Builds the optional custom auth entry as API-served static assets. */
|
|
123
258
|
export async function buildAuthExperience(
|
|
124
259
|
options: BuildAuthExperienceOptions
|
|
@@ -196,7 +331,8 @@ export async function prepareAppHtmlBundleEntry(entryPath: string): Promise<stri
|
|
|
196
331
|
/(<link\s+rel=["'](?:manifest|icon|apple-touch-icon)["']\s+href=["'])(\/[^"']*)(["'][^>]*>)/g,
|
|
197
332
|
`$1${STATIC_ASSET_ORIGIN}$2$3`
|
|
198
333
|
)
|
|
199
|
-
const
|
|
334
|
+
const entryName = basename(entryPath, ".html")
|
|
335
|
+
const bundleEntryPath = join(dirname(entryPath), `${entryName}.sixb-bundle.html`)
|
|
200
336
|
await writeFile(bundleEntryPath, bundleHtml, "utf-8")
|
|
201
337
|
return bundleEntryPath
|
|
202
338
|
}
|
|
@@ -256,11 +392,16 @@ const extensionlessSourceImportPlugin: Bun.BunPlugin = {
|
|
|
256
392
|
async function precompressBuildAssets(
|
|
257
393
|
outputs: readonly { readonly path: string; readonly type: string }[]
|
|
258
394
|
): Promise<void> {
|
|
259
|
-
const paths =
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
395
|
+
const paths = [
|
|
396
|
+
...new Set(
|
|
397
|
+
outputs
|
|
398
|
+
.filter(
|
|
399
|
+
(output) =>
|
|
400
|
+
output.type.startsWith("text/javascript") || output.type.startsWith("text/css")
|
|
401
|
+
)
|
|
402
|
+
.map((output) => output.path)
|
|
403
|
+
),
|
|
404
|
+
]
|
|
264
405
|
let cursor = 0
|
|
265
406
|
|
|
266
407
|
const worker = async () => {
|