Skip to content

Commit

Permalink
feat(solid): add support for devtools (#10937)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
florian-lefebvre and sarah11918 authored May 8, 2024
1 parent f0acd30 commit 7179930
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 322 deletions.
24 changes: 24 additions & 0 deletions .changeset/olive-bags-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@astrojs/solid-js": minor
---

Adds a `devtools` option

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:

```bash
npm install solid-devtools
# yarn add solid-devtools
# pnpm add solid-devtools
```

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

export default defineConfig({
integrations: [
solid({ devtools: true })
]
})
```
2 changes: 1 addition & 1 deletion packages/astro/test/sourcemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Sourcemap', async () => {

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

Expand Down
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

0 comments on commit 7179930

Please sign in to comment.