Skip to content

Commit c83f55f

Browse files
Refactors TypeScript into a single spacetimedb package (#3248)
# Description of Changes This PR moves most of the contents of `@clockworklabs/spacetimedb-sdk` into the `spacetimedb` module. The `spacetimedb` module now exports `sdk` and `server` as separate subpaths where `sdk` contains the code which was previously in `@clockworklabs/spacetimedb-sdk`. In particular it makes the following moves: - `/sdks/typescript/packages/sdk` -> `/sdks/typescript` - most of the contents of `/sdks/typescript/packages/sdk` -> `crates/bindings-typescript` - `/sdks/typescript/packages/test-app` -> `crates/bindings-typescript/test-app` The following packags was NOT moved: `/sdks/typescript/examples/quickstart-chat` ## Motivation In accordance with #3250, we would like to consolidate `@clockworklabs/spacetimedb-sdk` into a single `spacetimedb` package so that users can import the different things they need from a single package. ### Pros: - allow users to install a single package with subpaths `spacetimedb`, `spacetimedb/react`, `spacetimedb/sdk`, `spacetimedb/server`, etc. - Is much simpler for bundling, etc. - Is backwards compatible with `@clockworklabs/spacetimedb-sdk` which now becomes a thin wrapper - eventually allow us to break up the `spacetimedb` package into other packages if we want to split them up (e.g. `@spacetimedb/lib`, `@spacetimedb/sdk`, etc.) and we can solve the build complexity that introduces when we get to it - eventually allow us to move `bindings-csharp` out of the crates directory where it probably doesn't belong anyway - organizes all TypeScript packages into the packages directory where you'd normally expect them, with the possible exception of `/sdks/typescript` if we wanted to leave that separate ### Cons: - The `sdk` directory is now a bit of a ruse as to where the code actually lives since it's just a thin wrapper. If it eventually becomes its own independent package, we'll also have to break up spacetimedb into `@spacetimedb/lib` and `@spacetimedb/server` so that `@clockworklabs/spacetimedb-sdk` can depend on `@spacetimedb/lib` while being a dependency of `spacetimedb`. Ideally this change would have been made later, however, it became necessary for the following **heinously disastrous chain of forcing moves**: 1. Adding `react` support necessitated shipping react as an optional peer dependency under `@clockworklabs/spacetimedb-sdk/react` 2. This required adding a new build target/export/bundle 3. Previously `@clockworklabs/spacetimedb-sdk` was configured to have `noExternal` for `spacetimedb` meaning it would collect the library into the sdk bundle. I attempted to continue this for react support but... 4. Creating a new `react` bundle which also pulled in `spacetimedb` caused their to be nominal type conflicts between classes in the duplicate `spacetimedb` bundles. 5. Changing `spacetimedb` to be included as `external` caused compile errors because `@clockworklabs/spacetimedb-sdk` is configured in `tsconfig.json` to fail on unused variables and it was now including the source of `spacetimedb` which is not configured to error on unused variables and has "unused" private variables which are actually used by us secretly, but not exposed to the clients. > SIDE NOTE: The unused variables settings cannot be turned off on a line by line basis, so it has to be turned off entirely, but in order to maintain the linting checks we had I used `eslint` to enforce the rule so that we could disable it line by line. (This caused me to discover quite a lot of things that were broken that were caught by `eslint` being applied to the entire project. `eslint` was previously only applied to the `quickstart-chat` and the `crates/bindings-typescript` library) 6. Changing the build to be external, now requires `spacetimedb` to also be published to npm as its own module which `@clockworklabs/spacetimedb-sdk` now imports, which requires that we add `tsup` config to `spacetimedb` to publish a built version of the library. 7. The only way to avoid that is to move the `sdk` and `react` code from `@clockworklabs/spacetimedb-sdk` into the existing `spacetimedb` package to avoid the duplicate import problem on step 4 and change `@clockworklabs/spacetimedb-sdk` back to again use `noExternal` for its `spacetimedb` dependency. And here we are. I chose not to move `/crates/bindings-typescript` even though that's probably not a great place long term. It would be better to have it in `/packages/spacetimedb` or `/npm-packages/spacetimedb` or `/ts-packages/spacetimedb` or something, and move all our TypeScript packages in there. But that is a different matter. The net result however is that we have a new `spacetimedb` package which exports the different parts of the API under: - `spacetimedb` - `spacetimedb/server` - `spacetimedb/sdk` - `spacetimedb/react` while still not breaking the existing deploy process, nor any users/developers who are currently using `@clockworklabs/spacetimedb-sdk`. I think long term should we ever decide to split `spacetimedb` up into multiple packages or if we have additional unrelated packages, we should publish them to the `@spacetimedb` org which I reserved for us here: https://www.npmjs.com/org/spacetimedb > NOTE: `spacetimedb` is a package and `@spacetimedb` is an org. `spacetimedb/sdk` is not a separate package, it's a subpath export of the `spacetimedb` package, whereas `@spacetimedb/sdk` would be (and would need to be) it's own separate package. You can certainly have both `spacetimedb/sdk` and `@spacetimedb/sdk`. We could for example host the code for the sdk at `@spacetimedb/sdk` and just reexport it from `spacetimedb` under the `spacetimedb/sdk` subpath. # API and ABI breaking changes This should not change or modify the API or ABI in any way. If it does so accidentally it is a bug, although I carefully went through the exports. # Expected complexity level and risk 3 because it changes how the SDK is built a bit and rearranges a lot of paths. # Testing - [x] All of the CI passes - [x] I also ran quickstart-chat to confirm that it is not broken - [x] I also ran test-app
1 parent eeef4e9 commit c83f55f

187 files changed

Lines changed: 1405 additions & 1775 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/typescript-lint.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
run_install: true
2626

2727
- name: Get pnpm store directory
28-
working-directory: sdks/typescript
2928
shell: bash
3029
run: |
3130
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
@@ -39,5 +38,4 @@ jobs:
3938
${{ runner.os }}-pnpm-store-
4039
4140
- name: Lint
42-
working-directory: sdks/typescript
4341
run: pnpm lint

.github/workflows/typescript-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
run: pnpm build
5252

5353
- name: Run SDK tests
54-
working-directory: sdks/typescript/packages/sdk
54+
working-directory: sdks/typescript
5555
run: pnpm test
5656

5757
# - name: Extract SpacetimeDB branch name from file

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pnpm-lock.yaml
33
dist
44
target
55
.github
6+
coverage

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ members = [
4747
"sdks/rust/tests/connect_disconnect_client",
4848
"tools/upgrade-version",
4949
"tools/license-check",
50-
"sdks/typescript/packages/test-app/server",
50+
"crates/bindings-typescript/test-app/server",
5151
]
5252
default-members = ["crates/cli", "crates/standalone", "crates/update"]
5353
# cargo feature graph resolver. v3 is default in edition2024 but workspace

crates/bindings-typescript/eslint.config.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

crates/bindings-typescript/package.json

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@
66
"bugs": {
77
"url": "https://github.com/clockworklabs/SpacetimeDB/issues"
88
},
9-
"source": "src/index.ts",
10-
"main": "dist/index.cjs",
11-
"module": "dist/index.mjs",
12-
"types": "src/index.ts",
13-
"exports": {
14-
".": {
15-
"types": "./src/index.ts",
16-
"source": "./src/index.ts",
17-
"development": "./src/index.ts",
18-
"import": "./dist/index.mjs",
19-
"require": "./dist/index.cjs",
20-
"default": "./dist/index.mjs"
21-
}
22-
},
239
"files": [
2410
"src",
2511
"dist",
@@ -33,29 +19,136 @@
3319
"license": "ISC",
3420
"author": "Clockwork Labs",
3521
"type": "module",
22+
"sideEffects": false,
3623
"scripts": {
37-
"build": "tsc",
38-
"format": "prettier --write .",
39-
"lint": "eslint . && prettier . --check",
24+
"build": "tsup",
25+
"format": "prettier . --write --ignore-path ../../.prettierignore",
26+
"lint": "eslint . && prettier . --check --ignore-path ../../.prettierignore",
4027
"test": "vitest run",
4128
"coverage": "vitest run --coverage",
42-
"generate": "cargo run -p spacetimedb-codegen --example regen-typescript-moduledef && prettier --write src/autogen"
29+
"brotli-size": "brotli-size dist/index.js",
30+
"size": "pnpm -s build && size-limit",
31+
"generate:moduledef": "cargo run -p spacetimedb-codegen --example regen-typescript-moduledef && prettier --write src/lib/autogen",
32+
"generate:client-api": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-client-api-messages --example get_ws_schema > ws_schema.json && cargo run -p spacetimedb-cli generate --lang typescript --out-dir src/sdk/client_api --module-def ws_schema.json && rm ws_schema.json && find src/sdk/client_api -type f -exec perl -pi -e 's#\\@clockworklabs/spacetimedb-sdk#../../index#g' {} + && prettier --write src/sdk/client_api",
33+
"generate:test-app": "pnpm --filter @clockworklabs/test-app generate",
34+
"generate": "pnpm generate:moduledef && pnpm generate:client-api && pnpm generate:test-app"
4335
},
36+
"source": "src/index.ts",
37+
"main": "dist/index.cjs",
38+
"module": "dist/index.mjs",
39+
"types": "src/index.ts",
40+
"exports": {
41+
".": {
42+
"types": "./src/index.ts",
43+
"source": "./src/index.ts",
44+
"development": "./src/index.ts",
45+
"browser": "./dist/index.browser.mjs",
46+
"import": "./dist/index.mjs",
47+
"require": "./dist/index.cjs",
48+
"default": "./dist/index.mjs"
49+
},
50+
"./sdk": {
51+
"types": "./src/sdk/index.ts",
52+
"browser": "./dist/sdk/index.browser.mjs",
53+
"import": "./dist/sdk/index.mjs",
54+
"require": "./dist/sdk/index.cjs",
55+
"default": "./dist/sdk/index.mjs"
56+
},
57+
"./server": {
58+
"types": "./src/server/index.ts",
59+
"import": "./dist/server/index.mjs",
60+
"require": "./dist/server/index.cjs",
61+
"default": "./dist/server/index.mjs"
62+
}
63+
},
64+
"size-limit": [
65+
{
66+
"name": "cjs (brotli)",
67+
"path": "dist/index.cjs",
68+
"brotli": true,
69+
"limit": "25 kB"
70+
},
71+
{
72+
"name": "esm (brotli)",
73+
"path": "dist/index.mjs",
74+
"brotli": true,
75+
"limit": "20 kB"
76+
},
77+
{
78+
"name": "esm (gzip)",
79+
"path": "dist/index.mjs",
80+
"gzip": true,
81+
"limit": "25 kB"
82+
},
83+
{
84+
"name": "esm (uncompressed)",
85+
"path": "dist/index.mjs",
86+
"brotli": false,
87+
"limit": "150 kB"
88+
},
89+
{
90+
"name": "esm min (brotli)",
91+
"path": "dist/min/index.mjs",
92+
"brotli": true,
93+
"limit": "10 kB"
94+
},
95+
{
96+
"name": "esm min (gzip)",
97+
"path": "dist/min/index.mjs",
98+
"gzip": true,
99+
"limit": "15 kB"
100+
},
101+
{
102+
"name": "esm min (uncompressed)",
103+
"path": "dist/min/index.mjs",
104+
"brotli": false,
105+
"limit": "60 kB"
106+
},
107+
{
108+
"name": "sdk esm min (brotli)",
109+
"path": "dist/min/sdk/index.browser.mjs",
110+
"brotli": true,
111+
"limit": "10 kB"
112+
},
113+
{
114+
"name": "sdk esm min (gzip)",
115+
"path": "dist/min/sdk/index.browser.mjs",
116+
"gzip": true,
117+
"limit": "15 kB"
118+
},
119+
{
120+
"name": "sdk esm min (uncompressed)",
121+
"path": "dist/min/sdk/index.browser.mjs",
122+
"limit": "10 kB"
123+
}
124+
],
44125
"dependencies": {
45-
"@zxing/text-encoding": "^0.9.0",
46126
"base64-js": "^1.5.1",
47127
"prettier": "^3.3.3"
48128
},
129+
"peerDependencies": {
130+
"undici": "^6.19.2"
131+
},
132+
"peerDependenciesMeta": {
133+
"undici": {
134+
"optional": true
135+
}
136+
},
49137
"devDependencies": {
50138
"@eslint/js": "^9.17.0",
139+
"@size-limit/file": "^11.2.0",
51140
"@typescript-eslint/eslint-plugin": "^8.18.2",
52141
"@typescript-eslint/parser": "^8.18.2",
142+
"@vitest/coverage-v8": "^3.2.4",
143+
"brotli-size-cli": "^1.0.0",
53144
"eslint": "^9.33.0",
54145
"globals": "^15.14.0",
146+
"size-limit": "^11.2.0",
55147
"ts-node": "^10.9.2",
148+
"tsup": "^8.1.0",
56149
"typescript": "^5.9.2",
57150
"typescript-eslint": "^8.18.2",
58-
"vite": "^7.1.3",
151+
"vite": "^7.1.5",
59152
"vitest": "^3.2.4"
60153
}
61154
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
export * from './connection_id';
2-
export * from './algebraic_type';
3-
export * from './algebraic_value';
4-
export { default as BinaryReader } from './binary_reader';
5-
export { default as BinaryWriter } from './binary_writer';
6-
export * from './schedule_at';
7-
export * from './time_duration';
8-
export * from './timestamp';
9-
export * from './utils';
10-
export * from './identity';
1+
export * from './lib/connection_id';
2+
export * from './lib/algebraic_type';
3+
export * from './lib/algebraic_value';
4+
export { default as BinaryReader } from './lib/binary_reader';
5+
export { default as BinaryWriter } from './lib/binary_writer';
6+
export * from './lib/schedule_at';
7+
export * from './lib/time_duration';
8+
export * from './lib/timestamp';
9+
export * from './lib/utils';
10+
export * from './lib/identity';
11+
export * from './sdk';
1112
export { default as t } from './server/type_builders';
File renamed without changes.
File renamed without changes.

crates/bindings-typescript/src/autogen/algebraic_type_type.ts renamed to crates/bindings-typescript/src/lib/autogen/algebraic_type_type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
deepEqual as __deepEqual,
1515
type AlgebraicType as __AlgebraicTypeType,
1616
type AlgebraicTypeVariants as __AlgebraicTypeVariants,
17-
} from '../index';
17+
} from '../../index';
1818
import { SumType } from './sum_type_type';
1919
// Mark import as potentially unused
2020
declare type __keep_SumType = SumType;

0 commit comments

Comments
 (0)