Skip to content

Commit 6447ab2

Browse files
sprusaksprusak-lm
andauthored
fix: safe build for target ssr (#109)
- used tsup and new mapping as in query-devtools https://github.com/TanStack/query/blob/main/packages/query-devtools/package.json - eliminated browser only code for a server build, this makes sure projects with server side rendering can safely execute this code --------- Co-authored-by: Szymon Prusak <[email protected]>
1 parent 599a59d commit 6447ab2

File tree

9 files changed

+329
-21
lines changed

9 files changed

+329
-21
lines changed

.changeset/puny-eels-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools': patch
3+
---
4+
5+
Changed package.json exports to allow safe usage in ssr environments

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ vite.config.js.timestamp-*
5555
vite.config.ts.timestamp-*
5656

5757
.angular
58+
.nitro

examples/react/start/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"db:migrate": "prisma migrate dev --name init",
1212
"db:reset": " prisma migrate reset",
1313
"db:gen": "prisma generate",
14-
"db:studio": "prisma studio"
14+
"db:studio": "prisma studio",
15+
"build": "vite build"
1516
},
1617
"dependencies": {
1718
"@prisma/client": "^6.13.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"size-limit": [
4949
{
50-
"path": "packages/devtools/dist/esm/index.js",
50+
"path": "packages/devtools/dist/index.js",
5151
"limit": "40 KB"
5252
},
5353
{

packages/devtools/package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@
1818
"devtools"
1919
],
2020
"type": "module",
21-
"types": "dist/esm/index.d.ts",
22-
"module": "dist/esm/index.js",
21+
"types": "dist/index.d.ts",
22+
"module": "dist/index.js",
2323
"exports": {
24-
".": {
25-
"import": {
26-
"types": "./dist/esm/index.d.ts",
27-
"default": "./dist/esm/index.js"
28-
}
24+
"browser": {
25+
"development": {
26+
"types": "./dist/index.d.ts",
27+
"import": "./dist/dev.js"
28+
},
29+
"types": "./dist/index.d.ts",
30+
"import": "./dist/index.js"
2931
},
30-
"./package.json": "./package.json"
32+
"node": {
33+
"types": "./dist/index.d.ts",
34+
"import": "./dist/server.js"
35+
},
36+
"types": "./dist/index.d.ts",
37+
"import": "./dist/index.js"
3138
},
3239
"sideEffects": false,
3340
"engines": {
@@ -45,7 +52,7 @@
4552
"test:lib:dev": "pnpm test:lib --watch",
4653
"test:types": "tsc",
4754
"test:build": "publint --strict",
48-
"build": "vite build"
55+
"build": "tsup"
4956
},
5057
"dependencies": {
5158
"@solid-primitives/keyboard": "^1.2.8",
@@ -60,6 +67,8 @@
6067
"solid-js": ">=1.9.7"
6168
},
6269
"devDependencies": {
70+
"tsup": "^8.5.0",
71+
"tsup-preset-solid": "^2.2.0",
6372
"vite-plugin-solid": "^2.11.6"
6473
}
6574
}

packages/devtools/src/core.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export class TanStackDevtoolsCore {
6161
}
6262

6363
mount<T extends HTMLElement>(el: T) {
64+
// tsup-preset-solid statically replaces this variable during build, which eliminates this code from server bundle
65+
if (import.meta.env.SSR) return
66+
6467
if (this.#isMounted) {
6568
throw new Error('Devtools is already mounted')
6669
}

packages/devtools/tsconfig.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"include": [
4-
"src",
5-
"eslint.config.js",
6-
"vite.config.ts",
7-
"tests",
8-
"src/server"
9-
],
3+
"include": ["src", "*.config.*", "tests", "src/server"],
104
"compilerOptions": {
115
"jsxImportSource": "solid-js",
126
"jsx": "preserve",

packages/devtools/tsup.config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from 'tsup'
2+
import { generateTsupOptions, parsePresetOptions } from 'tsup-preset-solid'
3+
4+
const preset_options = {
5+
entries: {
6+
entry: 'src/index.ts',
7+
dev_entry: true,
8+
server_entry: true,
9+
},
10+
cjs: false,
11+
drop_console: true,
12+
}
13+
14+
export default defineConfig(() => {
15+
const parsed_data = parsePresetOptions(preset_options)
16+
const tsup_options = generateTsupOptions(parsed_data)
17+
18+
return tsup_options
19+
})

0 commit comments

Comments
 (0)