zoukankan      html  css  js  c++  java
  • babel 实践

    一、@babel/core

    var babel = require("@babel/core");
    babel.transform(code, options, function(err, result) { result; // => { code, map, ast } });

    二、@babel /cli

    babel src --out-dir lib

     三、presets

    preset 执行顺序从右向左

    **** @babel/preset-env
    实现原理:
    1、检测浏览器对JS特性的支持程度,比如通过通过 compat-table 这样的外部数据。

    2、将 JS特性 跟 特定的babel插件 建立映射,映射关系可以参考 这里

    3、stage-x 的插件不包括在内。

    4、根据开发者的配置项,确定至少需要包含哪些插件。比如声明了需要支持 IE8+、chrome62+,那么,所有IE8+需要的插件都会被包含进去。

    二、配置

    如果不写任何配置项,env 等价于 latest,也等价于 es2015 + es2016 + es2017 三个相加(不包含 stage-x 中的插件)。env 包含的插件列表维护在这里

    babel 默认只转换 js 语法,而不转换新的 API,比如 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(比如 Object.assign)都不会转码。

    const presets = [
      ['@babel/env', {
        // chrome, opera, edge, firefox, safari, ie, ios, android, node, electron
        // targets 和 browerslist 合并取最低版本
        targets: {
          ie: 8,
    
          // 是否使用 esmodules
          esmodules: true,
        },
    
        // 启用更符合规范的转换,但速度会更慢,默认为 `false`,从目前来看,是更严格的转化,包括一些代码检查。
        spec: false,
    
        // 有两种模式:normal, loose。其中 normal 更接近 es6 loose 更接近 es5
        loose: false,
    
        // "amd" | "umd" | "systemjs" | "commonjs" | "cjs" | false, defaults to "commonjs"
        modules: false,
    
        // 打印插件使用情况
        debug: true,
    
        // 按需增加移除一些功能
        // include: [],
        // exclude: [],
    
        // 增加 polyfills
        // 按需使用
        // useBuiltIns: 'usage',
        // 引用一次
        // useBuiltIns: 'entry',
        // 不引用,独自使用
        // useBuiltIns: false,
    
        // 强制使用所有的插件
        // forceAllTransforms: false,
    
        // 配置 browerslist 的位置
        // configPath: ,
        // 配置是否忽略 browerslist 文件
        // ignoreBrowserslistConfig: false,
    
        // 是否跳过 proposal 的文件
        // shippedProposals: false,
      }]
    ];
    
    const plugins = [
      [
        // 重用把 babel 注入的帮助代码, 依赖 @babel/runtime
        "@babel/plugin-transform-runtime",
        {
          // false || 2, 变成全局或者局部
          "corejs": false,
          
          // 生成器运行时的使用,变成全局或者局部
          "regenerator": true,
    
          // 帮助函数是变成 inline, 还是  lib
          "helpers": true,
    
          // helpers 切换成 esm
          "useESModules": true
        }
      ]
    ];
    
    module.exports = { presets, plugins };
    View Code

    useBuiltIns:

    "usage" | "entry" | false, defaults to false.

    四、plugins

    plugin 执行顺序 从左到右

    插件在 Presets 前运行

    五、@babel/plugin-transform-runtime

    默认情况下babel 会在每个文件中引入helper ,导致重复引入文件变大,@babel/plugin-transform-runtime,all of the helpers will reference the module @babel/runtime to avoid duplication across your compiled output. The runtime will be compiled into your build.

    @babel/runtime vs  @babel/runtime-corejs2.

    默认配置:

    { "plugins": [ [ "@babel/plugin-transform-runtime", { "absoluteRuntime": false, "corejs": false, "helpers": true, "regenerator": true, "useESModules": false } ] ] }

    @babel/runtime 默认 转化 不转换新的 API,比如 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象

    如果要转化用 @babel/runtime-corejs2. 同时开启  { corejs: 2 }

    @babel/plugin-transform-runtime 不转化实例方法

    五、@balbel/polyfill 

    babel 默认只转换 js 语法,而不转换新的 API,比如 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(比如 Object.assign)都不会转码。
     

    When used alongside @babel/preset-env,

    • If useBuiltIns: 'usage' is specified in .babelrc then do not include @babel/polyfill in either webpack.config.js entry array nor source. Note, @babel/polyfill still needs to be installed.

    • If useBuiltIns: 'entry' is specified in .babelrc then include @babel/polyfill at the top of the entry point to your application via require or import as discussed above.

    • If useBuiltIns key is not specified or it is explicitly set with useBuiltIns: false in your .babelrc, add @babel/polyfill directly to the entry array in your webpack.config.js.

     
  • 相关阅读:
    低功耗计算机视觉技术前沿,四大方向,追求更小、更快、更高效
    ELECTRA中文预训练模型开源,性能依旧媲美BERT
    局部敏感哈希源代码-python
    利用局部敏感哈希改进推荐系统实时性
    局部敏感哈希算法介绍
    为什么要用局部敏感哈希
    多采用panda的数据处理方式
    delphi:文件操作
    delphi:常用函数
    delphi:打印日志
  • 原文地址:https://www.cnblogs.com/shangyueyue/p/11097367.html
Copyright © 2011-2022 走看看