要更改 webpack 配置和构建脚本,您必须从 create-react-app 中弹出(我不推荐此步骤,因为它会破坏未来的兼容性)或使用 rewire 等工具来覆盖某些设置
看看这个。
https://github.com/timarney/react-app-rewired https://github.com/timarney/react-app-rewired
我个人使用的是重新布线
npm i rewire --save-dev
这是我过去为我的一个项目创建的示例配置,它运行得非常好!
- Create
build.js
- 更改你的 package.json 以便它运行 build.js
build.js
const rewire = require('rewire');
const defaults = rewire('react-scripts/scripts/build.js');
const config = defaults.__get__('config');
// Consolidate chunk files instead
config.optimization.splitChunks = {
cacheGroups: {
default: false,
},
};
// Move runtime into bundle instead of separate file
config.optimization.runtimeChunk = false;
// JS
config.output.filename = '[name].js';
// CSS. "5" is MiniCssPlugin
config.plugins[5].options.filename = '[name].css';
config.plugins[5].options.publicPath = '../';
然后在我的 package.json 中我像这样更改了 npm 脚本链接
(将运行 build.js 脚本的节点构建)
包.json
"scripts": {
"start": "react-scripts start",
"build": "node build && gulp",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
所以如果你真的想从 create-react-app 中弹出,你所要做的就是运行
npm run-script eject
然后你将得到一个新文件夹,其中包含 create-react-app 使用的所有配置
但正如我之前所说,没有理由不使用重新接线并覆盖配置而不是弹出。