如何使用babel-standalone来转译使用Spread / rest运算符的ES6 JavaScript?

时间:2019-07-04 14:46:02

标签: javascript babeljs ecmascript-next spread-syntax

我需要使我的javascript(在非Node.js环境中)向后兼容,因此我试图按照本post

的建议利用Babel来移植我的代码。

babel-standalone很好用,但似乎不适用于传播/休息运营商

例如,以下代码很好用:

var input = 'const getMessage = (D) => {return D}';
var output = Babel.transform(input, { presets: ['es2015'] }).code;

但是,以下内容会产生错误:

var input = 'const getMessage = (D) => {...D}';
var output = Babel.transform(input, { presets: ['es2015'] }).code;

下面的错误

babel.min.js:2 Uncaught SyntaxError: unknown: Unexpected token (1:26)
> 1 | const getMessage = (D) => return {D}
    |                           ^
babel.min.js:2 Uncaught SyntaxError: unknown: Unexpected token (1:27)
> 1 | const getMessage = (D) => {...D}
    |                            ^
    at t.J.raise (babel.min.js:7)
    at t.X.unexpected (babel.min.js:5)
    at te.parseExprAtom (babel.min.js:6)
    at te.parseExprSubscripts (babel.min.js:6)
    at te.parseMaybeUnary (babel.min.js:6)
    at te.parseExprOps (babel.min.js:6)
    at te.parseMaybeConditional (babel.min.js:6)
    at te.parseMaybeAssign (babel.min.js:6)
    at te.parseExpression (babel.min.js:6)
    at t.z.parseStatement (babel.min.js:5)
babel.js:14449 Uncaught SyntaxError: unknown: Unexpected token (1:27)
> 1 | const getMessage = (D) => {...D}
    |                            ^
    at Parser.pp$5.raise (babel.js:14449)
    at Parser.pp.unexpected (babel.js:11756)
    at Parser.pp$3.parseExprAtom (babel.js:13745)
    at Parser.pp$3.parseExprSubscripts (babel.js:13489)
    at Parser.pp$3.parseMaybeUnary (babel.js:13469)
    at Parser.pp$3.parseExprOps (babel.js:13399)
    at Parser.pp$3.parseMaybeConditional (babel.js:13376)
    at Parser.pp$3.parseMaybeAssign (babel.js:13339)
    at Parser.pp$3.parseExpression (babel.js:13301)
    at Parser.pp$1.parseStatement (babel.js:11901)

See my codepen here

独立的babel github页面指出“它与所有标准的Babel插件和预设捆绑在一起,”但它似乎无法掩盖我对散布运算符的使用

0 个答案:

没有答案