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

feat: allow custom worker #2578

Closed
wants to merge 2 commits into from
Closed

feat: allow custom worker #2578

wants to merge 2 commits into from

Conversation

dejour
Copy link
Contributor

@dejour dejour commented Mar 18, 2021

This PR offers a solution to #2550.

@Shinigami92 Shinigami92 added the enhancement New feature or request label Mar 21, 2021
@dejour
Copy link
Contributor Author

dejour commented Mar 23, 2021

done @Shinigami92

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 23, 2021
@ebeloded
Copy link

Hey @dejour! Thank you for working on this.

This PR misses TypeScript support. Currently client.d.ts defines the following for the workers:

// web worker
declare module '*?worker' {
  const workerConstructor: {
    new (): Worker
  }
  export default workerConstructor
}

declare module '*?worker&inline' {
  const workerConstructor: {
    new (): Worker
  }
  export default workerConstructor
}

It means that TSC won't recognize import with additional parameters.

Is it not possible to implement this functionality without augmenting the import statement?

Here is what I mean:

import MyWorker from './worker?worker'

const worker = new MyWorker() // classic worker
const worker = new MyWorker({type: 'classic'}) // classic worker
const worker = new MyWorker({type: 'module'}) // module worker

@ebeloded
Copy link

And I couldn't make it work with classic worker.

// main
import MyWorker from './worker?worker&type=classic';

const worker = new MyWorker();
console.log({ worker });
// worker.ts
importScripts('www.gstatic.com/firebasejs/8.0.0/firebase-app.js') // use importScripts to verify classic-ness 
console.log(`worker here`, { firebase: firebase })

The generated worker file looks like this:

import '/@fs/Users/evgenijbeloded/Code/vendors/vite-classic-workers/packages/vite/dist/client/env.js'
importScripts("www.gstatic.com/firebasejs/8.0.0/firebase-app.js");
console.log(`ts worker here`, {firebase});

Which rightfully stumbles on the first line with SyntaxError:

Uncaught SyntaxError: Cannot use import statement outside a module

@dejour
Copy link
Contributor Author

dejour commented Mar 29, 2021

Is it not possible to implement this functionality without augmenting the import statement?

it seems possible.

The generated worker file looks like this:

import '/@fs/Users/evgenijbeloded/Code/vendors/vite-classic-workers/packages/vite/dist/client/env.js'
importScripts("www.gstatic.com/firebasejs/8.0.0/firebase-app.js");
console.log(`ts worker here`, {firebase});

Which rightfully stumbles on the first line with SyntaxError:

Uncaught SyntaxError: Cannot use import statement outside a module

import statement can only be used in module worker, this indeed is a problem needed to be fixed. if not augmenting the import statement, we can not change the way env.js is imported.

@ebeloded
Copy link

Is this PR abandoned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow importing classic Web Workers (not Module Workers)
3 participants