Skip to content

Commit 40558dc

Browse files
authored
feat: svelte inspector updates and promote from experimental (#631)
* fix(inspector): move inspector host out of body to decrease risk of it getting removed by something overwriting body content * feat(inspector): promote from experimental to regular, warn users to update config * feat(inspector): enable inspector by default * feat(inspector): enable holdMode by default * fix(inspector): automatically disable on leave after a file has been opened * fix(inspector): prepend vite base when calling __openInEditor * feat(inspector): allow configuration via environment * fix: disable inspector in CI and use eager disable if found via env * fix: revert previous change, fix testcase and make sure inspector is only enabled for devserver * fix: don't enable inspector by default * fix(inspector): return void if no environment option is set * fix: remove extra check for serve mode, it is already covered by apply: serve * fix(inspector): remove keyup listener on destroy
1 parent 5202b0a commit 40558dc

File tree

15 files changed

+340
-176
lines changed

15 files changed

+340
-176
lines changed

.changeset/bright-fans-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
feat(inspector): Promote experimental.inspector to regular option

.changeset/cold-tips-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
feat(inspector): allow configuration via environment SVELTE_INSPECTOR_OPTIONS or SVELTE_INSPECTOR_TOGGLE

.changeset/funny-suits-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(inspector): prepend vite base when calling \_\_openInEditor

.changeset/green-rats-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(inspector): after a file has been opened, automatically disable inspector on leaving browser

.changeset/orange-lobsters-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(inspector): use control-shift as default keycombo on linux to avoid problems in firefox

.changeset/perfect-nails-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
feat(inspector): enable holdMode by default

.changeset/weak-crabs-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(svelte-inspector): mount outside body to avoid hydration claiming body removing it

docs/config.md

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -212,81 +212,16 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
212212

213213
See the [FAQ](./faq.md#what-is-going-on-with-vite-and-pre-bundling-dependencies) for details and how to fine-tune it for huge libraries.
214214

215-
## Experimental options
216-
217-
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
218-
219-
Either in Vite config:
220-
221-
```js
222-
// vite.config.js
223-
export default defineConfig({
224-
plugins: [
225-
svelte({
226-
experimental: {
227-
// experimental options
228-
}
229-
})
230-
]
231-
});
232-
```
233-
234-
or in Svelte config:
235-
236-
```js
237-
// svelte.config.js
238-
export default {
239-
vitePlugin: {
240-
experimental: {
241-
// experimental options
242-
}
243-
}
244-
};
245-
```
246-
247-
### dynamicCompileOptions
248-
249-
- **Type:**
250-
251-
```ts
252-
type DynamicCompileOptions = (data: {
253-
filename: string; // The file to be compiled
254-
code: string; // The preprocessed Svelte code
255-
compileOptions: Partial<CompileOptions>; // The current compiler options
256-
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
257-
```
258-
259-
A function to update the `compilerOptions` before compilation. To change part of the compiler options, return an object with the changes you need.
260-
261-
**Example:**
262-
263-
```js
264-
// vite.config.js
265-
export default defineConfig({
266-
plugins: [
267-
svelte({
268-
experimental: {
269-
dynamicCompileOptions({ filename, compileOptions }) {
270-
// Dynamically set hydration per Svelte file
271-
if (compileWithHydratable(filename) && !compileOptions.hydratable) {
272-
return { hydratable: true };
273-
}
274-
}
275-
}
276-
})
277-
]
278-
});
279-
```
280-
281215
### inspector
282216

283217
- **Type:**`InspectorOptions | boolean`
218+
- **Default:** `unset` for dev, always `false` for build
284219

285220
```ts
286221
interface InspectorOptions {
287222
/**
288223
* define a key combo to toggle inspector,
289-
* @default 'control-shift' on windows, 'meta-shift' on other os
224+
* @default 'meta-shift' on mac, 'control-shift' on other os
290225
*
291226
* any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
292227
* examples: control-shift, control-o, control-alt-s meta-x control-meta
@@ -318,7 +253,7 @@ export default {
318253

319254
/**
320255
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
321-
* @default false
256+
* @default true
322257
*/
323258
holdMode?: boolean;
324259

@@ -341,9 +276,94 @@ export default {
341276
}
342277
```
343278

344-
Set to `true` or customized `InspectorOptions` to enable svelte inspector during development.
279+
Set to `true` or options object to enable svelte inspector during development.
280+
281+
Inspector mode shows you the file location where the element under cursor is defined and you can click to quickly open your code editor at this location.
282+
283+
**Example:**
284+
285+
```js
286+
// vite.config.js
287+
export default defineConfig({
288+
plugins: [
289+
svelte({
290+
inspector: {
291+
toggleKeyCombo: 'meta-shift',
292+
holdMode: true,
293+
showToggleButton: 'always',
294+
toggleButtonPos: 'bottom-right'
295+
}
296+
})
297+
]
298+
});
299+
```
300+
301+
#### Customizing Inspector options via environment
302+
303+
Svelte Inspector toggle keys and other options are personal preferences. As such it isn't always convenient to define them in a shared svelte config file.
304+
To allow you to use your own setup, svelte inspector can be configured via environment variables, both from shell and dotenv files.
305+
306+
```shell
307+
# just keycombo, unquoted string
308+
SVELTE_INSPECTOR_TOGGLE=control-shift
309+
310+
# options object as json, all options except appendTo are supported
311+
SVELTE_INSPECTOR_OPTIONS='{"holdMode": false, "toggleButtonPos": "bottom-left"}'
312+
313+
# disable completely
314+
SVELTE_INSPECTOR_OPTIONS=false
315+
316+
# force default options
317+
SVELTE_INSPECTOR_OPTIONS=true
318+
```
345319

346-
When enabled, inspector mode shows you the file location where the element under cursor is defined and you can click to quickly open your code editor at this location.
320+
> Inspector options set on the environment take precedence over values set in svelte config and automatically enable svelte inspector during dev.
321+
322+
## Experimental options
323+
324+
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
325+
326+
Either in Vite config:
327+
328+
```js
329+
// vite.config.js
330+
export default defineConfig({
331+
plugins: [
332+
svelte({
333+
experimental: {
334+
// experimental options
335+
}
336+
})
337+
]
338+
});
339+
```
340+
341+
or in Svelte config:
342+
343+
```js
344+
// svelte.config.js
345+
export default {
346+
vitePlugin: {
347+
experimental: {
348+
// experimental options
349+
}
350+
}
351+
};
352+
```
353+
354+
### dynamicCompileOptions
355+
356+
- **Type:**
357+
358+
```ts
359+
type DynamicCompileOptions = (data: {
360+
filename: string; // The file to be compiled
361+
code: string; // The preprocessed Svelte code
362+
compileOptions: Partial<CompileOptions>; // The current compiler options
363+
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
364+
```
365+
366+
A function to update the `compilerOptions` before compilation. To change part of the compiler options, return an object with the changes you need.
347367

348368
**Example:**
349369

@@ -353,11 +373,11 @@ export default {
353373
plugins: [
354374
svelte({
355375
experimental: {
356-
inspector: {
357-
toggleKeyCombo: 'meta-shift',
358-
holdMode: true,
359-
showToggleButton: 'always',
360-
toggleButtonPos: 'bottom-right'
376+
dynamicCompileOptions({ filename, compileOptions }) {
377+
// Dynamically set hydration per Svelte file
378+
if (compileWithHydratable(filename) && !compileOptions.hydratable) {
379+
return { hydratable: true };
380+
}
361381
}
362382
}
363383
})

packages/e2e-tests/css-dev-sourcemap/__tests__/css-dev-sourcemap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('should apply css compiled from scss', async () => {
1616

1717
if (!isBuild) {
1818
test('should generate sourcemap', async () => {
19-
const style = await getText('style');
19+
const style = await getText('style[data-vite-dev-id*="App.svelte"]');
2020
const lines = style.split(`\n`).map((l) => l.trim());
2121
const css = lines[0];
2222
const mapComment = lines[1];

packages/vite-plugin-svelte/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
273273
);
274274
}
275275
}
276-
}
276+
},
277+
svelteInspector()
277278
];
278-
plugins.push(svelteInspector());
279-
return plugins.filter(Boolean);
279+
return plugins;
280280
}
281281

282282
export { vitePreprocess } from './preprocess';

0 commit comments

Comments
 (0)