Skip to content

Commit 1dd6933

Browse files
bluwydominikg
andauthored
refactor: move inspector to separate package (#646)
Co-authored-by: dominikg <[email protected]>
1 parent 5e9518b commit 1dd6933

File tree

22 files changed

+392
-211
lines changed

22 files changed

+392
-211
lines changed

.changeset/fresh-knives-reflect.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
'@sveltejs/vite-plugin-svelte-inspector': patch
4+
---
5+
6+
Refactor Svelte inspector as a separate package

.eslintrc.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,17 @@ module.exports = {
7070
}
7171
},
7272
{
73-
files: ['packages/vite-plugin-svelte/src/ui/inspector/load-inspector.js'],
73+
files: ['packages/vite-plugin-svelte-inspector/src/runtime/load-inspector.js'],
7474
env: {
7575
browser: true
7676
}
7777
},
78+
{
79+
files: ['**/*.d.ts'],
80+
rules: {
81+
'no-unused-vars': 'off'
82+
}
83+
},
7884
{
7985
files: ['packages/e2e-tests/**', 'packages/playground/**'],
8086
rules: {

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ jobs:
106106
cache: 'pnpm'
107107
cache-dependency-path: '**/pnpm-lock.yaml'
108108
- name: install
109+
if: matrix.node != 14
109110
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
111+
- name: install for node14
112+
if: matrix.node == 14
113+
run: pnpm install --no-frozen-lockfile --prefer-offline --ignore-scripts
110114
- name: build
111115
run: pnpm build:ci
112116
- name: install playwright chromium

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export default defineConfig({
3131
## Documentation
3232

3333
- [Plugin options](./docs/config.md)
34+
- [Svelte Inspector](./docs/inspector.md)
3435
- [FAQ](./docs/faq.md)
3536

3637
## Packages
3738

38-
| Package | Changelog |
39-
| ----------------------------------------------------------- | ----------------------------------------------------- |
40-
| [@sveltejs/vite-plugin-svelte](packages/vite-plugin-svelte) | [Changelog](packages/vite-plugin-svelte/CHANGELOG.md) |
39+
| Package | Changelog |
40+
| ------------------------------------------------------------------------------- | --------------------------------------------------------------- |
41+
| [@sveltejs/vite-plugin-svelte](packages/vite-plugin-svelte) | [Changelog](packages/vite-plugin-svelte/CHANGELOG.md) |
42+
| [@sveltejs/vite-plugin-svelte-inspector](packages/vite-plugin-svelte-inspector) | [Changelog](packages/vite-plugin-svelte-inspector/CHANGELOG.md) |
4143

4244
## Got a question? / Need help?
4345

docs/config.md

+2-100
Original file line numberDiff line numberDiff line change
@@ -214,111 +214,13 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
214214

215215
### inspector
216216

217-
- **Type:**`InspectorOptions | boolean`
217+
- **Type:** `InspectorOptions | boolean`
218218
- **Default:** `unset` for dev, always `false` for build
219219

220-
```ts
221-
interface InspectorOptions {
222-
/**
223-
* define a key combo to toggle inspector,
224-
* @default 'meta-shift' on mac, 'control-shift' on other os
225-
*
226-
* any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
227-
* examples: control-shift, control-o, control-alt-s meta-x control-meta
228-
* Some keys have native behavior (e.g. alt-s opens history menu on firefox).
229-
* To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
230-
*/
231-
toggleKeyCombo?: string;
232-
233-
/**
234-
* define keys to select elements with via keyboard
235-
* @default {parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }
236-
*
237-
* improves accessibility and also helps when you want to select elements that do not have a hoverable surface area
238-
* due to tight wrapping
239-
*
240-
* parent: select closest parent
241-
* child: select first child (or grandchild)
242-
* next: next sibling (or parent if no next sibling exists)
243-
* prev: previous sibling (or parent if no prev sibling exists)
244-
*/
245-
navKeys?: { parent: string; child: string; next: string; prev: string };
246-
247-
/**
248-
* define key to open the editor for the currently selected dom node
249-
*
250-
* @default 'Enter'
251-
*/
252-
openKey?: string;
253-
254-
/**
255-
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
256-
* @default true
257-
*/
258-
holdMode?: boolean;
259-
260-
/**
261-
* when to show the toggle button
262-
* @default 'active'
263-
*/
264-
showToggleButton?: 'always' | 'active' | 'never';
265-
266-
/**
267-
* where to display the toggle button
268-
* @default top-right
269-
*/
270-
toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
271-
272-
/**
273-
* inject custom styles when inspector is active
274-
*/
275-
customStyles?: boolean;
276-
}
277-
```
278-
279-
Set to `true` or options object to enable svelte inspector during development.
220+
Set to `true` or options object to enable svelte inspector during development. See the [inspector docs](./inspector.md) for the full configuration options.
280221

281222
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.
282223

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-
```
319-
320-
> Inspector options set on the environment take precedence over values set in svelte config and automatically enable svelte inspector during dev.
321-
322224
## Experimental options
323225

324226
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.

docs/inspector.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Inspector
2+
3+
`@sveltejs/vite-plugin-svelte-inspector` is a Vite plugin that adds a Svelte inspector in the browser. It shows the file location where the element under cursor is defined and you can click to quickly open your code editor at this location.
4+
5+
Note that `@sveltejs/vite-plugin-svelte` needs to be installed as a peer dependency as the inspector brings in Svelte components to be compiled.
6+
7+
## Setup
8+
9+
### with Svelte config
10+
11+
```js
12+
// svelte.config.js
13+
export default {
14+
vitePlugin: {
15+
// set to true for defaults or customize with object
16+
inspector: {
17+
toggleKeyCombo: 'meta-shift',
18+
showToggleButton: 'always',
19+
toggleButtonPos: 'bottom-right'
20+
}
21+
}
22+
};
23+
```
24+
25+
### with environment variables
26+
27+
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.
28+
To allow you to use your own setup, svelte inspector can be configured via environment variables, both from shell and dotenv files.
29+
30+
```shell
31+
# just keycombo, unquoted string
32+
SVELTE_INSPECTOR_TOGGLE=control-shift
33+
34+
# options object as json
35+
SVELTE_INSPECTOR_OPTIONS='{"holdMode": false, "toggleButtonPos": "bottom-left"}'
36+
37+
# disable completely
38+
SVELTE_INSPECTOR_OPTIONS=false
39+
40+
# force default options
41+
SVELTE_INSPECTOR_OPTIONS=true
42+
```
43+
44+
> Inspector options set on the environment take precedence over values set in svelte config and automatically enable svelte inspector during dev.
45+
46+
## Plugin options
47+
48+
### toggleKeyCombo
49+
50+
- **Type:** `string`
51+
- **Default:** `'meta-shift'` on mac, `'control-shift'` on other os
52+
53+
Define a key combo to toggle inspector.
54+
55+
The value is recommended to be any number of modifiers (e.g. `control`, `shift`, `alt`, `meta`) followed by zero or one regular key, separated by `-`. This helps avoid conflicts or accidentally typing into inputs. Note that some keys have native behavior (e.g. `alt-s` opens history menu on firefox).
56+
57+
Examples: `control-shift`, `control-o`, `control-alt-s`, `meta-x`, `control-meta`.
58+
59+
### navKeys
60+
61+
- **Type:** `{ parent: string; child: string; next: string; prev: string }`
62+
- **Default:** `{ parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }`
63+
64+
Define keys to select elements with via keyboard. This improves accessibility and helps selecting elements that do not have a hoverable surface area due to tight wrapping.
65+
66+
- `parent`: select closest parent
67+
- `child`: select first child (or grandchild)
68+
- `next`: next sibling (or parent if no next sibling exists)
69+
- `prev`: previous sibling (or parent if no prev sibling exists)
70+
71+
### openKey
72+
73+
- **Type:** `string`
74+
- **Default:** `'Enter'`
75+
76+
Define key to open the editor for the currently selected dom node.
77+
78+
### holdMode
79+
80+
- **Type:** `boolean`
81+
- **Default:** `true`
82+
83+
Inspector will only open when the `toggleKeyCombo` is held down, and close when released.
84+
85+
### showToggleButton
86+
87+
- **Type:** `'always' | 'active' | 'never'`
88+
- **Default:** `'active'`
89+
90+
When to show the toggle button. The toggle button allows you to click on-screen to enable/disable the inspector, rather than using the `toggleKeyCombo`.
91+
92+
- `'always'`: always show the toggle button
93+
- `'active'`: show the toggle button when the inspector is active
94+
- `'never'`: never show the toggle button
95+
96+
### toggleButtonPos
97+
98+
- **Type:** `'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'`
99+
- **Default:** `'top-right'`
100+
101+
Where to display the toggle button.
102+
103+
### customStyles
104+
105+
- **Type:** `boolean`
106+
- **Default:** `true`
107+
108+
Inject custom styles when inspector is active. This is useful if you want to customize the inspector styles to match your app.
109+
110+
When the inspector is active, the `svelte-inspector-enabled` class is added to the `body` element, and the `svelte-inspector-active-target` class is added to the current active target (e.g. via hover or keyboard).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 [these people](https://github.com/sveltejs/vite-plugin-svelte/graphs/contributors)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @sveltejs/vite-plugin-svelte-inspector
2+
3+
A [Svelte](https://svelte.dev) inspector plugin for [Vite](https://vitejs.dev).
4+
5+
## Usage
6+
7+
```js
8+
// vite.config.js
9+
import { defineConfig } from 'vite';
10+
import { svelte } from '@sveltejs/vite-plugin-svelte';
11+
import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
12+
13+
export default defineConfig({
14+
plugins: [
15+
// the svelte plugin is required to work
16+
svelte(),
17+
svelteInspector({
18+
/* plugin options */
19+
})
20+
]
21+
});
22+
```
23+
24+
## License
25+
26+
[MIT](./LICENSE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@sveltejs/vite-plugin-svelte-inspector",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"author": "dominikg",
6+
"files": [
7+
"src"
8+
],
9+
"type": "module",
10+
"types": "src/index.d.ts",
11+
"exports": {
12+
".": {
13+
"types": "./src/index.d.ts",
14+
"import": "./src/index.js"
15+
}
16+
},
17+
"engines": {
18+
"node": "^14.18.0 || >= 16"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/sveltejs/vite-plugin-svelte.git",
23+
"directory": "packages/vite-plugin-svelte-inspector"
24+
},
25+
"keywords": [
26+
"vite-plugin",
27+
"vite plugin",
28+
"vite",
29+
"svelte"
30+
],
31+
"bugs": {
32+
"url": "https://github.com/sveltejs/vite-plugin-svelte/issues"
33+
},
34+
"homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
35+
"dependencies": {
36+
"debug": "^4.3.4"
37+
},
38+
"peerDependencies": {
39+
"@sveltejs/vite-plugin-svelte": "^2.2.0",
40+
"svelte": "^3.54.0",
41+
"vite": "^4.0.0"
42+
},
43+
"devDependencies": {
44+
"@types/debug": "^4.1.7",
45+
"svelte": "^3.59.1"
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import _debug from 'debug';
2+
3+
export const debug = _debug('vite-plugin-svelte-inspector');

0 commit comments

Comments
 (0)