aklinker1 / vite-plugin-web-extension

Vite plugin for developing Chrome/Web Extensions
https://vite-plugin-web-extension.aklinker1.io/
MIT License
574 stars 48 forks source link

[Bug Report] generate config `build.lib.formats: ["umd"]` broken vite `import.meta.glob` function #20

Closed Rhilip closed 2 years ago

Rhilip commented 2 years ago

Vite provide a Glob Import feature , however when use this feature function import.meta.glob in script, the build will fail and with this output:

Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
error during build:
Error: Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
    at error (D:\path-to-project\node_modules\.pnpm\rollup@2.68.0\node_modules\rollup\dist\shared\rollup.js:159:30)
    at validateOptionsForMultiChunkOutput (D:\path-to-project\node_modules\.pnpm\rollup@2.68.0\node_modules\rollup\dist\shared\rollup.js:16134:16)
    at Bundle.generate (D:\path-to-project\node_modules\.pnpm\rollup@2.68.0\node_modules\rollup\dist\shared\rollup.js:15968:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at handleGenerateWrite (D:\path-to-project\node_modules\.pnpm\rollup@2.68.0\node_modules\rollup\dist\shared\rollup.js:23612:23)
    at doBuild (D:\path-to-project\node_modules\.pnpm\vite@2.8.5\node_modules\vite\dist\node\chunks\dep-ff3ab196.js:39134:26)
    at Object.build (D:\path-to-project\node_modules\.pnpm\vite@2.8.5\node_modules\vite\dist\node\chunks\dep-ff3ab196.js:38959:16)
    at buildScript (D:\path-to-project\node_modules\.pnpm\vite-plugin-web-extension@1.1.2_vite@2.8.5\node_modules\vite-plugin-web-extension\dist\index.js:90:3)
    at Object.closeBundle (D:\path-to-project\node_modules\.pnpm\vite-plugin-web-extension@1.1.2_vite@2.8.5\node_modules\vite-plugin-web-extension\dist\index.js:524:11)
    at async Promise.all (index 0)
Waiting for the debugger to disconnect...
 ELIFECYCLE  Command failed with exit code 1.

It's ok when use function import.meta.globEager since this function will not splitting code.


Steps to reproduce

  1. clone the last bugreport project https://github.com/Rhilip/bugreport-vite-plugin-web-extension-watchbuild
  2. create two or more files like src/background/module/1.ts and src/background/module/2.ts , each may have simple code like console.log("hello world");
  3. in src/background/index.ts , add line console.log(import.meta.glob("./module/*.ts"));
  4. run command pnpm run build and see error code, no dist/background folder will be built.

Try to fix


aklinker1 commented 2 years ago

I specifically set them to {} because the rollup options can't be shared between the multi-page build and the library builds.

I can add another plugin option like libModeViteConfig that would support overriding the rollup options for those builds

aklinker1 commented 2 years ago

Alright, I've implmented libModeViteConfig and released it in v1.3.0. For an example of configuring the lib mode builds for your case, see the tests:

https://github.com/aklinker1/vite-plugin-web-extension/blob/19a2c4a9a569286df10f8465a1128871209a1790/tests/e2e.test.ts#L325-L333

aklinker1 commented 2 years ago

@Rhilip if that doesn't work for you, feel free to reopen with more details.