Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker output paths aren't respected in lib mode #12250

Closed
7 tasks done
ivancuric opened this issue Mar 1, 2023 · 5 comments
Closed
7 tasks done

Worker output paths aren't respected in lib mode #12250

ivancuric opened this issue Mar 1, 2023 · 5 comments
Labels
invalid This doesn't seem right

Comments

@ivancuric
Copy link

ivancuric commented Mar 1, 2023

Describe the bug

I am using a hacky approach to build a worker bundle in a lib mode.

It's not possible to configure the lib.formats option for every entry, and I need to configure it as an iife for the worker output. I am therefore forced to use Vite's worker loading.

Vite's worker extraction won't when a dynamic path is used, and I need a configurable path in my lib, like:

// this won't generate a worker bundle
export function loadWorker(url: string) {
  const worker = new Worker(new URL("./worker.ts", url));
}

To force the extraction, I am using an unreachable code path which will get stripped out:

// unreachable code for worker extraction
if (false) {
  new Worker(new URL("./worker.ts", import.meta.url));
}

I am now stuck in trying to move the worker bundle to a different folder.

worker: {
    format: "iife",
    rollupOptions: {
      output: {
		// this won't work
        assetFileNames: "resources/blinkid.worker.js",
      },
    },
  },

#7804 doesn't seem to have fully fixed worker rollup options.

Reproduction

https://stackblitz.com/edit/vitejs-vite-vpk6ws?file=vite.config.js&view=editor

Steps to reproduce

npm install && npm run build

System Info

System:
    OS: macOS 12.6.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 84.34 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.7.0 - ~/.nvm/versions/node/v19.7.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v19.7.0/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v19.7.0/bin/npm
  npmPackages:
    vite: ^4.1.4 => 4.1.4

Used Package Manager

pnpm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Mar 1, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@xiaohk
Copy link
Contributor

xiaohk commented Jul 19, 2023

Same issue here. Did you find any workaround? @ivancuric

@xiaohk
Copy link
Contributor

xiaohk commented Jul 19, 2023

It seems the following config works:

worker: {
  format: 'es',
  rollupOptions: {
    output: {
      entryFileNames: '[name].js'
    }
  }
},

@mustafa0x
Copy link

mustafa0x commented Sep 9, 2023

entryFileNames does indeed work.

@sapphi-red
Copy link
Member

In this case, worker.js is the entry file of the worker. So entryFileNames needs to be used instead of assetFileNames.

@sapphi-red sapphi-red added invalid This doesn't seem right and removed pending triage labels Nov 7, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

4 participants