三、webpack学习过程中遇到的一些问题以及解决方法

1、使用devServer开发服务器实现自动化时,发现如果 项目构建后路径写成
     contentBase: resolve(__dirname, 'build'),  会出错,换了以下的写法后,就不会再报错了
  static:resolve(__dirname, 'build')

关于devServer的配置:

 devServer:{
        static:resolve(__dirname, 'build'),
        compress: true,
        port: 3000,  // 启动端口号
        open: true  //自动打开浏览器
    }

2、按照以下方法去配置css兼容性问题的话:

 {
      loader: 'postcss-loader',
      options: {
          ident: 'postcss',
          plugins: () => [
          // postcss 的插件
           require('postcss-preset-env')()
            ]
         }
}

会报错:“. PostCSS Loader has been initialized using an options object that does not match the API schema.\n - options has an unknown property 'plugins'.”

解决方法:

在webpack.config.js同级目录下新建文件postcss.config.js

写如下代码:

module.exports={
    plugins:[
    //使用postcss插件
        require('postcss-preset-env')
    ]
}

 3、在webpack5中使用eslint对js语法检查,使用eslint-loader,会报错

解决方法:安装如下插件:

npm i eslint eslint-webpack-plugin eslint-config-airbnb-base eslint-plugin-import -D

说明:eslint-webpack-plugin 用于替代eslint-loader(即将废弃)

在webpack.config.js配置文件中,做如下的配置:

const ESLintPlugin = require('eslint-webpack-plugin');

.....

plugins: [
         ...
        //js语法检查
        new ESLintPlugin({
            fix: true,
            extensions: ['js', 'json', 'coffee'],
            exclude: '/node_modules/'
        }),
    ],

在package.json文件中:

"eslintConfig": {
    "extends": "airbnb-base",
    "settings": {
      "import/resolver": {
        "webpack": {
          "config": "webpack.config.js"
        }
      }
    }
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值