Skip to content

Commit f7dd9d0

Browse files
authored
feat: make direct imports production-safe (#8)
1 parent c16cf07 commit f7dd9d0

8 files changed

Lines changed: 80 additions & 15 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start-devtools": patch
3+
---
4+
5+
Make direct imports production-safe with a no-op default export and development-only browser and server implementations. Register server-function observers from the browser entry.

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import { DevToolbar } from "@solidjs/start-devtools";
1818
</DevToolbar>;
1919
```
2020

21-
For authored server and client entries, render `DevToolbar` around the app in a shared
22-
document or root component. The package selects an SSR-safe build on the server, so the
23-
same component provides the development error boundary on both sides.
21+
For custom server and client entries, render `DevToolbar` around the app in a shared
22+
document or root component. In development, the package selects the browser or SSR build
23+
for the current environment. In production, it becomes a children-only passthrough and
24+
does not include the toolbar.
2425

25-
The package is intended for development and should not be imported into production entries.
26+
The same import is safe in development and production entries.
2627

2728
For component and reactivity inspection, see [Solid Devtools](https://github.com/thetarnav/solid-devtools).

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
"exports": {
1212
".": {
1313
"types": "./dist/types/index.d.ts",
14-
"worker": "./dist/server.js",
15-
"browser": "./dist/index.js",
16-
"deno": "./dist/server.js",
17-
"node": "./dist/server.js",
18-
"import": "./dist/index.js"
14+
"development": {
15+
"worker": "./dist/server.js",
16+
"browser": "./dist/index.js",
17+
"deno": "./dist/server.js",
18+
"node": "./dist/server.js",
19+
"import": "./dist/index.js"
20+
},
21+
"default": "./dist/noop.js"
1922
}
2023
},
2124
"scripts": {

rollup.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@ export default [
105105
generate: 'ssr',
106106
server: true,
107107
}),
108+
config({
109+
input: 'src/noop.ts',
110+
entryFileNames: 'noop.js',
111+
chunkFileNames: 'noop-chunks/[name]-[hash].js',
112+
generate: 'ssr',
113+
server: true,
114+
}),
108115
];

src/exports.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { execFileSync } from 'node:child_process';
2+
import { describe, expect, it } from 'vitest';
3+
4+
function resolvePackage(...conditions: string[]): string {
5+
return execFileSync(
6+
process.execPath,
7+
[
8+
...conditions.map((condition) => `--conditions=${condition}`),
9+
'--input-type=module',
10+
'--eval',
11+
`console.log(import.meta.resolve('@solidjs/start-devtools'))`,
12+
],
13+
{ encoding: 'utf-8' },
14+
).trim();
15+
}
16+
17+
describe('package exports', () => {
18+
it('uses the no-op build by default', () => {
19+
expect(resolvePackage()).toMatch(/\/dist\/noop\.js$/);
20+
});
21+
22+
it('uses the server build for development in Node', () => {
23+
expect(resolvePackage('development')).toMatch(/\/dist\/server\.js$/);
24+
});
25+
});

src/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { render } from '@solidjs/web';
2-
import { pushServerFunctionCall } from './dev-toolbar/functions/tracker.js';
3-
import { DevToolbar } from './dev-toolbar/index.js';
2+
import * as serverFunctions from '@solidjs/web/server-functions';
3+
import {
4+
pushServerFunctionCall,
5+
type ServerFunctionCall,
6+
} from './dev-toolbar/functions/tracker.js';
7+
import { DevToolbar, type DevToolbarProps } from './dev-toolbar/index.js';
48

59
let dispose: (() => void) | undefined;
610
let frame: number | undefined;
711

8-
export { DevToolbar, pushServerFunctionCall };
12+
export { DevToolbar, type DevToolbarProps, pushServerFunctionCall, type ServerFunctionCall };
13+
14+
const observe = Reflect.get(serverFunctions, 'observeServerFunctionCalls');
15+
if (typeof observe === 'function') observe(pushServerFunctionCall);
916

1017
export function mountDevToolbar(): () => void {
1118
if (dispose) return dispose;

src/noop.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { ServerFunctionCall } from './dev-toolbar/functions/tracker.js';
2+
import type { DevToolbarProps } from './dev-toolbar/index.js';
3+
4+
export type { DevToolbarProps, ServerFunctionCall };
5+
6+
export function DevToolbar(props: DevToolbarProps) {
7+
return props.children;
8+
}
9+
10+
export function mountDevToolbar(): () => void {
11+
return () => {};
12+
}
13+
14+
export function pushServerFunctionCall(_event: ServerFunctionCall): void {}

src/server.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { pushServerFunctionCall } from './dev-toolbar/functions/tracker.js';
2-
import { DevToolbar } from './dev-toolbar/index.js';
1+
import {
2+
pushServerFunctionCall,
3+
type ServerFunctionCall,
4+
} from './dev-toolbar/functions/tracker.js';
5+
import { DevToolbar, type DevToolbarProps } from './dev-toolbar/index.js';
36

4-
export { DevToolbar, pushServerFunctionCall };
7+
export { DevToolbar, type DevToolbarProps, pushServerFunctionCall, type ServerFunctionCall };
58

69
export function mountDevToolbar(): () => void {
710
return () => {};

0 commit comments

Comments
 (0)