I am trying to load a web worker from inside a custom npm package.
[App] -> [my npm package -> load worker]
I use worker-loader inside the npm package to load the worker file:
import Worker from 'worker-loader!./myPackage.worker';
const worker = new Worker();
My webpack config inside the local npm package:
{
entry: './packages/myPackage/src/index.ts',
output: {
library: 'myPackage',
libraryTarget: 'umd',
path: path.resolve(__dirname, './packages/myPackage/dist'),
filename: "index.js"
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader"
},
{
test: /\.worker\.js$/,
loader: 'worker-loader',
options: {
inline: "no-fallback"
}
},
]
},
}
Unfortunately every time I do so, my app cannot locate the worker file. The app is trying to get the worker from ./static/js/myPackage.worker.js but the file is not found and an error is raised Uncaught SyntaxError: Unexpected token '<'.
What am I doing wrong here? Ideally I would like the worker file to be bundled with the npm package so no additional file needs to be loaded. How can achieve this behavior?