Method of configuring Webpack in Create-react-app Scaffold

Source: Internet
Author: User

Overview

The react-scripts in the Create-react-app Scaffold can (1) help us to download the required Webpack dependencies automatically, (2) write a Nodejs server script code, (3) Use Express HTTP servers, (4) and help us the configuration file is hidden .

So what if we need additional configuration Webpack? such as the addition of MD loader.

I have summed up 2 ways for future development, and I believe it will be useful for others.

Method One

Run the following command to display the configuration file:

npm run eject//然后输入Y

After entering the project directory will be more than a Congfig folder, there is a webpack configuration file.

However, this process is irreversible, so it can no longer be hidden back when it appears.

Method Two

( to modify the Babel plug-in implementation on-demand loading antd as an example, modify the other configuration can be modeled this way.) )

(1) using react-app-rewired (a community solution for customizing the Create-react-app) and Babel-plugin-import (a Babel management-loaded plugin).

$ yarn add react-app-rewired --dev$ yarn add babel-plugin-import --dev//或者使用npmnpm install react-app-rewired --devnpm install babel-plugin-import --dev

(2) Modify the boot configuration in the Package.json.

/* package.json */"scripts": {    "start": "react-app-rewired start",    "build": "react-app-rewired build",    "test": "react-app-rewired test --env=jsdom",}

(3) Create a config-overrides.js in the project root directory to modify the default configuration.

/* config-overrides.js */const { injectBabelPlugin } = require('react-app-rewired');module.exports = function override(config, env) {    config = injectBabelPlugin(['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }], config);    return config;  };

Method of configuring Webpack in Create-react-app Scaffold

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.