GoogleChromeLabs / wasm-bindgen-rayon

An adapter for enabling Rayon-based concurrency on the Web with WebAssembly.
https://github.com/RReverser/wasm-bindgen-rayon
Apache License 2.0
404 stars 35 forks source link

Circular dependency between chunks with runtime using webpack as a bundler #32

Closed timoe closed 2 years ago

timoe commented 2 years ago

Description

I try to integrate wasm-bindgen-rayon to my web assembly, I'm using webpack as a bundler. When running webpack it ends up in a warning

Circular dependency between chunks with runtime (517, index)
This prevents using hashes of each other and should be avoided.

This happens after I added pub use wasm_bindgen_rayon::init_thread_pool; to the lib.rs file.

Or better, as the generated pkg/index.js file imports from workerHelpers.js via import { startWorkers } from './snippets/wasm-bindgen-rayon-7afa899f36665473/src/workerHelpers.js'; which imports const pkg = await import('../../..');.

So, I see the circular dependency and have not idea how to resolve that.

Versions

from Cargo.toml

wasm-bindgen = "0.2.45"
rayon = "1.5.3"
wee_alloc = { version = "0.4.2", optional = true }

[dependencies.wasm-bindgen-rayon]
version = "1.0.3"

Webpack

    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0",
    "webpack-dev-server": "^4.11.1",

Files

here is my webpack config

const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

const dist = path.resolve(__dirname, "dist");

module.exports = {
  mode: "production",
  entry: {
    index: "./js/index.js"
  },
  output: {
    path: dist,
    filename: "[name].js"
  },
  devServer: {
    compress: false,
    port: 9000
  },
  plugins: [
    new CopyPlugin([
      path.resolve(__dirname, "static")
    ]),

    new WasmPackPlugin({
      crateDirectory: __dirname,
      extraArgs: '--target web',
    }),
  ],
  experiments: {
    topLevelAwait: true
  }
};
RReverser commented 2 years ago

Yup, there is a circular dependency and it's intentional (module creating a Worker, but then Worker importing the original module). Webpack warns about those because they might be accidental, but they're a perfectly valid part of ES modules.