最近看到网上都是使用eject这种方式修改官方脚手架配置,但是也有少部分人因为eject不可逆,排斥它。
推荐一种打补丁的方式对脚手架进行少量修改。。。
先新建一个script文件夹。
scripts/customized-config.js
/*
本模块运行react-scripts里的脚本 (Create React App)
可以自定义webpack配置,通过在项目根目录创建"config-overrides.dev.js" 、 "config-overrides.prod.js" 文件.
A config-overrides file should export a single function that takes a
config and modifies it as necessary.
module.exports = function(webpackConfig) {
webpackConfig.module.rules[0].use[0].options.useEslintrc = true;
};
*/
var rewire = require('rewire');
var proxyquire = require('proxyquire');
switch(process.argv[2]) {
// The "start" script is run during development mode
case 'start':
rewireModule('react-scripts/scripts/start.js', loadCustomizer('../config-overrides.dev'));
break;
// The "build" script is run to produce a production bundle
case 'build':
rewireModule('react-scripts/scripts/build.js', loadCustomizer('../config-overrides.prod'));
break;
// The "test" script runs all the tests with Jest
case 'test':
// Load customizations from the config-overrides.testing file.
// That file should export a single function that takes a config and returns a config
let customizer = loadCustomizer('../config-overrides.testing');
proxyquire('react-scripts/scripts/test.js', {
// When test.js asks for '../utils/createJestConfig' it will get this instead:
'../utils/createJestConfig': (...args) => {
// Use the existing createJestConfig function to create a config, then pass
// it through the customizer
var createJestConfig = require('react-scripts/utils/createJestConfig');
return customizer(createJestConfig(...args));
}
});
break;
default:
console.log('customized-config only supports "start", "build", and "test" options.');
process.exit(-1);
}
// Attempt to load the given module and return null if it fails.
function loadCustomizer(module) {
try {
return require(module);
} catch(e) {
if(e.code !== "MODULE_NOT_FOUND") {
throw e;
}
}
// If the module doesn't exist, return a
// noop that simply returns the config it's given.
return config => config;
}
function rewireModule(modulePath, customizer) {
// Load the module with `rewire`, which allows modifying the
// script's internal variables.
let defaults = rewire(modulePath);
// Reach into the module, grab its global 'config' variable,
// and pass it through the customizer function.
// The customizer should *mutate* the config object, because
// react-scripts imports the config as a `const` and we can't
// modify that reference.
let config = defaults.__get__('config');
customizer(config);
}
上面这个js脚本中,还要执行下面命令
npm install --save rewire proxyquire
再新建开发环境替换配置文件
config-overrides.dev.js
(sass配置方式)
npm install --save-dev sass-loader node-sass
const path = require('path');
module.exports = function(config) {
// 使用你自己的 ESLint
let eslintLoader = config.module.rules[0];
eslintLoader.use[0].options.useEslintrc = true;
// Add the SASS loader second-to-last
// (last one must remain as the "file-loader")
let loaderList = config.module.rules[1].oneOf;
loaderList.splice(loaderList.length - 1, 0, {
test: /\.scss$/,
use: ["style-loader", "css-loader", "sass-loader"]
});
}
还需要修改package.json让他执行重写的脚本
"scripts": {
"start": "node scripts/customized-config start",
"build": "node scripts/customized-config build",
"test": "node scripts/customized-config test --env=jsdom",
}
下面有老外大神的示例demo