timarney / react-app-rewired

Override create-react-app webpack configs without ejecting
MIT License
9.77k stars 425 forks source link

__webpack_require__(...) is not a function #557

Closed polisen closed 2 years ago

polisen commented 3 years ago

Hey - I'm using customize-cra and react-app-rewired to override the webpack config usually abstracted away in a create-react-app.

I'm importing the random-int package to the worker file - which is the import that fails.

config-overrides.js

const { override, addWebpackModuleRule, babelInclude } = require("customize-cra");
const path = require('path')
module.exports = override(
  addWebpackModuleRule({
    test: /\.worker\.(js|ts)$/i,
    use: [
      {
        loader: "comlink-loader",
        options: {
          singleton: true,
        },
      },
    ],
  })
);

This is the correct syntax - but now it complains about that webpack doesn't have a require function - which is to be expected. Supposedly something wasn't transpiled by babel but I can't figure out where to even begin.

Do you geniuses have any idea?

dawnmist commented 3 years ago

I haven't seen the comlink-loader used before for workers. Reading their issues list, there's a couple of potential issues that may help.

  1. Webpack 5 compatibility: the project hasn't been updated since Jan 2020, and there is an open issue for changes required to make it compatible with Webpack 5. For them it was complaining about __webpack_exports__ rather than the require, so their solution might not help you but it still would be worth looking at. There is one option people were able to set to get it working: https://github.com/GoogleChromeLabs/comlink-loader/issues/34

  2. Another person found that the singleton mode wouldn't work without adding a babel loader portion to the use array as well. If the Webpack 5 issue's option doesn't help, the addition of babel-loader to the list of use loaders for the worker might: https://github.com/GoogleChromeLabs/comlink-loader/issues/36

Failing those, are you able to provide an example repository that has this issue so that I can trace through what is happening during compilation?

jobh commented 2 years ago

FWIW: I had a similar problem. Two issues in my case:

timarney commented 2 years ago

Closing this given no recent activity.