Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Webpack 5 for web worker loading example is broken #2

Open
kerel-fs opened this issue Sep 3, 2021 · 0 comments
Open

Using Webpack 5 for web worker loading example is broken #2

kerel-fs opened this issue Sep 3, 2021 · 0 comments

Comments

@kerel-fs
Copy link

kerel-fs commented Sep 3, 2021

🐞 Problem

While following the Getting Started guide I failed to use the native worker support introduced with Webpack 5. Following the Webpack 4 approach of loading web workers using worker-loader works though.

🗒️ Steps to reproduce

The files at kerel-fs/spectranalysis@eb1e672 contain the example project of spectroplot-js, running fine while using worker-loader. Changing ./src/easy.js#L14 from

import spectroplot_worker from 'worker-loader?filename=js/spectroplot.[hash].worker.js!spectroplot/lib/worker.js'

to

const SpectroplotWorker = Worker(new URL('spectroplot/lib/worker.js', import.meta.url))

(as described in the README) will generate the following error on page load:

Uncaught TypeError: Worker constructor: 'new' is required
    <anonymous> webpack://demo3/./src/easy.js?:21
    js http://localhost:8080/example.bundle.js:495
[...]

# For context, src/easy.js#L21:
spectroplot_worker = Worker(new URL(/* asset import */ __webpack_require__(/*! spectroplot/lib/worker.js */ "./node_modules/spectroplot/lib/worker.js"), __webpack_require__.b));

💡 Possible solutions

My first attempt at a quick fix was to just create a new Worker instance by using the new operator (#1). This does not work since spectroplot-js itself tries to create instances of workerOrUrl:

renderWorker[i] = (typeof workerOrUrl === 'string') ? new Worker(workerOrUrl) : new workerOrUrl()

Probably some adjustment is needed here for the native web worker support of Webpack 5, but I didn't come of with a working patch yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant