grunt-babel 破坏文件格式(新行和空格)

grunt-babel breaks file formatting (new lines and spaces)

我刚刚开始在我的应用程序中使用 babel with grunt-babel。但是我遇到了一些我想避免的行为:

babel之前:

(function() {
    'use strict';

    angular
        .module('app')
        .controller('Ctrl', Ctrl);

    Ctrl.$inject = ['$stateParams'];

    function Ctrl($stateParams) {

    }
})();

babel之后:

(function () {
    'use strict';

    angular.module('app.standingOrder').controller('Ctrl', Ctrl);

    Ctrl.$inject = ['$stateParams'];

    function Ctrl($stateParams) {}
})();

我的 grunt 任务 看起来像这样:

babel: {
    options: {
        sourceMap: false,
        blacklist: ['strict']
    },
    dist: {
        files: [
            {
                src: [ 'src/**/*.js' ],
                cwd: '<%= build_dir %>',
                dest: '<%= build_dir %>',
                expand: true
            }
        ]
    }
},

请注意,babel 删除了空行,added/removed 空格破坏了之前的格式。

有什么方法可以避免这种情况并保留我的格式吗?

retainLines 选项将尝试保留您的行号。 https://babeljs.io/docs/usage/options/

我认为源地图可能是最好的选择,尽管它们需要更多的工作来管理。

你可以使用repl看看babel会做什么https://babeljs.io/repl/