开发 Chrome 浏览器插件时进行 Vue3+Vite 多页面多入口配置

使用 Vite 开发 Chrome 插件时,构建多页面以及多 js 文件

因为发现 Vite 多页面构建有很多分歧以及问题点,所以我把我在 Chrome 插件开发上面使用到的 Vite 多页面以及多入口文件构建配置单独拿出来

开发 Chrome 插件是,一般会需要一个 popup html 页面,有时候还会需要一个 content html 页面,但是还需要 service-worker.js 文件以及 content.js 文件

一、Chrome 插件版- Vue 项目构建

我们通过 yarn create vite 创建新的项目,然后改成我们插件的项目结构

shell 复制代码
.
├── README.md
├── manifest.json
├── package.json
├── pnpm-lock.yaml
├── src
│   ├── assets
│   │   └── vue.svg
│   ├── background
│   │   └── service-worker.ts
│   ├── content
│   │   └── content.ts
│   ├── contentPage
│   │   ├── App.vue
│   │   ├── components
│   │   │   └── TestContent.vue
│   │   ├── index.html
│   │   ├── main.ts
│   │   └── style.css
│   ├── icons
│   │   └── icon.png
│   ├── popup
│   │   ├── App.vue
│   │   ├── components
│   │   │   └── TestPopup.vue
│   │   ├── index.html
│   │   ├── main.ts
│   │   └── style.css
│   └── vite-env.d.ts
├── tsconfig.json
├── tsconfig.node.json
└── vite.config.ts
  • manifest.json 是插件的入口文件,此文件必须有,在根目录和 src 目录都行,但一般习惯放在根目录中
  • src/popup 是弹框页面
  • src/contentPage 是注入页面的 content 页面,和 src/content 的区别是这个最终打包为 index.html 文件,通过 iframe 的形式插入对应域名的页面中
  • src/backgroundservice-worker 页面,V3 虽然也叫 background,但是这个文件一般都写成 service-worker
  • src/content 是对应域名的 content.ts 文件
  • src/icons 是放置插件的 16、32、48、128 的 png 图片,可以用一张 128 的也行

二、配置 Vite.config.ts 文件

typescript 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import copy from 'rollup-plugin-copy'

// https://vitejs.dev/config/
export default defineConfig({
  root: 'src/',
  plugins: [
    vue(),
    copy({
      targets: [
        { src: 'manifest.json', dest: 'dist' },
        { src: "src/icons/**", dest: 'dist/icons' }
      ]
    })
  ],
  build: {
    outDir: path.resolve(__dirname, 'dist'),
    rollupOptions: {
      input: {
        popup: path.resolve(__dirname, 'src/popup/index.html'),
        contentPage: path.resolve(__dirname, 'src/contentPage/index.html'),
        content: path.resolve(__dirname, 'src/content/content.ts'),
        background: path.resolve(__dirname, 'src/background/service-worker.ts'),
      },
      output: {
        assetFileNames: 'assets/[name]-[hash].[ext]', // 静态资源
        chunkFileNames: 'js/[name]-[hash].js', // 代码分割中产生的 chunk
        entryFileNames: (chunkInfo) => { // 入口文件
          const baseName = path.basename(chunkInfo.facadeModuleId, path.extname(chunkInfo.facadeModuleId))
          const saveArr = ['content', 'service-worker']
          return `[name]/${saveArr.includes(baseName) ? baseName : chunkInfo.name}.js`;
        },
        name: '[name].js'
      }
    },
  },
})
  • 引入 rollup-plugin-copy 是通过复制文件,因为 iconsmanifest.json 这两个文件不需要 build,原来是什么样,还是什么样就行,通过复制,直接放到 dist 目录中
  • root: 'src/', 是配置跟路径,因为我们的页面都是在 src 页面下的,但是我们打包之后不需要 src 这个路径,在 manifest.json 中配置更不需要,所以把这个过滤掉
  • rollupOptions 中的 input,配置了四个入口文件,两个 index.html,两个 ts 文件
    • index.html 文件好理解,就是对应的 popup 页面和注入到对应域名项目中的 content 页面
    • ts 文件是因为我需要把 content.tsservice-worker.ts 这两个文件也打包出去,因为这两个是单独的 js,而且需要在 manifest.json 中使用
  • output 中的 entryFileNames 是需要把 content.tsservice-worker.ts 在构建之后还是保持这两个名字,这样 manifest.json 中不会出错

三、Manifest.json 配置

json 复制代码
{
  "manifest_version": 3,
  "name": "My Vue Chrome Ext",
  "version": "0.0.1",
  "description": "Chrome 插件",
  "icons": {
    "16": "icons/icon.png",
    "19": "icons/icon.png",
    "38": "icons/icon.png",
    "48": "icons/icon.png",
    "128": "icons/icon.png"
},
  "action": {
    "default_title": "Vue Chrome Ext",
    "default_icon": "icons/icon.png",
    "default_popup": "popup/index.html"
  },
  "background": {
    "service_worker": "background/service-worker.js"
  },
  "permissions": [
    "activeTab",
    "tabs",
    "alarms"
  ],
  "host_permissions": [],
  "content_scripts": [
    {
      "js": [
        "content/content.js"
      ],
      "matches": [
        "http://127.0.0.1:5500/*"
      ],
      "all_frames": true,
      "run_at": "document_end",
      "match_about_blank": true
    }
  ],
  "web_accessible_resources": [
    {
      "resources": ["popup/*", "contentPage/*", "assets/*", "js/*"],
      "matches": ["http://127.0.0.1:5500/*"],
      "use_dynamic_url": true
    }
  ]
}

参考

【两万字大章带你使用 Vue3、Vite、TypeScript、Less、Pinia、Naive-ui 开发 Chrome 浏览器 Manifest V3 版本插件】

相关推荐
lkbhua莱克瓦2414 小时前
JavaScript核心语法
开发语言·前端·javascript·笔记·html·ecmascript·javaweb
Trae1ounG14 小时前
这是什么dom
前端·javascript·vue.js
比老马还六14 小时前
Bipes项目二次开发/扩展积木功能(八)
前端·javascript
易营宝14 小时前
全球建站SaaS平台能提升SEO评分吗?是否值得切换?
大数据·前端·人工智能
5134959214 小时前
在Vue.js项目中使用docx和file-saver实现Word文档导出
前端·vue.js·word
AC赳赳老秦15 小时前
Prometheus + DeepSeek:自动生成巡检脚本与告警规则配置实战
前端·javascript·爬虫·搜索引擎·prometheus·easyui·deepseek
接着奏乐接着舞。15 小时前
前端大数据渲染性能优化:Web Worker + 分片处理 + 渐进式渲染
大数据·前端·性能优化
Beginner x_u15 小时前
CSS 中的高度、滚动与溢出:从 height 到 overflow 的完整理解
前端·css·overflow·min-height
vx1_Biye_Design15 小时前
基于web的物流管理系统的设计与实现-计算机毕业设计源码44333
java·前端·spring boot·spring·eclipse·tomcat·maven
tqs_1234516 小时前
倒排索引数据结构
java·前端·算法