前言:create-react-app 项目名,创建出一个项目来,使用yarn start的时候发现输出很多的日志信息,这些日志信息开发的时候像我这种只有做项目优化的时候才想看到,但是进行开发的时候我们并不想看到。
如图:
如何使用 craco修改webpack参考这篇
create-react-app craco进行webpack配置(修 改篇)
不过不能完全安照他的来,安装的时候要使用yarn add
来安装使用npm可能装不上。
然后我只说怎么屏蔽这些输出,如果在craco.config.js文件你直接这样直接加stats为’errors-only’你发现他并不会生效。
module.exports = {
webpack: {
alias: {
'@': pathResolve('src'),//别名
},
stats:'errors-only',//直接加stats为'errors-only'你发现他并不会生效
},
}
这时候就要想到用configure给他一个webpackConfig的返回值它才会生效。
webpack: {
configure: (webpackConfig, { env, paths }) => {
webpackConfig.stats='errors-only'//这样才会生效
// console.log(webpackConfig);//不懂怎样修改的可以看一下webpackConfig有啥,然后对它的一直配置进行修改。
return webpackConfig
},
我的练手craco.config.js配置
const CracoLessPlugin = require('craco-less')
const path = require('path')
const pathResolve = pathUrl => path.join(__dirname, pathUrl)
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
// const webpack=require('webpack');
const WebpackBar = require('webpackbar');
module.exports = {
webpack: {
configure: (webpackConfig, { env, paths }) => {
webpackConfig.stats='errors-only'//这样才会生效
// 修改build的生成文件名称
paths.appBuild = 'dist';
webpackConfig.output ={
...webpackConfig.output,
path:path.resolve(__dirname,'dist'),
publicPath:'/'
}//修改输出为dist文件
// console.log(webpackConfig);//不懂怎样修改的可以看一下webpackConfig有啥,然后对它的一直配置进行修改。
return webpackConfig
},
alias: {
'@': pathResolve('src'),//别名
},
plugins:[
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false,
//代码最紧凑
drop_debugger: true,
//清楚console
drop_console: true,
},
},
sourceMap: false,
parallel: true,
}),
new WebpackBar()//增加一个进度条
],
//抽离公用模块
optimization: {
splitChunks: {
cacheGroups: {
commons: {
chunks: 'initial',
minChunks: 2, maxInitialRequests: 5,
minSize: 0
},
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
}
}
}
},
babel: {
plugins: [
['import', { libraryName: 'antd', style: true }],
['@babel/plugin-proposal-decorators', { legacy: true }]
]
},
plugins: [
{
plugin: CracoLessPlugin,
options: {
// 根据 less-loader 版本的不同会有不同的配置
lessLoaderOptions: {
lessOptions: {
modifyVars: {},
javascriptEnabled: true
}
}
}
}
]
}
然后这样看起来就舒服多了
更多属性值可以看官网