react 使用rewired_create-react-app 通过 react-app-rewired 添加 webpack 的 alias

react-app-rewired

Override create-react-app webpack configs without ejecting

解决方案

翻阅 react-scripts 源码,发现 webpack 的配置只有两个,并且没有配置的提示。

alias: {

// @remove-on-eject-begin

// Resolve Babel runtime relative to react-scripts.

// It usually still works on npm 3 without this but it would be

// unfortunate to rely on, as react-scripts could be symlinked,

// and thus @babel/runtime might not be resolvable from the source.

'@babel/runtime': path.dirname(

require.resolve('@babel/runtime/package.json')

),

// @remove-on-eject-end

// Support React Native Web

// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/

'react-native': 'react-native-web',

}

接触 react-app-rewired 工具,我们可以拿到 create-react-app 当前的默认配置,在 config-overrides.js 的里面添加一下配置,就可以使用新的别名了。

module.exports = function override(config, env) {

// alias

config.resolve.alias = {

...config.resolve.alias,

'@': resolve('src')

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值