Vadim Tikanov
Vadim Tikanov

Reputation: 671

Webworker import fails to resolve

I'm trying to tweak this component: https://github.com/Lucas-C/Nonogram

I added the src folder to my project. When using Game and Editor classes, no problem arise. When I try to import Solver class, however, I get an error:

@parcel/core: Failed to resolve 'worker!./worker.ts’ from from './nonogram_lib/Solver.ts’

It looks like this import fails. How can I fix that?

import SolverWorker from 'worker!./worker.ts'

My package.json:

"dependencies": {
    "parcel": "^2.8.0"
  },
  "devDependencies": {
    "typescript": "^4.6.4"
  },

My tsconfig.json:

{
  "compilerOptions": {
    "noEmit": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es6"
  }
}

Upvotes: 0

Views: 1091

Answers (1)

hackape
hackape

Reputation: 19967

This line in Solver.ts causes problem.

import SolverWorker from 'worker!./worker.ts'

export default class Solver extends Nonogram {
  worker: Worker = new SolverWorker()
// ...

Like @webelo pointed out in comment, the worker! prefix is a rollup plugin hint, if you check the original repo uses rollup-plugin-bundle-worker.

Because you use parcel as bundler, you need to convert it into parcel's way of importing a worker, which should be:

export default class Solver extends Nonogram {
  worker: Worker = new Worker(
    new URL('worker.ts', import.meta.url),
    {type: 'module'}
  );

Worth noting that, new URL('worker.ts', import.meta.url) and new Worker(...), even though both are web standard APIs, they actually have some parcel magic mixed-in behind the scene. Check the link for details.

Upvotes: 1

Related Questions