-
Notifications
You must be signed in to change notification settings - Fork 920
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
documentation: Web Worker Support #1280
Comments
@FredKSchott , I have been looking into this issue and I'm running into a bit of a snag. I followed the documentation in webpack on how to setup a web worker with workbox. But Im running into a bit of a snag. These are the changes I made as per the webpack documentation Is this something you can help me out with? If thats not feasible, I do plan to raise an issue on webpack github repo to be able to get it to work. |
Heya, thanks for snowpack, really enjoying it so far. Running into a bit of a problem where FF&Safari won't work because both of the WebWorker implementations don't seem to support edit: realized this is about documentation, I should probably take it elsewhere 🙊 |
I have a difficulty configuring this setup:
currently, my code looks like this: const worker = new Worker('./worker.js');
worker.onmessage = ({ data }) => console.log('From Worker ->', data);
worker.postMessage(MsgToWorker.HiWorker); Note I do have In DEVWith that code I get
if I change my code to const worker = new Worker('./worker.ts'); // <---- .ts I got the same In PRODWhen I generate with // generated code
const worker = new Worker('./worker.ts'); // <-- should it be .js? Thus, I don't know how to configure that properly :( UPDI was able to start fetching a worker with this: const worker = new Worker('./_dist_/worker.js'); // <-- .js + _dist_ Which seems unintuitive to me. |
I was also caught by the need to add the @FredKSchott I think this might be snowpack specific, and so maybe worth adding to your checklist for this issue? |
I've been trying web workers with snowpack, and I've come up with the required steps to make it work (ish) This snippet should nearly work: const worker = new Worker(
new URL('../../../my-worker.js', import.meta.url),
{
name: 'my-web-worker',
type: import.meta.env.MODE === 'development' ? "module" : "classic"
}
); Some notes for the documentation: When creating a Worker, snowpack relies on the native support from webpack 5. The first argument of the Worker is a URI with the location of the file in the final bundle. Please note that this URI is not fully resolvable against your local filesystem. The URI should point to the address where the browser can get the web worker file. In order to help Webpack to create the right URI, we have to pass When using typescript, the file extension should always be The second argument of the Worker is an object with options. In development, Snowpack will emit a module and we need to pass that configuration to the Worker constructor. Otherwise, the script won't be able to import any dependencies. Please note, that support for web worker modules is limited at the moment In production, however, the type of your web worker will depend on the build configuration. If not using module as output, then "classic" is the preferred option. TODOI would gladly formalise these notes in a PR. However, there is still something that I couldn't get to work. Snowpack's hmr-client has a reference to |
Tackling in #2027 |
const worker = new Worker(new URL('./worker.js', import.meta.url), {
type: import.meta.env.MODE === 'development' ? 'module' : 'classic'
})
We use Webpack's DefinePlugin to make available at compile-time a global variable
const { DefinePlugin } = require('webpack')
/** @type {import("snowpack").SnowpackUserConfig } */
module.exports = {
plugins: [
['@efox/snowpack-plugin-webpack5', {
/** @param {import("webpack").Configuration } config */
extendConfig: (config) => {
config.plugins.push(new DefinePlugin({
__SNOWPACK_ENV__: JSON.stringify('production')
}))
return config
}
}]
snowpack build You can also refer to my pull request for an example of how to serve the bundle via a local webserver. EDIT: |
Background
Now that Webpack can detect native web worker usage (
new Worker(new URL("./worker.js", import.meta.url))
) and bundle automatically, we can claim native web worker support. We've always had this support in snowpack, but due to the lack of support for this in older browsers you were limited by where you could run your application in production.Now that Webpack 5 automatically bundles this, you have support in both dev and prod.
Feature Request
The text was updated successfully, but these errors were encountered: