crxjs / chrome-extension-tools

Bundling Chrome Extensions can be pretty complex. It doesn't have to be.
https://crxjs.dev/vite-plugin
2.96k stars 189 forks source link

Vite build for firefox #854

Open honwhy opened 10 months ago

honwhy commented 10 months ago

Describe the problem

vite build produces multiple js files, that is good. But when js file contains Function, it will be block by firefox CSP policy. image

manifest.config.ts

{
  content_scripts: [
            {
            "js": [
                "src/contents/douban.ts"
            ],
            "matches": [
                "https://book.douban.com/subject/*"
            ]
        },
  ]
}

compiles to

content_scripts: [
    {
      "js": [
        "assets/douban.ts-loader-ddd82093.js"
      ],
      "matches": [
        "https://book.douban.com/subject/*"
      ],
      "css": [
        "assets/_plugin-vue_export-helper-175e6fba.css",
        "assets/el-empty-743b38f5.css",
        "assets/ContextMenuSearchDrawer-c8e94998.css",
        "assets/el-link-5aef13b0.css",
        "assets/douban-3cb6a821.css",
        "assets/el-loading-6846fe26.css",
        "assets/el-scrollbar-df5aa9a0.css"
      ]
    },
]

contet of douban.ts-loader-ddd82093.js

(function () {
  'use strict';

  const injectTime = performance.now();
  (async () => {
    const { onExecute } = await import(
      /* @vite-ignore */
      chrome.runtime.getURL("assets/douban.ts-ebbbac0d.js")
    );
    onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });
  })().catch(console.error);

})();

abstract of douban.ts-ebbbac0d.js

import{d as $,o as z,r as x,a as i,c as u,b as c,e as p,u as s,t as f,w as G,i as J,F as C,E as K,f as O,g as P,h as X,j as Y,v as Z,k as ee}from"./element-plus-f1e000a5.js";

key problem, content of element-plus-f1e000a5.js contains Function usage. according to https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_Security_Policy, Function is not supported, image

any helps is welcome

Describe the proposed solution

I guess this is something to do with vite build config,

  optimizeDeps: {
    esbuildOptions: {
      target: "esnext",
    },
  },
  build: {
    target: "esnext",
    rollupOptions: {
      output: {
        manualChunks: command === 'build' ? {
          "element-plus": ["element-plus"],
          echarts: ['echarts'],
          dayjs: ['dayjs']
        } : undefined
      }
    }
  },

should I change setting of build.target ?

Alternatives considered

so sorry, I have not idea

Importance

nice to have

trungpv1601 commented 2 months ago

any update about this issue? Thank you