Tomáš Zato
Tomáš Zato

Reputation: 53191

Where to put webpack.config.js in React project?

I obtained already existing React project and I'm supposed to continue with it.I never used React before. I need to use web worker in the project - there's a project that suits my needs: Worker Loader

The suggest one adds this to their webpack.config.js:

  module: {
    rules: [
      {
        test: /\.worker\.js$/,
        use: { loader: 'worker-loader' }
      }
    ]
  }

Then use worker like this:

import Worker from './file.worker.js';

const worker = new Worker();

But even though I can see that my React project uses webpack and babel o the background, there is no .babelrc or webpack.config.js in the project. This is how I run the project:

npm start

That actually runs react-scripts start based on what I see on package.json:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },

Neither webpack nor babel are in package.json dependencies, so I really have no idea how are they run by React. I would really appreciate if someone explained to me how does it work and how to configure webpack to use worker loader.

Upvotes: 2

Views: 5848

Answers (2)

akanksha-hp
akanksha-hp

Reputation: 86

You need to run npm run eject first.

This command will copy all of your configuration files into your project(including webpack.config.js). However, run this command only if you MUST because although you will have full control over your configuration, you'll be on your own. React won't manage the configuration for you anymore.

Upvotes: 4

alernerdev
alernerdev

Reputation: 2064

Not sure 100%, but I think this app was created using react starter kit called Create React App and it comes with a package called react-scripts which is a wrapper around the build tools -- webpack being one of them. So lookup that stuff in npm. It might be all taken care of for you Also, see what you have under node_modules directory --i am guessing it's all there

Upvotes: 0

Related Questions