crxjs / chrome-extension-tools

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

Dynamic module import in content script tries to load from website instead of extension (web components polyfill) #592

Open devidw opened 1 year ago

devidw commented 1 year ago

Build tool

Vite

Where do you see the problem?

Describe the bug

I have got a content script with something like:

content script content.ts

await import('@webcomponents/webcomponentsjs')

On build, this generates into:

 await c(
    () => import('./webcomponents-bundle.b61d43ec.js').then((s) => s.w),
    [
      'assets/webcomponents-bundle.b61d43ec.js',
      'assets/_commonjsHelpers.4e997714.js',
    ]
  ),

Here, the last two paths are problematic, since they are tried to be loaded from the website the content scripts run on.

<link rel="modulepreload" as="script" crossorigin="" href="/assets/webcomponents-bundle.b61d43ec.js">
<link rel="modulepreload" as="script" crossorigin="" href="/assets/_commonjsHelpers.4e997714.js">

Which results in them not being found:

Screenshot 2022-11-27 at 14 02 41

However, removing the extra paths and putting an empty array into the generated files does work, the extension seems to work without these available.

Reproduction

https://github.com/devidw/crxjs-issue

Seems to be in combination with other package imports that let the tooling generate these helper files like _commonjsHelpers.

In this example, importing extpay, without this, there are no such helper files generated and no loading problems.

However, trying to get things working with such imports.

Thanks a lot 🙏

Logs

GET https://stackoverflow.com/assets/_commonjsHelpers.4e997714.js net::ERR_ABORTED 404
GET https://stackoverflow.com/assets/webcomponents-bundle.b61d43ec.js net::ERR_ABORTED 404

System Info

System:
    OS: macOS 13.0.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.50 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.1.0 - /opt/homebrew/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.19.3 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 107.1.45.133
    Chrome: 107.0.5304.110
    Safari: 16.1

Severity

annoyance

Whoaa512 commented 1 year ago

This is something with Vite itself not the plugin. I worked around by adding build.modulePreload = false in my vite.config.ts

Toumash commented 1 month ago

@Whoaa512 thank you so much for this contribution works in my project with "vite": "^5.2.11",! Maybe we could add it to the docs and close the thread. Where should we put it? Here https://crxjs.dev/vite-plugin/concepts/content-scripts?

alexdymov commented 1 month ago

It wasn't enough for my setup with Vite 4.4.5 to just modulePreload = false. Vite was still adding a preloader function with preloading a single CSS file. The only way to remove it was to remove the whole vite:build-import-analysis plugin:


function viteDisablePlugins(list: string[]): Plugin {
  return {
    name: 'vite-disable-plugins',
    configResolved(config) {
      const pls: Plugin[] = config.plugins as any;
      list.forEach(torem => {
        const idx = pls.findIndex(pl => pl.name === torem);
        idx >= 0 && pls.splice(idx, 1);
      })
    },
  }
}

export default defineConfig(({ command }) => ({
  plugins: [
    viteDisablePlugins(['vite:build-import-analysis']),
  ]
}))
Toumash commented 1 month ago

Ok, im using the newest "vite": "^5.2.11",