react-app-rewire-babel-loader Babel配置增强工具
react-app-rewire-babel-loader 的妙用在于让你在没 eject 的 Create React App 项目中,也能吃上 ES6+ 的 npm 包。你遇到过这种情况:npm 上发现了个宝藏库,API 好用、文档清晰,可惜是用 ES6 写的,babel-loader
默认跳过node_modules
,直接跑不起来。用这个工具就能。
搭配 react-app-rewired,它能让你“偷偷改配置”,把你想要转译的包塞进 babel 的编译流程。用起来也不复杂,直接用 babelInclude
把你要转译的路径加进来就行了。
比如你发现了个动画库,效果炫酷、轻量,还用 ES6 module 写的?这时候就可以:
const { babelInclude } = require('customize-cra');
module.exports = function override(config, env) {
config = babelInclude([
path.resolve('src'),
path.resolve('node_modules/awesome-es6-lib')
])(config);
return config;
};
不过注意一点:这个库本身现在不再维护了,尤其是对 react-app-rewired v2+
,建议直接模仿它的实现,用 customize-cra 搭配 babelInclude
来搞定。
如果你刚好在搞 webpack + react 项目,还想看点配套的资源,像react-app-rewire-favicons-plugin、React 0.14 + Webpack + ES6这些也都蛮实用。
,react-app-rewire-babel-loader 是个小巧又实用的配置神器,如果你不想 eject,又想自定义 babel 行为,真的值得一试。
评论区