nodejs v6+ 不兼容 ES6 import/export 优雅解决方法

nodejs 发布已有多时,版本 log 重点说明今后 nodejs 开始原生支持 ES6,这让许多开发者都 happy 了一下。javascript

笔者马上将原来的一个小 server 框架改为 ES6 语法,可是: java

clipboard.png

WTF!~~~说好的所有原生支持呢?import / export各类报错,加了--es_staging,也不行,人与人以前的信任呢?node

回头再查查版本 log ,Modules 那一章仍是 commonjs 的引用方法,网上其它人也说这个问题,那就是妥妥的不支持了。es6

测试文件以下:json

'use strict';
    class Main {
        constructor() {
            this.parent = 'this is parent msg';
        }

        say() {
            setTimeout(() => {
                console.log(this.parent);
            }, 500);
        }
    }

    export default Main;

简单暴力(没)有成效的解决方法

“不支持 ES6 就用 babel 转换呗~” 大多数人都是这样想的。将彻底体的 ES6 经过 babel 降级转换~~~而后就 OK 了。node 运行各类顺利。babel

标签 es2015 转换后:app

'use strict';

    Object.defineProperty(exports, "__esModule", {
        value: true
    });

    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); } } 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 Main = function () {
        function Main() {
            _classCallCheck(this, Main);

            this.parent = 'this is parent msg';
        }

        _createClass(Main, [{
            key: 'say',
            value: function say() {
                var _this = this;

                setTimeout(function () {
                    console.log(_this.parent);
                }, 500);
            }
        }]);

        return Main;
    }();

    exports.default = Main;
    //# sourceMappingURL=main.js.map

......框架

我差点也就中了。-_-!性能

看着 node V6 对其它各类 es6 特性都支持的很好,真不想经转换后一下回到解放前。测试

注:
其实上面一句并非真正缘由啦,node 的原生支持并非简单暴力的内置了一个 babel,是在整个内核运行机制上进行改进,尽量符合 es6 的语言思想。
在执行流程、内存管理上都有优化。网上有很多原生 es6 运行性能的对比,有兴趣能够去看一下。

经笔者测试,以最新的 6.4 为例,好像只有 import / export 不能支持,所以,笔者试着找一下只对此进行降级转换的方法。

针对性的 babel 降级转换

经过查看 babel 的插件列表 http://babeljs.io/docs/plugins/;

clipboard.png

其中在 Modules 有一个 es2015_modules_commonjs,应该就是将 ES6 Modules 转换成 nodejs 通用的 commonjs 形式的。

马上试用,.babelrc 文件以下:

{
        "plugins": [
            "transform-es2015-modules-commonjs"
        ]
    }

如此通过转换后的文件:

'use strict';

    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    class Main {
        constructor() {
            this.parent = 'this is parent msg';
        }

        say() {
            setTimeout(() => {
                console.log(this.parent);
            }, 500);
        }
    }

    exports.default = Main;
    //# sourceMappingURL=main.js.map

执行经过 perfect!!! 要得就是这个效果。并且由于转换程度不深,速度也差很少减小了 2/3。

总结

如上只是一个经历,相信随着 nodejs 的不断发展,完美支持 es6 也是早晚的事。但新语法、新事物确定也会随之出现,咱们须要的是一个“增量式”的转换,但愿对之后处理此类问题有所启发。

相关文章
相关标签/搜索