美文网首页
webpack学习——loose模式

webpack学习——loose模式

作者: 说好的幸福2020 | 来源:发表于2019-08-28 10:30 被阅读0次

babel的松散模式将ES6代码转换为不遵循ES6语义的ES5代码。

babel中的许多插件有两种模式:

》正常模式 :尽可能地遵循ECMAScript 6的语义。

》松散模式:产生更简单的ES5代码。

通常,建议不要使用松散模式。优点和缺点是:

优点:生成的代码可能更快,并且与旧引擎兼容。它也趋于更清洁,更“ES5式”。

缺点:当你从ES6转换到ES5时,你可能会遇到问题。这很少是值得冒险的。

打开松散模式

es2015-loose预设是标准ES6预设的松散版本。它提供了一个概观如何打开某个插件的松散模式:

module.exports = {

  plugins: [

    ···

    [require("babel-plugin-transform-es2015-classes"), {loose: true}],

    require("babel-plugin-transform-es2015-object-super"),

    ···

  ]

};

示例:松散模式和正常模式输出区别

让我们看看模式的区别如何影响到以下代码的输出:

class Point {

    constructor(x, y) {

        this.x = x;

        this.y = y;

    }

    toString() {

        return `(${this.x}, ${this.y})`;

    }

}

正常模式

正常模式下,类的属性通过Object.defineProperty:

"use strict";

var _createClass = (function () {

    function defineProperties(target, props) {

        for (var i = 0; i < props.length; i++) {

            var descriptor = props[i];

            descriptor.enumerable = descriptor.enumerable || false;

            descriptor.configurable = true;

            if ("value" in descriptor) descriptor.writable = true;

            Object.defineProperty(target, descriptor.key, descriptor); // (A)

        }

    }

    return function (Constructor, protoProps, staticProps) {

        if (protoProps) defineProperties(Constructor.prototype, protoProps);

        if (staticProps) defineProperties(Constructor, staticProps);

        return Constructor;

    };

})();

function _classCallCheck(instance, Constructor) {

    if (!(instance instanceof Constructor)) {

        throw new TypeError("Cannot call a class as a function");

    }

}

var Point = (function () {

    function Point(x, y) {

        _classCallCheck(this, Point);

        this.x = x;

        this.y = y;

    }

    _createClass(Point, [{

        key: "toString",

        value: function toString() {

            return "(" + this.x + ", " + this.y + ")";

        }

    }]);

    return Point;

})();

松散模式

松散模式下,通过正常添加方法方式,更像es5写法:

"use strict";

function _classCallCheck(instance, Constructor) { ··· }

var Point = (function () {

    function Point(x, y) {

        _classCallCheck(this, Point);

        this.x = x;

        this.y = y;

    }

    Point.prototype.toString = function toString() { // (A)

        return "(" + this.x + ", " + this.y + ")";

    };

    return Point;

})();

相关文章

  • webpack学习——loose模式

    babel的松散模式将ES6代码转换为不遵循ES6语义的ES5代码。 babel中的许多插件有两种模式: 》正常模...

  • 关于Babel 6的 loose mode

    1.Overview loose mode 我翻译为松散模式,loose mode在babel中通常是不推荐使用的...

  • 模块化简介第二章

    放大模式or 宽放大模式(Loose augmentation) 如果一个模块很大,必须分成几个部分,或者一个模块...

  • 自动编译及热加载

    模式均在开发模式下 webpack启动监控自动编译 "watch": "npx webpack --watch -...

  • npm 安装时 --save --dev 和 --save 区别

    一、模式 运行webpack命令时,一定要指定模式。 webpack --mode developmentwebp...

  • webpack常见的基本概念

    mode 开发模式 webpack 提供 mode 配置选项,配置 webpack 相应模式的内置优化。 支持以下...

  • webpack打包非mvvm项目

    webpack大家都不陌生 vue/react/angular + webpack的模式很常见了 而一般上面的模式...

  • 2017-07-03

    webpack更好配置 webpack进阶-知乎 webpack 学习 nodejs 学习

  • [webpack 学习系列 ] 4. koa2 + webpac

    前言 webpack 提供了三种搭建开发环境的方式: webpack 监听模式 webpack-dev-serve...

  • loser or loose

    Girls 是这么多美剧中唯一看哭的剧,我每分钟都在害怕,又在期待,这是我唯一一部猜不到结局的剧。汉娜的忧虑症和我...

网友评论

      本文标题:webpack学习——loose模式

      本文链接:https://www.haomeiwen.com/subject/sxcpectx.html