Skip to content

how to use vue-devtools in chrome extension options page #727

Open

Description

@honwhy

failed to load virtual js files from vue-devtools when running in chrome extension page.
how to specify injected js src with origin from dev server.

I am using wxt.dev to develop chrome extension, wxt.config.ts config like this,

// wxt.config.ts
import vueDevTools from 'vite-plugin-vue-devtools'

export default defineConfig({
  plugins: [
   vue(),
   vueDevtools()
  ]
})

something i found after start npx wxt on my project,

//.output/chrome-mv3/options.html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <script type="module" src="/@id/virtual:vue-devtools-path:overlay.js"></script>
    <script type="module" src="/@id/virtual:vue-inspector-path:load.js"></script>
....

as a result, request is failed

Request URL:
chrome-extension://amidodbgcdpbldlmccigekeemjhkkhlh/@id/virtual:vue-devtools-path:overlay.js
Referrer Policy:
strict-origin-when-cross-origin

it would be correct if the injected js src to be prepend with dev server, for example

    <script type="module" src="http://localhost:3000/@id/virtual:vue-devtools-path:overlay.js"></script>
    <script type="module" src="http://localhost:3000/@id/virtual:vue-inspector-path:load.js"></script>

Activity

  1. added
    vite
    usage
    on Dec 6, 2024
  2. honwhy commented on Dec 7, 2024

    @honwhy
    Author

    Yeah, I am using WXT, work around solution may like this adding another plugin,

    export function vueDevtoolsHack(
      config: wxt.ResolvedConfig,
      getWxtDevServer: () => wxt.WxtDevServer | undefined,
    ): vite.Plugin {
      const server = getWxtDevServer?.()
      return {
        name: `md:vue-devtools-hack`,
        apply: `build`,
        transformIndexHtml: {
          order: `post`,
          handler(html) {
            const { document } = parseHTML(html)
            const inlineScripts = document.querySelectorAll(`script[src^='/@id/virtual:']`)
            inlineScripts.forEach((script) => {
              const src = script.getAttribute(`src`)
              const newSrc = `${server?.origin}${src}`
              script.setAttribute(`src`, newSrc)
            })
            const newHtml = document.toString()
            config.logger.debug(`Old HTML:\n${html}`)
            config.logger.debug(`New HTML:\n${newHtml}`)
            return newHtml
          },
        },
      }
    }
    
    
  3. alexzhang1030 commented on Dec 9, 2024

    @alexzhang1030
    Member

    We have supported this by configuring appendTo, which works out-of-box by passing to vite. If you have some other special cases, we can talk about them.

  4. honwhy commented on Dec 9, 2024

    @honwhy
    Author
  5. alexzhang1030 commented on Dec 10, 2024

    @alexzhang1030
    Member

    This is your entry point I guess. Can you try it out?

    image

  6. DES2048 commented on Dec 24, 2024

    @DES2048

    Ufortunately setting appendTo: "/entrypoints/options/main.ts" doesnt working for me. Can you explain how to properly set this option? I tried to set appendTo starting with src: "/src/entrypoints/options/main.ts" and with full url: http://localhost:3000/src/entrypoints/options/main.ts". But it still doesnt work:(. But in rendered main.ts I saw that devtools imports is present:
    import "/node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/vite-plugin-vue-devtools/src/overlay.js?__vue-devtools-next-resource"; import { createApp } from "/node_modules/.vite/deps/vue.js?v=1f931d2c"; import vuetify from "/src/plugins/vuetify.ts"; import Options2 from "/src/entrypoints/options/Options2.vue"; const app = createApp(Options2).use(vuetify); app.mount("#app"); import "/node_modules/.pnpm/[email protected][email protected]_@[email protected]_/node_modules/vite-plugin-vue-inspector/src/load.js";

  7. marijnbent commented on Mar 12, 2025

    @marijnbent

    The appendTo doesn't work for me either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    usagevite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions