I am facing an issue while attempting to include a web worker in my custom npm package.
[App] -> [my npm package -> load worker]
Within the npm package, I am utilizing worker-loader
to import the worker file:
import Worker from 'worker-loader!./myPackage.worker';
const worker = new Worker();
The webpack configuration inside the local npm package looks like this:
{
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"
}
},
]
},
}
However, I am encountering an issue where my app is unable to find the worker file. It is looking for the worker at ./static/js/myPackage.worker.js
, but the file cannot be located, resulting in an error message of
Uncaught SyntaxError: Unexpected token '<'
.
I need the worker file to be bundled with the npm package so that no additional files are required. How can I accomplish this?