简体   繁体   English

Grunt ,带有外部助手的 Es6 Babel 设置

[英]Grunt , Babel setup for Es6 with external helper

Hi there I have been forced to come here due to every resource out there on the topic is very poor and incomplete.嗨,我被迫来到这里,因为有关该主题的所有资源都非常贫乏且不完整。

Not only on the babel site but every single post out there is not complete and informative enough.不仅在 babel 站点上,而且那里的每一个帖子都不够完整和信息量不够。

I tried to reach out at the babel forum and no replies.我试图在 babel 论坛上联系,但没有回复。

I am trying to convert my prototype libraries to Es6 and convert to the most leanest possible code.我正在尝试将我的原型库转换为 Es6 并转换为最精简的代码。 So no bloaty duplicated generated code and if possible no bloaty requirejs and whatever browserify generates.所以没有臃肿的重复生成的代码,如果可能的话,没有臃肿的 requirejs 和浏览器生成的任何内容。

I have tried to make a project with grunt and babel directly, configure the external-helpers plugin according to the babel documentation.我试过直接用grunt和babel做一个项目,根据babel文档配置external-helpers插件。

It fails to include the relevant helper code and fails to include the import module code altogether.它没有包含相关的帮助程序代码,也没有完全包含导入模块代码。

ie a babel config like即 babel 配置,如

{
    options: {
        sourceMap: false,
        presets: ['es2015'],
        "plugins": ["external-helpers"]

    },
    dist: {
        files: {
            'build/<%= package.name %>.js': ['src/<%= package.name %>.js']
        }
    }
}

The main project file has an import like主项目文件有一个像

import Button from './ui/buttons/Button';

The module code looks like this as if the export is placed underneath extra code is generated for that.模块代码看起来像这样,就好像导出被放置在为此生成的额外代码之下。

export default class ShareButton {}

produces an output like this产生这样的输出

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

require('babel-core/external-helpers');

var _Button = require('./ui/buttons/Button');

var _Button2 = babelHelpers.interopRequireDefault(_Button);

No source of the module or the helper object is included.不包含模块或帮助对象的来源。

I searched hard for how to deal with external-helpers and it suggests it has to be imported into a separate file ie something like this to generate only the helper functions needed我努力寻找如何处理外部助手,它表明必须将其导入到一个单独的文件中,即这样的东西以仅生成所需的助手功能

babel-external-helpers -l createClass > src/helpers.js

But any resource regards to this fails to go as far as to how to import that into the project.但是,与此相关的任何资源都无法将其导入到项目中。

If I use the transform-runtime plugin, it produces a massive polyfill that cannot be disabled so a bug and not so useful for what I need.如果我使用 transform-runtime 插件,它会产生一个无法禁用的大量 polyfill,所以这是一个错误,对我需要的东西没有多大用处。

"plugins": [
    ["transform-runtime", { "polyfill": false, "regenerator": false }]
]

If I use browserify / babelify it makes a royal mess and duplicates code still.如果我使用 browserify / babelify 它会使皇家混乱并且仍然重复代码。

A config like像这样的配置

{
    options: {
        "transform": [["babelify", {

            "presets": ["es2015"],

            "plugins": ["external-helpers"],

            sourceMap: false
        }]]
    },
    dist: {
        files: {

            'build/<%= package.name %>.js': ['src/<%= package.name %>.js']

        }
    }
}

Produces code like this still with the external helper missing and duplicated code not relevant to the helper.生成这样的代码仍然缺少外部助手,并且重复的代码与助手无关。 ie IE

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

Is within every module in the generated file.在生成的文件中的每个模块中。

If I export the classes like this at the bottom of every file如果我在每个文件的底部导出这样的类

export default class {}

Duplicated code is generated like重复的代码是这样生成的

var _class = function _class() {
    babelHelpers.classCallCheck(this, _class);
};

exports.default = _class;

In terms of filesize that doesn't include bloaty wrapping code like就文件大小而言,不包括臃肿的包装代码,如

},{}],2:[function(require,module,exports){

It seems concatting all the prototype classes files together to bundle in one file is the winner still.似乎将所有原型类文件连接在一起捆绑在一个文件中仍然是赢家。

So trying to port the library but keep it similar and bundle it together into one file.因此,尝试移植库但保持其相似性并将其捆绑到一个文件中。

Hopefully this is concise enough and there is a simple solution.希望这足够简洁,并且有一个简单的解决方案。

FYI browsers do not understand tabs and 4 spaces.仅供参考的浏览器不理解制表符和 4 个空格。 I had to edit this post in my editor to get the code blocks working !我必须在我的编辑器中编辑这篇文章才能使代码块工作! It would be nice to have a markup like other places like "```" ?像“```”这样的其他地方有一个标记会很好吗?

Let me know thanks.让我知道谢谢。

I'm using rollup with babel now.我现在正在使用带有 babel 的汇总。 Rollup produces a clean output as umd mode. Rollup 以 umd 模式产生干净的输出。 Browserify is really bloaty in itself. Browserify 本身就很臃肿。

There is just a problem with polyfills being converted.转换 polyfill 只是一个问题。 I have to concat external ones in like for WeakMap.我必须像 WeakMap 一样连接外部的。

I had a problem trying to use the generated Iterator and finding a polyfill for that so I have to code loops a particular way it doesn't generate Iterators.我在尝试使用生成的迭代器并为此查找 polyfill 时遇到问题,因此我必须以不生成迭代器的特定方式对循环进行编码。

The polyfill generation in babel is still too bloaty and crazy. babel 中的 polyfill 生成仍然过于臃肿和疯狂。 It's pretty terrible.这太可怕了。 So I concat in minified polyfills that are very small and it's used globally.所以我连接了非常小的缩小的 polyfills,它在全球范围内使用。

I was running into something very similar.我遇到了非常相似的事情。 Was tired of trying to do it the "right way" and ended up just creating https://www.npmjs.com/package/grunt-babel-helpers which simply manipulates the string output.厌倦了尝试以“正确的方式”进行操作,最终只创建了https://www.npmjs.com/package/grunt-babel-helpers来简单地操作字符串输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM