Skip to content

Commit

Permalink
refactor: move inspector to separate package (#646)
Browse files Browse the repository at this point in the history
Co-authored-by: dominikg <[email protected]>
  • Loading branch information
bluwy and dominikg authored May 17, 2023
1 parent 5e9518b commit 1dd6933
Show file tree
Hide file tree
Showing 22 changed files with 392 additions and 211 deletions.
6 changes: 6 additions & 0 deletions .changeset/fresh-knives-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/vite-plugin-svelte': minor
'@sveltejs/vite-plugin-svelte-inspector': patch
---

Refactor Svelte inspector as a separate package
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ module.exports = {
}
},
{
files: ['packages/vite-plugin-svelte/src/ui/inspector/load-inspector.js'],
files: ['packages/vite-plugin-svelte-inspector/src/runtime/load-inspector.js'],
env: {
browser: true
}
},
{
files: ['**/*.d.ts'],
rules: {
'no-unused-vars': 'off'
}
},
{
files: ['packages/e2e-tests/**', 'packages/playground/**'],
rules: {
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ jobs:
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'
- name: install
if: matrix.node != 14
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
- name: install for node14
if: matrix.node == 14
run: pnpm install --no-frozen-lockfile --prefer-offline --ignore-scripts
- name: build
run: pnpm build:ci
- name: install playwright chromium
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export default defineConfig({
## Documentation

- [Plugin options](./docs/config.md)
- [Svelte Inspector](./docs/inspector.md)
- [FAQ](./docs/faq.md)

## Packages

| Package | Changelog |
| ----------------------------------------------------------- | ----------------------------------------------------- |
| [@sveltejs/vite-plugin-svelte](packages/vite-plugin-svelte) | [Changelog](packages/vite-plugin-svelte/CHANGELOG.md) |
| Package | Changelog |
| ------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| [@sveltejs/vite-plugin-svelte](packages/vite-plugin-svelte) | [Changelog](packages/vite-plugin-svelte/CHANGELOG.md) |
| [@sveltejs/vite-plugin-svelte-inspector](packages/vite-plugin-svelte-inspector) | [Changelog](packages/vite-plugin-svelte-inspector/CHANGELOG.md) |

## Got a question? / Need help?

Expand Down
102 changes: 2 additions & 100 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,111 +214,13 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt

### inspector

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

```ts
interface InspectorOptions {
/**
* define a key combo to toggle inspector,
* @default 'meta-shift' on mac, 'control-shift' on other os
*
* any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
* examples: control-shift, control-o, control-alt-s meta-x control-meta
* Some keys have native behavior (e.g. alt-s opens history menu on firefox).
* To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
*/
toggleKeyCombo?: string;

/**
* define keys to select elements with via keyboard
* @default {parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }
*
* improves accessibility and also helps when you want to select elements that do not have a hoverable surface area
* due to tight wrapping
*
* parent: select closest parent
* child: select first child (or grandchild)
* next: next sibling (or parent if no next sibling exists)
* prev: previous sibling (or parent if no prev sibling exists)
*/
navKeys?: { parent: string; child: string; next: string; prev: string };

/**
* define key to open the editor for the currently selected dom node
*
* @default 'Enter'
*/
openKey?: string;

/**
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
* @default true
*/
holdMode?: boolean;

/**
* when to show the toggle button
* @default 'active'
*/
showToggleButton?: 'always' | 'active' | 'never';

/**
* where to display the toggle button
* @default top-right
*/
toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';

/**
* inject custom styles when inspector is active
*/
customStyles?: boolean;
}
```

Set to `true` or options object to enable svelte inspector during development.
Set to `true` or options object to enable svelte inspector during development. See the [inspector docs](./inspector.md) for the full configuration options.

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.

**Example:**

```js
// vite.config.js
export default defineConfig({
plugins: [
svelte({
inspector: {
toggleKeyCombo: 'meta-shift',
holdMode: true,
showToggleButton: 'always',
toggleButtonPos: 'bottom-right'
}
})
]
});
```

#### Customizing Inspector options via environment

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.
To allow you to use your own setup, svelte inspector can be configured via environment variables, both from shell and dotenv files.

```shell
# just keycombo, unquoted string
SVELTE_INSPECTOR_TOGGLE=control-shift

# options object as json, all options except appendTo are supported
SVELTE_INSPECTOR_OPTIONS='{"holdMode": false, "toggleButtonPos": "bottom-left"}'

# disable completely
SVELTE_INSPECTOR_OPTIONS=false

# force default options
SVELTE_INSPECTOR_OPTIONS=true
```

> Inspector options set on the environment take precedence over values set in svelte config and automatically enable svelte inspector during dev.
## Experimental options

These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
Expand Down
110 changes: 110 additions & 0 deletions docs/inspector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Inspector

`@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.

Note that `@sveltejs/vite-plugin-svelte` needs to be installed as a peer dependency as the inspector brings in Svelte components to be compiled.

## Setup

### with Svelte config

```js
// svelte.config.js
export default {
vitePlugin: {
// set to true for defaults or customize with object
inspector: {
toggleKeyCombo: 'meta-shift',
showToggleButton: 'always',
toggleButtonPos: 'bottom-right'
}
}
};
```

### with environment variables

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.
To allow you to use your own setup, svelte inspector can be configured via environment variables, both from shell and dotenv files.

```shell
# just keycombo, unquoted string
SVELTE_INSPECTOR_TOGGLE=control-shift

# options object as json
SVELTE_INSPECTOR_OPTIONS='{"holdMode": false, "toggleButtonPos": "bottom-left"}'

# disable completely
SVELTE_INSPECTOR_OPTIONS=false

# force default options
SVELTE_INSPECTOR_OPTIONS=true
```

> Inspector options set on the environment take precedence over values set in svelte config and automatically enable svelte inspector during dev.
## Plugin options

### toggleKeyCombo

- **Type:** `string`
- **Default:** `'meta-shift'` on mac, `'control-shift'` on other os

Define a key combo to toggle inspector.

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).

Examples: `control-shift`, `control-o`, `control-alt-s`, `meta-x`, `control-meta`.

### navKeys

- **Type:** `{ parent: string; child: string; next: string; prev: string }`
- **Default:** `{ parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }`

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.

- `parent`: select closest parent
- `child`: select first child (or grandchild)
- `next`: next sibling (or parent if no next sibling exists)
- `prev`: previous sibling (or parent if no prev sibling exists)

### openKey

- **Type:** `string`
- **Default:** `'Enter'`

Define key to open the editor for the currently selected dom node.

### holdMode

- **Type:** `boolean`
- **Default:** `true`

Inspector will only open when the `toggleKeyCombo` is held down, and close when released.

### showToggleButton

- **Type:** `'always' | 'active' | 'never'`
- **Default:** `'active'`

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`.

- `'always'`: always show the toggle button
- `'active'`: show the toggle button when the inspector is active
- `'never'`: never show the toggle button

### toggleButtonPos

- **Type:** `'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'`
- **Default:** `'top-right'`

Where to display the toggle button.

### customStyles

- **Type:** `boolean`
- **Default:** `true`

Inject custom styles when inspector is active. This is useful if you want to customize the inspector styles to match your app.

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).
21 changes: 21 additions & 0 deletions packages/vite-plugin-svelte-inspector/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 [these people](https://github.com/sveltejs/vite-plugin-svelte/graphs/contributors)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions packages/vite-plugin-svelte-inspector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @sveltejs/vite-plugin-svelte-inspector

A [Svelte](https://svelte.dev) inspector plugin for [Vite](https://vitejs.dev).

## Usage

```js
// vite.config.js
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';

export default defineConfig({
plugins: [
// the svelte plugin is required to work
svelte(),
svelteInspector({
/* plugin options */
})
]
});
```

## License

[MIT](./LICENSE)
47 changes: 47 additions & 0 deletions packages/vite-plugin-svelte-inspector/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@sveltejs/vite-plugin-svelte-inspector",
"version": "1.0.0",
"license": "MIT",
"author": "dominikg",
"files": [
"src"
],
"type": "module",
"types": "src/index.d.ts",
"exports": {
".": {
"types": "./src/index.d.ts",
"import": "./src/index.js"
}
},
"engines": {
"node": "^14.18.0 || >= 16"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sveltejs/vite-plugin-svelte.git",
"directory": "packages/vite-plugin-svelte-inspector"
},
"keywords": [
"vite-plugin",
"vite plugin",
"vite",
"svelte"
],
"bugs": {
"url": "https://github.com/sveltejs/vite-plugin-svelte/issues"
},
"homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
"dependencies": {
"debug": "^4.3.4"
},
"peerDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.2.0",
"svelte": "^3.54.0",
"vite": "^4.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"svelte": "^3.59.1"
}
}
3 changes: 3 additions & 0 deletions packages/vite-plugin-svelte-inspector/src/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import _debug from 'debug';

export const debug = _debug('vite-plugin-svelte-inspector');
Loading

0 comments on commit 1dd6933

Please sign in to comment.