Vite项目构建chrome extension,实现多入口

本项目使用Vite5 + Vue3进行构建。

要使用vite工程构建浏览器插件,无非就是要实现popup页面和options页面。这就需要在项目中用到多入口打包(生成多个html文件)。

实现思路:

  1. 通过配置vite工程,使得项目打包后有两个html文件。
  2. 同时打包入口打包background.js。
  3. 在manifest.json文件中配置popup、options、background等内容。
  4. 将项目中的manifest.json文件打包至dist目录下。

第一步、创建Vue3项目并调整目录结构

复制代码
npm create vue@latest

通过此命令创建项目,创建后调整项目目录结构,由下图所示:

项目根目录的index.html打包后配置为popup,options.html配置为options。

将manifest.json放在src目录下,当然也可以放在public目录下(打包时vite自动将静态资源打包至dist目录下)。放在src目录下更符合个人的开发模式。

第二步、编写index.html和options.html

由于index.html打包后配置为popup页面,所以应该这样写:

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite App</title>
  </head>
  <body>
    <div id="app"></div>
    <!--    引入popup的入口ts-->
    <script type="module" src="src/popup/main.ts"></script>
  </body>
</html>

同样options.html应引入src/options/main.ts

第三步、编写popup/main.ts和options/main.ts

两者的内容基本相同:

javascript 复制代码
import '../assets/main.css'

import { createApp } from 'vue'
import elementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// popup页面引入Popup组件,options页面引入Options组件
import Popup from './Popup.vue'

const app = createApp(Popup)
app.use(elementPlus)
app.mount('#app')

第四步、编写vite配置文件

此文件主要实现两部分内容,其他实现读者可自行添加。

  1. 将src目录下的manifest.json打包构建时移到dist目录下。
  2. 配置多入口文件。
javascript 复制代码
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    viteStaticCopy({
      targets: [
          { src: 'src/*.json', dest: './' },
      ]
    })
  ],
  build: {
    rollupOptions: {
      input: {
        index: resolve(__dirname, 'index.html'),
        options: resolve(__dirname, 'options.html'),
        background: resolve(__dirname, 'src/background.ts'),
      },
      output: {
        entryFileNames: `[name].js`,
      }
    },
    outDir: 'dist',
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

第五步、编写manifest.json文件

javascript 复制代码
{
  "name": "xxx",
  "version": "1.0",
  "description": "xxx",
  "homepage_url": "https://xxx.com",
  "manifest_version": 3,
  "icons": {
    "16": "logo.png",
    "48": "logo.png",
    "64": "logo.png",
    "128": "logo.png"
  },
  "commands": {
    "reload_extension": {
      "suggested_key": {
        "default": "Ctrl+Shift+K",
        "mac": "Command+Shift+K"
      },
      "global": true,
      "description": "Toggle My Extension"
    }
  },
  "action": {
    "default_icon": "logo.png",
    "default_popup": "index.html"
  },
  "options_page": "options.html",
  "background": {
    "service_worker": "background.js",
    "type": "module"
  },
  "permissions": [
    "management",
    "scripting",
    "notifications",
    "contextMenus",
    "webRequest",
    "storage",
    "tabs",
    "activeTab",
    "nativeMessaging"
  ]
}

通过以上的几个步骤,即可实现浏览器插件使用vue项目开发,同时支持popup页面和options页面,以及background.js。

相关推荐
漂流瓶jz42 分钟前
快速定位源码问题:SourceMap的生成/使用/文件格式与历史
前端·javascript·前端工程化
samroom43 分钟前
iframe实战:跨域通信与安全隔离
前端·安全
fury_1231 小时前
vue3:数组的.includes方法怎么使用
前端·javascript·vue.js
weixin_405023371 小时前
包资源管理器NPM 使用
前端·npm·node.js
宁&沉沦1 小时前
Cursor 科技感的登录页面提示词
前端·javascript·vue.js
Dragonir1 小时前
React+Three.js 实现 Apple 2025 热成像 logo
前端·javascript·html·three.js·页面特效
peachSoda72 小时前
封装一个不同跳转方式的通用方法(跳转外部链接,跳转其他小程序,跳转半屏小程序)
前端·javascript·微信小程序·小程序
@PHARAOH3 小时前
HOW - 浏览器兼容(含 Safari)
前端·safari
undefined在掘金390413 小时前
flutter 仿商场_首页
前端