Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(solid): add support for devtools #10937

Merged
merged 12 commits into from
May 8, 2024
18 changes: 18 additions & 0 deletions .changeset/olive-bags-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@astrojs/solid-js": minor
---

Adds a `devtools` option

Enabling `devtools` will enable the [official Solid Devtools](https://github.com/thetarnav/solid-devtools) in development:
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

```js
import { defineConfig } from "astro/config"
import solid from "@astrojs/solid-js"

export default defineConfig({
integrations: [
solid({ devtools: true })
]
})
```
9 changes: 8 additions & 1 deletion packages/integrations/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@
"devDependencies": {
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"solid-js": "^1.8.17"
"solid-js": "^1.8.17",
"vite": "^5.2.10"
},
"peerDependencies": {
"solid-devtools": "^0.30.1",
"solid-js": "^1.8.5"
},
"peerDependenciesMeta": {
"solid-devtools": {
"optional": true
}
},
"engines": {
"node": "^18.17.1 || ^20.3.0 || >=21.0.0"
},
Expand Down
82 changes: 74 additions & 8 deletions packages/integrations/solid/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
import type { AstroConfig, AstroIntegration, AstroRenderer } from 'astro';
import type { AstroIntegration, AstroIntegrationLogger, AstroRenderer } from 'astro';
import solid, { type Options as ViteSolidPluginOptions } from 'vite-plugin-solid';
import type { UserConfig, PluginOption } from 'vite';

async function getViteConfiguration(isDev: boolean, { include, exclude }: Options = {}) {
// TODO: keep in sync with https://github.com/thetarnav/solid-devtools/blob/main/packages/main/src/vite/index.ts#L7
type DevtoolsPluginOptions = {
/** Add automatic name when creating signals, memos, stores, or mutables */
autoname?: boolean;
locator?:
| boolean
| {
/** Choose in which IDE the component source code should be revealed. */
targetIDE?: string;
/**
* Holding which key should enable the locator overlay?
* @default 'Alt'
*/
key?: string;
/** Inject location attributes to jsx templates */
jsxLocation?: boolean;
/** Inject location information to component declarations */
componentLocation?: boolean;
};
};
type DevtoolsPlugin = (_options?: DevtoolsPluginOptions) => PluginOption;

async function getDevtoolsPlugin(logger: AstroIntegrationLogger, retrieve: boolean) {
if (!retrieve) {
return null;
}

try {
// @ts-ignore
return (await import('solid-devtools/vite')).default as DevtoolsPlugin;
} catch (_) {
logger.warn(
'Solid Devtools requires `solid-devtools` as a peer dependency, add it to your project.'
);
return null;
}
}

async function getViteConfiguration(
isDev: boolean,
{ include, exclude }: Options,
devtoolsPlugin: DevtoolsPlugin | null
) {
// https://github.com/solidjs/vite-plugin-solid
// We inject the dev mode only if the user explicitly wants it or if we are in dev (serve) mode
const nestedDeps = ['solid-js', 'solid-js/web', 'solid-js/store', 'solid-js/html', 'solid-js/h'];
return {
const config: UserConfig = {
resolve: {
conditions: ['solid', ...(isDev ? ['development'] : [])],
dedupe: nestedDeps,
Expand Down Expand Up @@ -34,7 +77,13 @@ async function getViteConfiguration(isDev: boolean, { include, exclude }: Option
ssr: {
external: ['babel-preset-solid'],
},
} satisfies AstroConfig['vite'];
};

if (devtoolsPlugin) {
config.plugins?.push(devtoolsPlugin({ autoname: true }));
}

return config;
}

function getRenderer(): AstroRenderer {
Expand All @@ -45,17 +94,34 @@ function getRenderer(): AstroRenderer {
};
}

export type Options = Pick<ViteSolidPluginOptions, 'include' | 'exclude'>;
export interface Options extends Pick<ViteSolidPluginOptions, 'include' | 'exclude'> {
devtools?: boolean;
}

export default function (opts: Options = {}): AstroIntegration {
export default function (options: Options = {}): AstroIntegration {
return {
name: '@astrojs/solid-js',
hooks: {
'astro:config:setup': async ({ command, addRenderer, updateConfig }) => {
'astro:config:setup': async ({
command,
addRenderer,
updateConfig,
injectScript,
logger,
}) => {
const devtoolsPlugin = await getDevtoolsPlugin(
logger,
!!options.devtools && command === 'dev'
);

addRenderer(getRenderer());
updateConfig({
vite: await getViteConfiguration(command === 'dev', opts),
vite: await getViteConfiguration(command === 'dev', options, devtoolsPlugin),
});

if (devtoolsPlugin) {
injectScript('page', 'import "solid-devtools";');
}
},
},
};
Expand Down
Loading
Loading