Skip to content

Commit 7179930

Browse files
feat(solid): add support for devtools (#10937)
Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent f0acd30 commit 7179930

File tree

5 files changed

+244
-322
lines changed

5 files changed

+244
-322
lines changed

.changeset/olive-bags-drive.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@astrojs/solid-js": minor
3+
---
4+
5+
Adds a `devtools` option
6+
7+
You can enable the [official Solid Devtools](https://github.com/thetarnav/solid-devtools) while working in development mode by setting `devtools: true` in your `solid()` integration config and adding `solid-devtools` to your project dependencies:
8+
9+
```bash
10+
npm install solid-devtools
11+
# yarn add solid-devtools
12+
# pnpm add solid-devtools
13+
```
14+
15+
```js
16+
import { defineConfig } from "astro/config"
17+
import solid from "@astrojs/solid-js"
18+
19+
export default defineConfig({
20+
integrations: [
21+
solid({ devtools: true })
22+
]
23+
})
24+
```

packages/astro/test/sourcemap.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Sourcemap', async () => {
1212

1313
it('Builds sourcemap', async () => {
1414
const dir = await fixture.readdir('./_astro');
15-
const counterMap = dir.find((file) => file.match(/^Counter\.\w+\.js\.map$/));
15+
const counterMap = dir.find((file) => file.match(/^Counter\.[\w-]+\.js\.map$/));
1616
assert.ok(counterMap);
1717
});
1818

packages/integrations/solid/package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@
4040
"devDependencies": {
4141
"astro": "workspace:*",
4242
"astro-scripts": "workspace:*",
43-
"solid-js": "^1.8.17"
43+
"solid-js": "^1.8.17",
44+
"vite": "^5.2.10"
4445
},
4546
"peerDependencies": {
47+
"solid-devtools": "^0.30.1",
4648
"solid-js": "^1.8.5"
4749
},
50+
"peerDependenciesMeta": {
51+
"solid-devtools": {
52+
"optional": true
53+
}
54+
},
4855
"engines": {
4956
"node": "^18.17.1 || ^20.3.0 || >=21.0.0"
5057
},

packages/integrations/solid/src/index.ts

+74-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
1-
import type { AstroConfig, AstroIntegration, AstroRenderer } from 'astro';
1+
import type { AstroIntegration, AstroIntegrationLogger, AstroRenderer } from 'astro';
22
import solid, { type Options as ViteSolidPluginOptions } from 'vite-plugin-solid';
3+
import type { UserConfig, PluginOption } from 'vite';
34

4-
async function getViteConfiguration(isDev: boolean, { include, exclude }: Options = {}) {
5+
// TODO: keep in sync with https://github.com/thetarnav/solid-devtools/blob/main/packages/main/src/vite/index.ts#L7
6+
type DevtoolsPluginOptions = {
7+
/** Add automatic name when creating signals, memos, stores, or mutables */
8+
autoname?: boolean;
9+
locator?:
10+
| boolean
11+
| {
12+
/** Choose in which IDE the component source code should be revealed. */
13+
targetIDE?: string;
14+
/**
15+
* Holding which key should enable the locator overlay?
16+
* @default 'Alt'
17+
*/
18+
key?: string;
19+
/** Inject location attributes to jsx templates */
20+
jsxLocation?: boolean;
21+
/** Inject location information to component declarations */
22+
componentLocation?: boolean;
23+
};
24+
};
25+
type DevtoolsPlugin = (_options?: DevtoolsPluginOptions) => PluginOption;
26+
27+
async function getDevtoolsPlugin(logger: AstroIntegrationLogger, retrieve: boolean) {
28+
if (!retrieve) {
29+
return null;
30+
}
31+
32+
try {
33+
// @ts-ignore
34+
return (await import('solid-devtools/vite')).default as DevtoolsPlugin;
35+
} catch (_) {
36+
logger.warn(
37+
'Solid Devtools requires `solid-devtools` as a peer dependency, add it to your project.'
38+
);
39+
return null;
40+
}
41+
}
42+
43+
async function getViteConfiguration(
44+
isDev: boolean,
45+
{ include, exclude }: Options,
46+
devtoolsPlugin: DevtoolsPlugin | null
47+
) {
548
// https://github.com/solidjs/vite-plugin-solid
649
// We inject the dev mode only if the user explicitly wants it or if we are in dev (serve) mode
750
const nestedDeps = ['solid-js', 'solid-js/web', 'solid-js/store', 'solid-js/html', 'solid-js/h'];
8-
return {
51+
const config: UserConfig = {
952
resolve: {
1053
conditions: ['solid', ...(isDev ? ['development'] : [])],
1154
dedupe: nestedDeps,
@@ -34,7 +77,13 @@ async function getViteConfiguration(isDev: boolean, { include, exclude }: Option
3477
ssr: {
3578
external: ['babel-preset-solid'],
3679
},
37-
} satisfies AstroConfig['vite'];
80+
};
81+
82+
if (devtoolsPlugin) {
83+
config.plugins?.push(devtoolsPlugin({ autoname: true }));
84+
}
85+
86+
return config;
3887
}
3988

4089
function getRenderer(): AstroRenderer {
@@ -45,17 +94,34 @@ function getRenderer(): AstroRenderer {
4594
};
4695
}
4796

48-
export type Options = Pick<ViteSolidPluginOptions, 'include' | 'exclude'>;
97+
export interface Options extends Pick<ViteSolidPluginOptions, 'include' | 'exclude'> {
98+
devtools?: boolean;
99+
}
49100

50-
export default function (opts: Options = {}): AstroIntegration {
101+
export default function (options: Options = {}): AstroIntegration {
51102
return {
52103
name: '@astrojs/solid-js',
53104
hooks: {
54-
'astro:config:setup': async ({ command, addRenderer, updateConfig }) => {
105+
'astro:config:setup': async ({
106+
command,
107+
addRenderer,
108+
updateConfig,
109+
injectScript,
110+
logger,
111+
}) => {
112+
const devtoolsPlugin = await getDevtoolsPlugin(
113+
logger,
114+
!!options.devtools && command === 'dev'
115+
);
116+
55117
addRenderer(getRenderer());
56118
updateConfig({
57-
vite: await getViteConfiguration(command === 'dev', opts),
119+
vite: await getViteConfiguration(command === 'dev', options, devtoolsPlugin),
58120
});
121+
122+
if (devtoolsPlugin) {
123+
injectScript('page', 'import "solid-devtools";');
124+
}
59125
},
60126
},
61127
};

0 commit comments

Comments
 (0)