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

the worker is located on a path inaccessible to the bundle #51

Open ravecat opened 3 years ago

ravecat commented 3 years ago

I am a bit confused about building the project from ts example https://github.com/darionco/rollup-typescript-webworkers. how do the bundle will link to the worker if it is located on an inaccessible path and only dist/** send into the published package

Screen Shot 2021-05-27 at 21 07 43

could you clarify that and give an example of rollup config

I've expected similar structure after bundle

dist
├── index.js
├── worker
      ├──worker.js

with current plugins section

import { nodeResolve } from "@rollup/plugin-node-resolve";
import ts from "@wessberg/rollup-plugin-ts";
import webWorkerLoader from "rollup-plugin-web-worker-loader";
...
[
  nodeResolve(),
  webWorkerLoader(),
  ts({
    tsconfig: "tsconfig.build.json",
    hook: {
      declarationStats: (declarationStats) =>
        console.warn(JSON.stringify(declarationStats)),
    },
  }),
  ...
]

I get warning

.../rollup-typescript-webworkers/src/index.ts → dist/next/index.js...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
web-worker:./Worker.ts (imported by src/index.ts)
darionco commented 3 years ago

with your current configuration the worker gets embedded in the index file as a base64 string so there is no need for a standalone worker file. If you want to output an explicit worker file, use the inline option:

...
[
  ...
  webWorkerLoader({ inline: false }).
  ...
]
...

as for the warning, I assume that your worker source file is in the worker folder? if so, your import should be:

import WebWorker from 'web-worker:./worker/Worker.ts';
ravecat commented 3 years ago

@darionco thank you for quick answer and for a little misunderstanding, I am trying to build a project from your example https://github.com/darionco/rollup-typescript-webworkers and get current warning and build structure

So, yes worker exist in right path. How do js resolve web-worker:./worker/Worker.ts? I mean npm-published module. Or maybe I expect impossible from that plugin

darionco commented 3 years ago

I am not sure I understand the question, I'll try to explain how the plugin works by default.:

by following those steps, the dist folder doesn't need to ship a worker file because the file is embedded into the main JavaScript file (index.js in the example you linked).

If what you want is for the worker file to not be embedded into the main JavaScript file, that can be done using the option I suggested above:

 ...
[
  ...
  webWorkerLoader({ inline: false }).
  ...
]
...

Otherwise you could look into using rollup-plugin-off-main-thread since that plugin main functionality is code splitting while this plugin's main functionality is embedding the workers in the main file.

Let me know if that helps.