Bug report
What is the current behavior?
Importing wasm in web workers throws an error.
If the current behavior is a bug, please provide the steps to reproduce.
Webpack configuration:
This is basically a merge of the worker + wasm examples.
const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const prod = process.env.NODE_ENV === 'production'
module.exports = {
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: prod ? 'js/main.[chunkhash].js' : 'js/main.js',
webassemblyModuleFilename: '[modulehash].wasm',
globalObject: 'this'
},
module: {
rules: [
{
test: /\.wasm$/,
type: 'webassembly/experimental'
}
]
},
plugins: [
new HtmlWebpackPlugin({ template: './src/index.html' }),
new webpack.LoaderOptionsPlugin()
]
};
index.js
var Worker = require("worker-loader!./worker")
var worker = new Worker()
worker.onmessage = event => {
console.log('mainthread got:', event.data)
}
worker.js
onmessage = event => {
import('../wasm/hello_world.wasm').then(module => {
postMessage(module.add_one(event.data))
})
}
This leads to :
Uncaught (in promise) TypeError: Cannot read property './src/wasm/hello_world.wasm' of undefined
But the same wasm import works when put in index.js.
What is the expected behavior?
We should be able to import wasm in workers just as we can in normal js files.
Other relevant information:
webpack version: 4.14
Node.js version: 10.5.0
Operating System: Arch
Additional tools:
This isn’t implemented yet…
Okay, thanks for the info.
For anyone trying to solve the same thing, i got it working like this: (see https://github.com/cars10/rust-wasm-webworker-example for an example)
webpack config
index.js
worker.js
I was able to use Wasm in a web worker by avoiding use of the
worker-loader
plugin and choosing thewebworker
target:webpack.config.js
worker.js
app.js