Skip to content

Commit dd9e458

Browse files
committed
Move built client files to client_bundle folder to make embedding them into the server binary cleaner.
1 parent dcafb5c commit dd9e458

File tree

10 files changed

+65
-40
lines changed

10 files changed

+65
-40
lines changed

.air.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ tmp_dir = "tmp"
44
[build]
55
args_bin = []
66
bin = "silverbullet"
7-
cmd = "deno task build && go build"
7+
cmd = "make"
88
# delay = 10000
9-
exclude_dir = ["node_modules", "website", "scripts", "dist_client_bundle", "dist_base_fs_bundle", "dist"]
9+
exclude_dir = ["node_modules", "website", "scripts", "client_bundle", "dist"]
1010
exclude_file = ["public_version.ts"]
1111
exclude_regex = ["_test.go", ".test.ts"]
1212
exclude_unchanged = false

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.env
22
.DS_Store
3-
dist_client_bundle
4-
dist_base_fs_bundle
3+
client_bundle/client
4+
client_bundle/base_fs
55
dist
66
*.js.map
77
website_build
@@ -17,4 +17,4 @@ deploy.json
1717
tmp_playground
1818
/public_version.ts
1919
tmp
20-
*.ipynb
20+
.idea

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
build:
2-
# Build frontend
2+
# Build client
33
deno task build
44
# Build plug-compile
55
deno task build-plug-compile
6-
# Build backend
6+
# Build server
77
go build
88

99
clean:
10-
deno task clean
10+
rm -rf client_bundle/{base_fs,client} dist public_version.ts
1111
rm -f silverbullet
1212

1313
check:

build_client.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { patchDenoLibJS } from "./client/plugos/plug_compile.ts";
77
import { denoPlugins } from "@luca/esbuild-deno-loader";
88
import * as esbuild from "esbuild";
99

10-
// This builds the client and puts it into dist_client_bundle/
10+
// This builds the client and puts it into client_bundle/client
1111

1212
export async function bundleAll(): Promise<void> {
1313
await buildCopyBundleAssets();
@@ -50,8 +50,8 @@ export async function copyAssets(dist: string) {
5050
}
5151

5252
async function buildCopyBundleAssets() {
53-
await Deno.mkdir("dist_client_bundle", { recursive: true });
54-
await Deno.mkdir("dist_base_fs_bundle", { recursive: true });
53+
await Deno.mkdir("client_bundle/client", { recursive: true });
54+
await Deno.mkdir("client_bundle/base_fs", { recursive: true });
5555

5656
console.log("Now ESBuilding the client and service workers...");
5757

@@ -66,7 +66,7 @@ async function buildCopyBundleAssets() {
6666
out: "service_worker",
6767
},
6868
],
69-
outdir: "dist_client_bundle",
69+
outdir: "client_bundle/client",
7070
absWorkingDir: Deno.cwd(),
7171
bundle: true,
7272
treeShaking: true,
@@ -89,11 +89,13 @@ async function buildCopyBundleAssets() {
8989
}
9090

9191
// Patch the service_worker {{CACHE_NAME}}
92-
let swCode = await Deno.readTextFile("dist_client_bundle/service_worker.js");
92+
let swCode = await Deno.readTextFile(
93+
"client_bundle/client/service_worker.js",
94+
);
9395
swCode = swCode.replaceAll("{{CACHE_NAME}}", `cache-${Date.now()}`);
94-
await Deno.writeTextFile("dist_client_bundle/service_worker.js", swCode);
96+
await Deno.writeTextFile("client_bundle/client/service_worker.js", swCode);
9597

96-
await copyAssets("dist_client_bundle/.client");
98+
await copyAssets("client_bundle/client/.client");
9799

98100
console.log("Built!");
99101
}

build_plugs_libraries.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
77
import { copy } from "@std/fs";
88
import { version } from "./version.ts";
99

10-
// This builds all built-in plugs and libraries and puts them into dist_base_fs_bundle/
10+
// This builds all built-in plugs and libraries and puts them into client_bundle/base_fs
1111

1212
if (import.meta.main) {
1313
await updateVersionFile();
@@ -20,13 +20,13 @@ if (import.meta.main) {
2020
`./plugs/${name}/${name}.plug.yaml`
2121
);
2222

23-
const plugBundlePath = "dist_base_fs_bundle";
23+
const plugBundlePath = "client_bundle/base_fs";
2424
const targetDir = path.join(plugBundlePath, "_plug");
2525
Deno.mkdirSync(targetDir, { recursive: true });
2626
Deno.mkdirSync("dist", { recursive: true });
2727

2828
// Copy Library files
29-
await copy("libraries/Library", "dist_base_fs_bundle/Library", {
29+
await copy("libraries/Library", `${plugBundlePath}/Library`, {
3030
overwrite: true,
3131
});
3232

client_bundle/bundle.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package client_bundle
2+
3+
// Files in this folder are generated as part of the build process (except this one)
4+
// The sole purpose of this file is to embed these generated client files into the server binary
5+
6+
import "embed"
7+
8+
//go:embed base_fs/*
9+
//go:embed client/*
10+
var BundledFiles embed.FS

deno.json

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
"name": "@silverbulletmd/silverbullet",
33
"version": "2.0.0",
44
"tasks": {
5-
"build": "deno task clean && deno run -A build_plugs_libraries.ts && deno run -A build_client.ts",
5+
"build": "make clean && deno run -A build_plugs_libraries.ts && deno run -A build_client.ts",
66
"build-production": "deno task build --production",
77
"build-plug-compile": "deno run -A build_plug_compile.ts",
8-
"clean": "rm -rf dist_client_bundle dist_base_fs_bundle dist public_version.ts",
9-
"deep-clean-mac": "rm -f deno.lock && rm -rf $HOME/Library/Caches/deno && deno task clean",
108
"check": "find . -name '*.ts*' -not -path \"*/node_modules/*\" | xargs deno check",
119
"checks": "deno task check && deno task lint && deno task test",
1210
"lint": "deno lint --fix",
@@ -15,17 +13,29 @@
1513
"plugs": "deno run -A build_plugs_libraries.ts"
1614
},
1715
"publish": {
18-
"exclude": ["website", "CHANGELOG.md", "**/*.md"]
16+
"exclude": [
17+
"website",
18+
"CHANGELOG.md",
19+
"**/*.md"
20+
]
1921
},
2022
"lint": {
21-
"exclude": ["dist", "dist_client_bundle", "dist_base_fs_bundle"],
23+
"exclude": [
24+
"dist",
25+
"client_bundle"
26+
],
2227
"rules": {
23-
"exclude": ["no-explicit-any", "no-slow-types", "jsx-boolean-value"]
28+
"exclude": [
29+
"no-explicit-any",
30+
"no-slow-types",
31+
"jsx-boolean-value"
32+
]
2433
}
2534
},
2635
"fmt": {
2736
"exclude": [
2837
"dist*",
38+
"client_bundle/*",
2939
"website",
3040
"**/*.md",
3141
"**/*.html",
@@ -34,7 +44,12 @@
3444
]
3545
},
3646
"compilerOptions": {
37-
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"],
47+
"lib": [
48+
"dom",
49+
"dom.iterable",
50+
"dom.asynciterable",
51+
"deno.ns"
52+
],
3853
"jsx": "react-jsx",
3954
"jsxImportSource": "npm:preact@10.26.4"
4055
},

server/cmd/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
_ "embed"
5+
"errors"
56
"fmt"
67
"io/fs"
78
"log"
@@ -150,8 +151,8 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
150151
}
151152
}
152153

153-
serverConfig.ClientBundle = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "dist_client_bundle", bundlePathDate, nil)
154-
rootSpaceConfig.SpacePrimitives = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "dist_base_fs_bundle", bundlePathDate, spacePrimitives)
154+
serverConfig.ClientBundle = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "client", bundlePathDate, nil)
155+
rootSpaceConfig.SpacePrimitives = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "base_fs", bundlePathDate, spacePrimitives)
155156

156157
if serverConfig.BindHost == "127.0.0.1" {
157158
log.Println("SilverBullet will only be available locally, to allow outside connections, pass -L0.0.0.0 as a flag, and put a TLS terminator on top.")
@@ -197,7 +198,7 @@ func ensureIndexAndConfig(rootSpaceConfig *server.SpaceConfig) {
197198
// Now let's check for a CONFIG.md
198199
configPagePath := "CONFIG.md"
199200
_, err = rootSpaceConfig.SpacePrimitives.GetFileMeta(configPagePath)
200-
if err == server.ErrNotFound {
201+
if errors.Is(err, server.ErrNotFound) {
201202
log.Printf("Config page %s does not yet exist, creating...", configPagePath)
202203
if _, err := rootSpaceConfig.SpacePrimitives.WriteFile(configPagePath, configPageContent, nil); err != nil {
203204
log.Fatalf("Could not write config page %s: %v", configPagePath, err)

server/proxy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
7070
}
7171
}
7272

73-
// Make the request
73+
// TODO: Replaced with specifically configured client
7474
client := http.DefaultClient
7575

7676
resp, err := client.Do(req)
@@ -90,5 +90,7 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
9090

9191
// Set status code and copy body
9292
w.WriteHeader(resp.StatusCode)
93-
io.Copy(w, resp.Body)
93+
if _, err := io.Copy(w, resp.Body); err != nil {
94+
log.Printf("Proxy: failed to copy response body: %v", err)
95+
}
9496
}

silverbullet.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package main
22

33
import (
4-
"embed"
4+
_ "embed"
55

6+
"github.com/silverbulletmd/silverbullet/client_bundle"
67
"github.com/silverbulletmd/silverbullet/server/cmd"
78
)
89

9-
// Embed client files, plugs and version files into the binary
10-
//
11-
//go:embed dist_client_bundle/*
12-
//go:embed dist_base_fs_bundle/*
13-
var bundledFiles embed.FS
14-
1510
//go:embed public_version.ts
16-
var versionFileText string
11+
var VersionFileText string
1712

1813
func main() {
19-
c := cmd.ServerCommand(bundledFiles)
20-
c.AddCommand(cmd.VersionCommand(versionFileText), cmd.UpgradeCommand(), cmd.UpgradeEdgeCommand())
14+
c := cmd.ServerCommand(client_bundle.BundledFiles)
15+
c.AddCommand(cmd.VersionCommand(VersionFileText), cmd.UpgradeCommand(), cmd.UpgradeEdgeCommand())
2116
c.Execute()
2217
}

0 commit comments

Comments
 (0)