Webpack初学遇到的问题

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

可能缘由:vue

  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/
}

这样会报错:node

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的,webpack

安装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来完成转译过程的。web

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中,由于它不会污染全局变量正则表达式

相关文章
相关标签/搜索