vite - 在 vite 脚本中禁用哈希 - chrome 扩展的多个入口点

标签 vite

我正在尝试编写一个 chrome 扩展程序。我找到了一种方法来创建多个 页面和后台脚本,但后台脚本包含哈希并且是 放入 dist/assets 文件夹中。我只想输出“dist/background.js”。或者(也许更好)我想更新我的 list 以包含带有哈希的实际脚本名称。

这是我的 vite.config.ts

import { defineConfig, BuildOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  build: {
    // lib: {
    //   entry: resolve(__dirname, 'background.ts'),
    //   name: 'background',
    //   fileName: format => `background.{format}.js`
    // },
    rollupOptions: {
      // output: {
      //   format: 'cjs'
      // },
      input: {
        main: resolve(__dirname, 'index.html'),
        popup: resolve(__dirname, 'popup/index.html'),
        options: resolve(__dirname, 'options/index.html'),
        background: resolve(__dirname, 'background.ts'),
      },
      // output: {
      //   format: 'cjs'
      // }
    }
  }
})

我已经尝试了一些东西,这里显示的一些东西被注释掉了,但我无法让任何东西按我想要的方式工作。如果我创建一个单独的 vite.config.background.ts 文件,我终于能够让它工作:

import { defineConfig } from 'vite'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    emptyOutDir: false,
    rollupOptions: {
      input: resolve(__dirname, 'background.ts'),
      output: {
        format: "esm",
        file: "dist/background.js",
        dir: null,
      }
    }
  }
})

然后编辑我的构建脚本以在其他配置之后构建它:

"build": "vue-tsc --noEmit && vite build && vite build --config vite.config.background.ts",

但是有没有办法只使用一个文件,或者生成 list 并使用散列的 文件名?现在它是一个静态的 json 文件。

最佳答案

这就是我最终做的,但不是最优的。很难相信您可以指定产生多个输出的多个输入,但不能为每个定义输出设置...结构:

+-- dist (output directory)
+-- options (options page)
+-- popup (popup page)
+-- public (contents copied directly to dist
|   +-- background.js (generated background script from '/background.ts')
|   +-- manifest.json (extension manifest)
+-- scripts (scripts used in build)
|   +-- watch.mjs (script for 'yarn watch')
+-- src (main page)
+-- background.ts

因此 manifest.json 位于复制到 dist 根目录的公共(public)目录中。后台脚本构建也将输出 background.js 放在该文件夹中,以便主构建作为“主”页面的一部分进行复制。

scripts/watch.mjs

import { createServer, build } from 'vite'

/**
 * @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
 */
function watchBackground(server) {
  return build({
    configFile: 'vite.config.background.ts',
    mode: 'development',
    build: {
      watch: {},
    },
  })
}

/**
 * @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
 */
 function watchMain(server) {
  return build({
    configFile: 'vite.config.ts',
    mode: 'development',
    build: {
      watch: {},
    },
  })
}

// bootstrap
const server = await createServer({ configFile: 'vite.config.ts' })

await server.listen()
await watchBackground(server) // outputs to public/background.js, so run first
await watchMain(server)

vite.config.background.ts

import { defineConfig } from 'vite'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    emptyOutDir: false,
    rollupOptions: {
      input: resolve(__dirname, 'background.ts'),
      output: {
        format: "esm",
        file: "public/background.js",
        dir: null,
      }
    }
  }
})

vite.config.ts

import { defineConfig, BuildOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        popup: resolve(__dirname, 'popup/index.html'),
        options: resolve(__dirname, 'options/index.html'),
      },
    }
  }
})

关于vite - 在 vite 脚本中禁用哈希 - chrome 扩展的多个入口点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72331733/

相关文章:

javascript - 函数 'each' 未定义 antd Vite with React

javascript - 如果 NPM 包将被消费项目捆绑,为什么还要捆绑它们?

javascript - Vite 和 PostCSS 问题,分配中的左侧无效

vue.js - 如何配置Vite开发服务器通过端口代理路径运行?

javascript - vite.config.js rollupOptions 在构建过程中导致 "Unexpected token"错误

reactjs - Vite 中的构建命令出现 typescript 错误

webpack - 如何修复 Vue3 Vite 在生产中丢失动态图像的问题?

vue.js - 如何配置 Vite 来解析嵌套的 SFC CSS url

javascript - "~"(代字号)在 import ... from "~/"中意味着什么