Skip to content

How can i custom config Webpack without "npm run eject" #10307

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

Open
ghost opened this issue Dec 28, 2020 · 4 comments
Open

How can i custom config Webpack without "npm run eject" #10307

ghost opened this issue Dec 28, 2020 · 4 comments

Comments

@ghost
Copy link

ghost commented Dec 28, 2020

Sometimes I want to add webpack plugins externally. But how can I do this without "reject"?

i see a node_modules/react-script/config/webpack.config.js file.

However, there is more than one "plugin" definition in this file. Is there any way to configure here? Or can a separate file be specified for the configurations in the next version? for example:

like this node_modules/react-script/config/custom/webpack.config.js

Thank you

@ghost ghost added the needs triage label Dec 28, 2020
@teo-garcia
Copy link

Create React App comes with its own configuration that seeks to cover the most used Webpack plugins. However, adding new plugins obligates the user to eject it if no third library is used.

A popular approach to override it without ejecting your project is using

Normally, both packages work together. You can find more info in customize-cra's repository.

@cirosantilli
Copy link

This seems to be the central issue where this is being discussed: #6303

@ashvin777
Copy link

I was able to change Webpack configurations without ejecting the CRA app. It was done using the rewire npm package with minimal changes -

First change the scripts in pkg.json to below

   "start": "node scripts/start.js",
   "build": "node scripts/build.js",

scripts/start.js

const rewire = require('rewire');
const defaults = rewire('react-scripts/scripts/start.js');
const webpackConfig = require('react-scripts/config/webpack.config');
const webpackExternals = require('./webpack-externals');

//In order to override the webpack configuration without ejecting the create-react-app
defaults.__set__('configFactory', (webpackEnv) => {
  let config = webpackConfig(webpackEnv);

  //Customize the webpack configuration here, for reference I have updated webpack externals field
  config.externals = [..];

  return config;
}); 

scripts/build.js

const rewire = require('rewire');
const defaults = rewire('react-scripts/scripts/build.js');
const webpackExternals = require('./webpack-externals');

//In order to override the webpack configuration without ejecting the create-react-app
const config = defaults.__get__('config');

//Customize the Webpack configuration here,  for reference I have updated Webpack externals field
config.externals =[..]; 

This is working as expected in one of my applications. The good thing about this is it doesn't break the create-react-app structure and is very simple to integrate compared to react-app-rewired

@adamerose
Copy link

Could this possibly be added as a feature directly in create-react-app?

It seems like craco is currently the most maintained package for this. react-app-rewired claims it is "lightly" maintained and recommends using NextJS instead. There is also customize-cra and rescripts. It would be nice if there was an official solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants