Webpack初学遇到的问题

引入图片资源时遇到的问题
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.

可能原因:

  1. 未安装处理图片的loader 解决方法:首先安装loader:npm install file-loader url-loader --save-dev,然后在 webpack.config.js 文件中的 module 添加 rules 配置
  2. 正则表达式错误,无法匹配到正确的rules,正确的正则表达式 /\.(jpg|png|svg)\??.*$/
引入vue-loader遇到的问题
rules: {
  test: /\.vue$/,
  use: [{
    loader: 'vue-loader', //将vue格式编写的组件转换为JavaScript模块
  }],
  exclude: /node_module/
}

这样会报错:

ERROR in ./src/App.vue
Module Error (from ./node_modules/vue-loader/lib/index.js):
vue-loader was used without the corresponding plugin.Make sure to include VueLoaderPlugin in your webpack config.

原因:
Vue-loader在15.*之后的版本都是 Vue-loader的使用都是需要伴生 VueLoaderPlugin的,

安装babel-loader的时候,如果babel-loader@8需要安装babel-core@7.x,如果你想要使用Babel6.x的话,需要babel-loader@7
在webpack.config.js中只需要配置babel-loader即可,不需要babel-core,但是需要安装babel-core
babel-core是babel转译器的核心,提供了babel转译的API,webpack中的bable-loader就是调用这些API来完成转译过程的。

Babel的功能包

babel-plugin-xxx: babel转译过程中使用到的插件,其中babel-plugin-transform-xxx是transform步骤使用的。
babel-preset-xxx: transform阶段使用到的一系列plugin
babel-polyfill: JS标准新增的原生对象和API的shim,实现上仅仅是core-js和regenerator-runtime两个包的封装。
babel-runtime: 功能类似babel-polyfill,一般用于library或者plugin中,因为它不会污染全局变量

相关推荐