vite icon indicating copy to clipboard operation
vite copied to clipboard

Support "worker" export condition when ssr.target is "webworker"

Open DylanPiercey opened this issue 3 years ago • 7 comments

Clear and concise description of the problem

https://github.com/vitejs/vite/pull/3151/files added support for setting ssr.target to "webworker". This appears to set the export conditions to include "browser".

It would be great to be able to detect when compiling to a worker specifically with export conditions but there is no condition currently set for web workers.

Suggested solution

I propose setting a "worker" condition when we target web workers. This would be consistent with webpack (https://webpack.js.org/guides/package-exports/#target-environment).

Alternative

No response

Additional context

No response

Validations

DylanPiercey avatar Jan 06 '22 04:01 DylanPiercey

Do you plan to send a PR so we can discuss seeing the needed changes?

patak-dev avatar Jan 06 '22 09:01 patak-dev

@patak-dev I'd like to make a PR, but I might need a bit of guidance. The code around resolving paths is quite spread out throughout the codebase.

Conceptually I think I'd just hook into https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/resolve.ts#L784

and add

  if (options.ssrConfig?.target === 'webworker') {
    conditions.push('worker');
  }

However we only want to add the worker condition if we are doing an ssr resolve and I'm not sure if I have that information at that point.

DylanPiercey avatar Jan 06 '22 16:01 DylanPiercey

A similar feature request #7439

bluwy avatar Apr 01 '22 16:04 bluwy

Adding worker and webworker to resolve.conditions config as mentioned in #7439 sorted this for me. I agree with that issue that this should be automatic for WebWorkers targets.

https://vitejs.dev/config/shared-options.html#resolve-conditions

cesutherland avatar Jan 05 '23 15:01 cesutherland

Also hitting us with trying to use react with emotion in Qwik!

manucorporat avatar Jan 26 '23 08:01 manucorporat

I think it's a mistake to confuse webworker with browser:

  • workers in general might run in the browser but have no access dom browser API like the DOM
  • webworker target is usually used to target edge deployment for SSR (ie, netlify, vercel-edge, cloudflare) non of them is a browser setup, but it's not 'node'

webworker might be a bad term that already spans very different APIs. Maybe we should have:

  • "browser-worker"
  • "browser"
  • "node"
  • "worker" (edge)

manucorporat avatar Jan 26 '23 09:01 manucorporat

@manucorporat I think you could still differentiate by using nested export conditions like so:

{
  "exports": {
    "browser": {
      "worker": "target browsers web workers",
      "default": "regular browser import"
    },
    "node": {
      "worker": "worker in a node environment",
      "default": "regular node script"
    },
    "worker": "probably edge?",
    "default": "🤷‍♂️"
  }
}

DylanPiercey avatar Jan 26 '23 17:01 DylanPiercey