darionco / rollup-plugin-web-worker-loader

Rollup plugin to load Workers. Supports inlining, dependencies, source maps, NodeJS and browsers.
MIT License
112 stars 31 forks source link

Why not show any example of the correct workerLoader configuration syntax ?? #71

Closed pcbuildpluscoding closed 1 year ago

pcbuildpluscoding commented 1 year ago

Hi I'm trying to adapt your rollup-typescript-webworkers project from a web-worker type to a shared-worker WorkerFactory type My attempt to update the workers.d.ts is apparently incorrect =>

declare module 'shared-worker:*' {
    const WorkerFactory: new () => SharedWorker;
    export default WorkerFactory;
}

OR my attempt to define the 'shared-worker' plugin option in rollup.config.js is wrong =>

const workerLoader = require('rollup-plugin-web-worker-loader');
...
        plugins: [
            ...
            workerLoader({
              'shared-worker?': /\.\/Worker\.ts$/
            }),
            ...
       ]

I also import the shared-worker module into index.ts =>

import SharedWorker from 'shared-worker:./Worker.ts';

When I run yarn build:all I get this error =>

!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
shared-worker:./Worker.ts (imported by src/index.ts)

I assume this means the workerLoader plugin is not loading the shared-worker dependency. Correct? Your readme content does not actually show an example of a workerLoader configuration. The related section shows this =>

import webWorkerLoader from 'rollup-plugin-web-worker-loader';

export default {
    entry: 'src/index.js',
    plugins: [
        webWorkerLoader(/* configuration */),
    ],
    format: 'esm',
};

Why not add an example that shows the expected syntax? Any help with my issue is appreciated

pcbuildpluscoding commented 1 year ago

vite has builtin webworker and sharedworker support

import MyWorker from './worker?sharedworker'

const worker = new MyWorker()

Or to inline the worker as base64 strings

import MyWorker from './worker?worker&inline'

const worker = new MyWorker()