您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

babel编译js文件

彭世瑜 发布时间:2020-04-17 15:00:24 ,浏览量:1

# 安装
$ cnpm install --save-dev @babel/core @babel/cli

# 转换
$ ./node_modules/.bin/babel script.js
# 或者
$ npx babel script.js

要编译的文件 script.js

[1, 2, 3].map(n => n + 1);

编译测试

# 编译(发现没有变化)
$ npx babel script.js 
[1, 2, 3].map(n => n + 1);


# 安装插件
$ cnpm i -D @babel/plugin-transform-arrow-functions


# 指定插件
$ npx babel script.js --plugins=@babel/plugin-transform-arrow-functions

[1, 2, 3].map(function (n) {
  return n + 1;
});

常用参数 –out-file/-o 指定输出文件名 –watch/-w 监控文件变化 –out-dir/-d 指定输出文件夹

使用presets

preset-env 处理es6+规范语法的插件集合

$ cnpm install --save-dev @babel/preset-env

新建配置文件 babel.config.json

{
    "presets": [
        [
            "@babel/env"
        ]
    ]
}

编译测试 demo.js

var name = () => {};
$ npx babel demo.js

编译结果

"use strict";

var name = function name() {};

参考 https://babeljs.io/docs/en/babel-cli Babel 配置用法解析

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1795s