1
- import type { AstroConfig , AstroIntegration , AstroRenderer } from 'astro' ;
1
+ import type { AstroIntegration , AstroIntegrationLogger , AstroRenderer } from 'astro' ;
2
2
import solid , { type Options as ViteSolidPluginOptions } from 'vite-plugin-solid' ;
3
+ import type { UserConfig , PluginOption } from 'vite' ;
3
4
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
+ ) {
5
48
// https://github.com/solidjs/vite-plugin-solid
6
49
// We inject the dev mode only if the user explicitly wants it or if we are in dev (serve) mode
7
50
const nestedDeps = [ 'solid-js' , 'solid-js/web' , 'solid-js/store' , 'solid-js/html' , 'solid-js/h' ] ;
8
- return {
51
+ const config : UserConfig = {
9
52
resolve : {
10
53
conditions : [ 'solid' , ...( isDev ? [ 'development' ] : [ ] ) ] ,
11
54
dedupe : nestedDeps ,
@@ -34,7 +77,13 @@ async function getViteConfiguration(isDev: boolean, { include, exclude }: Option
34
77
ssr : {
35
78
external : [ 'babel-preset-solid' ] ,
36
79
} ,
37
- } satisfies AstroConfig [ 'vite' ] ;
80
+ } ;
81
+
82
+ if ( devtoolsPlugin ) {
83
+ config . plugins ?. push ( devtoolsPlugin ( { autoname : true } ) ) ;
84
+ }
85
+
86
+ return config ;
38
87
}
39
88
40
89
function getRenderer ( ) : AstroRenderer {
@@ -45,17 +94,34 @@ function getRenderer(): AstroRenderer {
45
94
} ;
46
95
}
47
96
48
- export type Options = Pick < ViteSolidPluginOptions , 'include' | 'exclude' > ;
97
+ export interface Options extends Pick < ViteSolidPluginOptions , 'include' | 'exclude' > {
98
+ devtools ?: boolean ;
99
+ }
49
100
50
- export default function ( opts : Options = { } ) : AstroIntegration {
101
+ export default function ( options : Options = { } ) : AstroIntegration {
51
102
return {
52
103
name : '@astrojs/solid-js' ,
53
104
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
+
55
117
addRenderer ( getRenderer ( ) ) ;
56
118
updateConfig ( {
57
- vite : await getViteConfiguration ( command === 'dev' , opts ) ,
119
+ vite : await getViteConfiguration ( command === 'dev' , options , devtoolsPlugin ) ,
58
120
} ) ;
121
+
122
+ if ( devtoolsPlugin ) {
123
+ injectScript ( 'page' , 'import "solid-devtools";' ) ;
124
+ }
59
125
} ,
60
126
} ,
61
127
} ;
0 commit comments