提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.3 KiB

  1. <p align="center">
  2. <a href="https://github.com/webfansplz/vite-plugin-vue-inspector"><img src="./logo.svg" width="180" alt="vite-plugin-vue-inspector"></a>
  3. </p>
  4. <p align="center">
  5. <a href="https://www.npmjs.com/package/vite-plugin-vue-inspector" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/npm/v/vite-plugin-vue-inspector" alt="NPM Version" /></a>
  6. <a href="https://www.npmjs.com/package/vite-plugin-vue-inspector" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/npm/dt/vite-plugin-vue-inspector" alt="NPM Downloads" /></a>
  7. <a href="https://github.com/webfansplz/vite-plugin-vue-inspector/blob/master/LICENSE" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/github/license/webfansplz/vite-plugin-vue-inspector" alt="License" /></a>
  8. </p>
  9. <p align="center">
  10. <a href="https://stackblitz.com/edit/vitejs-vite-rbr2as?file=src%2FApp.vue"><img src="https://developer.stackblitz.com/img/open_in_stackblitz.svg" alt=""></a>
  11. </p>
  12. ## 📖 Introduction
  13. A vite plugin which provides the ability that to jump to the local IDE when you click the element of browser automatically. It supports Vue2 & 3 & SSR.
  14. <p align="center">
  15. <img src="./public/preview.gif" alt="vite-plugin-vue-inspector">
  16. </p>
  17. ## 📦 Installation
  18. ```bash
  19. # vite-plugin-vue-inspector
  20. pnpm install vite-plugin-vue-inspector -D
  21. # unplugin-vue-inspector
  22. pnpm install unplugin-vue-inspector -D
  23. ```
  24. ## 🦄 Usage
  25. ### Configuration Vite
  26. ```ts
  27. // for Vue2
  28. import { defineConfig, } from 'vite'
  29. import { createVuePlugin, } from 'vite-plugin-vue2'
  30. import Inspector from 'unplugin-vue-inspector/vite' // OR vite-plugin-vue-inspector
  31. export default defineConfig({
  32. plugins: [
  33. createVuePlugin(),
  34. Inspector({
  35. vue: 2
  36. }),
  37. ],
  38. })
  39. ```
  40. ```ts
  41. // for Vue3
  42. import { defineConfig } from 'vite'
  43. import Vue from '@vitejs/plugin-vue'
  44. import Inspector from 'unplugin-vue-inspector/vite' // OR vite-plugin-vue-inspector
  45. export default defineConfig({
  46. plugins: [Vue(), Inspector()],
  47. })
  48. ```
  49. ```ts
  50. // for Nuxt3
  51. // nuxt.config.ts
  52. import { defineNuxtConfig } from 'nuxt/config'
  53. import Inspector from 'vite-plugin-vue-inspector'
  54. export default defineNuxtConfig({
  55. modules: [
  56. ['unplugin-vue-inspector/nuxt', {
  57. enabled: true,
  58. toggleButtonVisibility: 'always',
  59. }],
  60. ],
  61. })
  62. ```
  63. ### Options
  64. ```ts
  65. interface VitePluginInspectorOptions {
  66. /**
  67. * Vue version
  68. * @default 3
  69. */
  70. vue?: 2 | 3
  71. /**
  72. * Default enable state
  73. * @default false
  74. */
  75. enabled?: boolean
  76. /**
  77. * Define a combo key to toggle inspector
  78. * @default 'control-shift' on windows, 'meta-shift' on other os
  79. *
  80. * any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
  81. * examples: control-shift, control-o, control-alt-s meta-x control-meta
  82. * Some keys have native behavior (e.g. alt-s opens history menu on firefox).
  83. * To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
  84. * You can also disable it by setting `false`.
  85. */
  86. toggleComboKey?: string | false
  87. /**
  88. * Toggle button visibility
  89. * @default 'active'
  90. */
  91. toggleButtonVisibility?: 'always' | 'active' | 'never'
  92. /**
  93. * Toggle button display position
  94. * @default top-right
  95. */
  96. toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
  97. /**
  98. * append an import to the module id ending with `appendTo` instead of adding a script into body
  99. * useful for frameworks that do not support transformIndexHtml hook (e.g. Nuxt3)
  100. *
  101. * WARNING: only set this if you know exactly what it does.
  102. */
  103. appendTo?: string | RegExp
  104. /**
  105. * Customize openInEditor host (e.g. http://localhost:3000)
  106. * @default false
  107. * @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
  108. */
  109. openInEditorHost?: string | false
  110. /**
  111. * lazy load inspector times (ms)
  112. * @default false
  113. */
  114. lazyLoad?: number | false
  115. /**
  116. * disable inspector on editor open
  117. * @default false
  118. */
  119. disableInspectorOnEditorOpen?: boolean
  120. /**
  121. * Hide information in VNode and produce clean html in DevTools
  122. *
  123. * Currently, it only works for Vue 3
  124. *
  125. * @default true
  126. */
  127. cleanHtml?: boolean
  128. /**
  129. * Target editor when open in editor (v5.1.0+)
  130. *
  131. * @default code (Visual Studio Code)
  132. */
  133. launchEditor?: 'appcode' | 'atom' | 'atom-beta' | 'brackets' | 'clion' | 'code' | 'code-insiders' | 'codium' | 'emacs' | 'idea' | 'notepad++' | 'pycharm' | 'phpstorm' | 'rubymine' | 'sublime' | 'vim' | 'visualstudio' | 'webstorm' | 'cursor'
  134. }
  135. ```
  136. ### Example
  137. - [Vue2](https://github.com/webfansplz/vite-plugin-vue-inspector/tree/main/packages/playground/vue2)
  138. - [Vue3](https://github.com/webfansplz/vite-plugin-vue-inspector/tree/main/packages/playground/vue3)
  139. - [Nuxt3](https://github.com/webfansplz/vite-plugin-vue-inspector/tree/main/packages/playground/nuxt)
  140. ## Supported editors
  141. | Value | Editor | Linux | Windows | OSX |
  142. |--------|------|:------:|:------:|:------:|
  143. | `appcode` | [AppCode](https://www.jetbrains.com/objc/) | | |✓|
  144. | `atom` | [Atom](https://atom.io/) |✓|✓|✓|
  145. | `atom-beta` | [Atom Beta](https://atom.io/beta) | | |✓|
  146. | `brackets` | [Brackets](http://brackets.io/) |✓|✓|✓|
  147. | `clion` | [Clion](https://www.jetbrains.com/clion/) | |✓|✓|
  148. | `code` | [Visual Studio Code](https://code.visualstudio.com/) |✓|✓|✓|
  149. | `code-insiders` | [Visual Studio Code Insiders](https://code.visualstudio.com/insiders/) |✓|✓|✓|
  150. | `codium` | [VSCodium](https://github.com/VSCodium/vscodium) |✓|✓|✓|
  151. | `emacs` | [Emacs](https://www.gnu.org/software/emacs/) |✓| | |
  152. | `idea` | [IDEA](https://www.jetbrains.com/idea/) |✓|✓|✓|
  153. | `notepad++` | [Notepad++](https://notepad-plus-plus.org/download/v7.5.4.html) | |✓| |
  154. | `pycharm` | [PyCharm](https://www.jetbrains.com/pycharm/) |✓|✓|✓|
  155. | `phpstorm` | [PhpStorm](https://www.jetbrains.com/phpstorm/) |✓|✓|✓|
  156. | `rubymine` | [RubyMine](https://www.jetbrains.com/ruby/) |✓|✓|✓|
  157. | `sublime` | [Sublime Text](https://www.sublimetext.com/) |✓|✓|✓|
  158. | `vim` | [Vim](http://www.vim.org/) |✓| | |
  159. | `visualstudio` | [Visual Studio](https://www.visualstudio.com/vs/) | | |✓|
  160. | `webstorm` | [WebStorm](https://www.jetbrains.com/webstorm/) |✓|✓|✓|
  161. | `cursor` | [Cursor](https://www.cursor.com/) |✓|✓|✓|
  162. ## 🔌 Configuration IDE / Editor
  163. **Starting from v5.1.0, We recommend using the `launchEditor` option configuration to specify the IDE** (Please ensure that the editor's environment variables are correctly configured beforehand.)
  164. It uses an **environment variable** named **`LAUNCH_EDITOR`** to specify an IDE application, but if you do not set this variable, it will try to open a common IDE that you have open or installed once it is certified.
  165. For example, if you want it always open VS Code when inspection clicked, set `export LAUNCH_EDITOR=code` in your shell.
  166. ### VS Code
  167. - install VS Code command line tools, [see the official docs](https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line)
  168. ![install-vscode-cli](./public/install-vscode-cli.png)
  169. - set env to shell, like `.bashrc` or `.zshrc`
  170. ```bash
  171. export LAUNCH_EDITOR=code
  172. ```
  173. <br />
  174. ### VS Code with WSL (Windows)
  175. - add the configuration in the `settings.json`
  176. - restart the VS Code (All Windows should be closed to take effect)
  177. ```json
  178. {
  179. // other config...
  180. "terminal.integrated.env.linux": {
  181. "EDITOR": "code"
  182. }
  183. }
  184. ```
  185. ### WebStorm
  186. - just set env with an absolute path to shell, like `.bashrc` or `.zshrc` (only MacOS)
  187. ```bash
  188. export LAUNCH_EDITOR='/Applications/WebStorm.app/Contents/MacOS/webstorm'
  189. ```
  190. **OR**
  191. - install WebStorm command line tools
  192. - then set env to shell, like `.bashrc` or `.zshrc`
  193. ```bash
  194. export LAUNCH_EDITOR=webstorm
  195. ```
  196. <br />
  197. ### PhpStorm
  198. - just set env with an absolute path to shell, like `.bashrc` or `.zshrc` (only MacOS)
  199. ```bash
  200. export LAUNCH_EDITOR='/Applications/PhpStorm.app/Contents/MacOS/phpstorm'
  201. ```
  202. **OR**
  203. - install PhpStorm command line tools
  204. - then set env to shell, like `.bashrc` or `.zshrc`
  205. ```bash
  206. export LAUNCH_EDITOR=phpstorm
  207. ```
  208. <br />
  209. ### Vim
  210. Yes! you can also use vim if you want, just set env to shell
  211. ```bash
  212. export LAUNCH_EDITOR=vim
  213. ```
  214. <br />
  215. ## 💡 Notice
  216. - **[BREAKING CHANGE] From v1.0, `enabled` option default value changed from `true` to `false` .**
  217. - It only work in develop mode .
  218. - It does not currently support `Template Engine (e.g. pug)` .
  219. ## 👨‍💻 Programmatic Usage
  220. You can also use control inspector programmatically, by accessing the `__VUE_INSPECTOR__` global variable.
  221. ```ts
  222. import type { VueInspectorClient } from 'vite-plugin-vue-inspector'
  223. const inspector: VueInspectorClient = window.__VUE_INSPECTOR__
  224. if (inspector) {
  225. // enable inspector
  226. inspector.enable()
  227. // or
  228. inspector.disable()
  229. }
  230. ```
  231. ## 🌸 Credits
  232. This project is inspired by [react-dev-inspector](https://github.com/zthxxx/react-dev-inspector) .
  233. Partially implementation is inspired by [vite-plugin-svelte-inspector](https://github.com/sveltejs/vite-plugin-svelte/tree/main/packages/vite-plugin-svelte/src/ui/inspector) .
  234. ## 🤖️ Analysis of Theory
  235. [Chinese] [点击页面元素,这个Vite插件帮我打开了Vue组件](https://juejin.cn/post/7077347158545924127)
  236. ## 📄 License
  237. [MIT LICENSE](./LICENSE)