babel

链接地址

在index.js中写入js6的语法如

let fn = (){
console.log('this is es6')
}

执行npm run build

在打包出来的js文件中搜索fn会看到如下片段结果mode状态为development

fn = ()=>{\r\n    console.log(\"this is es6\")\r\n}\r\nfn()\r\n\n\n//# sourceURL=webpack:///./src/index.js?")

可以看到箭头函数依旧以箭头函数的方式输出,而没有以es5的方式输出

接下来安装babel-loader

npm install -D babel-loader @babel/core @babel/preset-env webpack

在module rules中进行配置

{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}, }
]
}

再次打包,执行后在js文件中查看fn可以发现如下代码

fn = function fn() {\n  console.log(\"this is es6\");\n};\n\nfn();\n\n//# sourceURL=webpack:///./src/index.js?")

可见此时的箭头函数已经被转化为es5的语法了

在src下将要打包的js文件中写如下高级语法

class Test{
a = 11
}

打包时发现报错并且出现以下提示

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

也就是下载安装

npm i @babel/plugin-proposal-class-properties -D

接着重新进行配置

{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'] ,
plugins:[
'@babel/plugin-proposal-class-properties'
]
}, }
]
}
``

最新文章

  1. 性能计数器与profiler的组合性能诊断
  2. json和jsonp(json是目的,jsonp是手段)
  3. HP_UX系统批量创建LV或raw设备的Shell 脚本
  4. Android Native jni 编程入门
  5. 关于vue.js 组件的调用
  6. [Effective JavaScript 笔记]第33条:使构造函数与new操作符无关
  7. HTML前端
  8. 《Python基础教程(第二版)》学习笔记 -> 第七章 更加抽象
  9. [Raobin] Ext.net在页面中以窗体的形式打开另外的页面
  10. [C入门 - 游戏编程系列] 贪吃蛇篇(四) - 食物实现
  11. PHP学习笔记--1,不总结,不掌握,不明白!
  12. Linux 安装依赖库
  13. Python的time和datetime模块
  14. Gate One——用web展示Terminal(安装)
  15. 再次实践 MySQL chart - 每天5分钟玩转 Docker 容器技术(166)
  16. [Java]LeetCode690. 员工的重要性 | Employee Importance
  17. Android + https 实现 文件上传
  18. Android 开发 Activity里获取View的宽度和高度 转载
  19. Tomcat9报错 The valid characters are defined in RFC 7230 and RFC 3986
  20. [Tensorflow] Object Detection API - predict through your exclusive model

热门文章

  1. 模块 heapq_堆排序
  2. .Net Core 跨平台开发实战-服务器缓存:本地缓存、分布式缓存、自定义缓存
  3. K-Folds cross-validator-K折交叉验证实现
  4. html+css实现图片或元素的垂直、水平同时居中的多种方法
  5. B - Charlie's Change
  6. 手动搭建I/O网络通信框架2:Socket和ServerSocket入门实战,实现单聊
  7. iapp,iapp http请求,iapp解析json数据
  8. 配置并验证Split分离解析
  9. php--phpstudy更新数据库版本后,无法一键启动
  10. websocket聊天室