开发 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 版本插件】

相关推荐
invicinble3 分钟前
c端系统,其实更像一个信息展示平台
前端
李姆斯1 小时前
管理是否可以被完全量化
前端·产品经理·团队管理
名字还没想好☜1 小时前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司2 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
江华森2 小时前
Web 开发基础:HTTP 与 REST
前端·网络协议·http
IT_陈寒3 小时前
Redis的KEYS命令把我搞崩溃了,改用SCAN才活过来
前端·人工智能·后端
Jackson__3 小时前
为什么 Agent 越聊越慢?聊聊 Context(上下文)管理
前端·agent·ai编程
KaMeidebaby4 小时前
卡梅德生物技术快报|抗体合成:多肽抗体合成工程化方案:Nsp2 保守肽多抗制备与多维度验证
前端·网络·数据库·人工智能·算法
青禾网络4 小时前
前端做音画匹配这件事,我从"随机塞"到"AI 自动对齐"
前端·github
2601_963771375 小时前
Hardening Enterprise WordPress Sites Against REST API Leaks and Bad Headers
前端·windows·word·php