[英]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.