简体   繁体   English

如何将React-app-rewired与Customize-CRA集成

[英]How to integrate React-app-rewired with Customize-CRA

I want to override the Webpack config by react-app-rewired. 我想通过react-app-rewired覆盖Webpack配置。 But I using Ant design for my project, so I must use Customize-CRA to import Babel plugin, etc. How to using React-app-rewired and Customize-CRA together. 但是我在项目中使用Ant设计,因此必须使用Customize-CRA导入Babel插件等。如何将React-app-rewired和Customize-CRA一起使用。

The config-overrides.js for React-app-rewired as below: React-app-rewire的config-overrides.js如下所示:

module.exports = function override(config, env) {
  config.module.rules = config.module.rules.map(rule => {
        if (rule.oneOf instanceof Array) {
            return {
                ...rule,
                oneOf: [
                    {
                        test: /\.(svg|png|jpg|jpeg|gif|bmp|tiff)$/i,
                        use: [
                            {
                                loader: 'file-loader',
                                options: {
                                    name: '[path][name]-[hash:8].[ext]'
                                }
                            }
                        ]
                    },
                    ...rule.oneOf
                ]
            };
        }

        return rule;
    });

  return config;
}

The config-overrides.js for Customize-CRA as below: Customize-CRA的config-overrides.js如下:

const {override, fixBabelImports, addLessLoader, addDecoratorsLegacy, disableEsLint} = require('customize-cra');

module.exports = override(
  addDecoratorsLegacy(),
  disableEsLint(),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),
  addLessLoader({
    javascriptEnabled: true,
    modifyVars: {'@primary-color': '#a50052'},
  }),
);

Thank you. 谢谢。

I think I ran into the same problem you did. 我想我遇到了您遇到的相同问题。 The customize-cra override takes any number of override functions as arguments. Customize-cra覆盖将任意数量的覆盖函数作为参数。 Each function is given config as its first argument. 每个函数都以config作为其第一个参数。 Think of it as a compose function. 将其视为一个组合功能。 Take your existing override export, put it into a function you define, I called myOverrides or something, then export customize-cra's override with your override function as one of the arguments. 进行现有的覆盖导出,将其放入您定义的函数中(我称为myOverrides或其他名称),然后使用您的覆盖功能作为参数之一导出custom-cra的覆盖。

before: 之前:

module.exports = function override(config, env){
  // do stuff to config
  return config
}

after: 后:

function myOverrides(config) {
  // do stuff to config
  return config
}

module.exports = override(
  myOverrides,
  addDecoratorsLegacy(),
  disableEsLint(),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),
  addLessLoader({
    javascriptEnabled: true,
    modifyVars: {'@primary-color': '#a50052'},
  }),
);

Putting react-app-rewired config AFTER customize-cra worked for me. 将react-app-wired配置放到custom-cra之后对我来说很有效。 It doesn't work though if I assign an object to config, but works if I fix config line by line. 如果我将一个对象分配给config,则不起作用,但如果逐行修复config,则它起作用。 I'm sure there is a more elegant solution. 我敢肯定有一个更优雅的解决方案。

 module.exports = function override(config, env) { const APP = process.env.REACT_APP_APP const BRAND = process.env.REACT_APP_BRAND addWebpackAlias({ ['@app-config']: path.resolve(__dirname, `./src/brands/${BRAND}/app`), }) config.entry = `./src/${APP}/index.js` config.resolve.alias['@app-config'] = path.resolve(__dirname, `./src/brands/${BRAND}`) config.resolve.modules = [ path.join(__dirname, `src/brands/${BRAND}`) ].concat(config.resolve.modules) return config }; 

I found tips from document of ant design: 我从蚂蚁设计文档中找到了一些技巧:

const {
  override,
  fixBabelImports,
} = require("customize-cra");


module.exports = override( 
  fixBabelImports("import", {
    libraryName: "antd-mobile",
    style: 'css'
  })
);

Ref: 参考:

https://mobile.ant.design/docs/react/use-with-create-react-app#Use-modularized-antd-mobile https://mobile.ant.design/docs/react/use-with-create-react-app#Use-modularized-antd-mobile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 custom-cra, (react-app-rewired) 如何覆盖路径 - customize-cra, (react-app-rewired) how to override paths 添加Webpack Alias使用react-app-rewired和customize-cra - Add Webpack Alias With react-app-rewired and customize-cra 使用 react-app-rewired 和 customize-cra 时如何配置 Jest? - How configure Jest when using react-app-rewired and customize-cra? 如何确定 css 在我的使用 webpack 和customize-cra 的创建反应应用程序构建中的顺序? - How to determine the order of css in my create react app build with webpack and customize-cra? 如何配置 Storybook 以使用 React-App-Rewired? - How Can I Configure Storybook to Use React-App-Rewired? react-app-rewired 构建抛出尝试导入错误 - react-app-rewired build throws attempted import error 使用react-app-rewired的nginx进行React生产 - React production using react-app-rewired nginx react-app-rewired 并非在开发模式下编译所有文件 - react-app-rewired compiles not all files in dev mode sh:1:react-app-rewired:构建 Dockerfile 时未找到 - sh: 1: react-app-rewired: not found when building Dockerfile react-app-rewired 用于启用 Web Worker 无法构建 - react-app-rewired used to enable web workers fails to build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM