简体   繁体   English

使用 Babel 和 JSPM 启用 ES2015 默认参数

[英]enable ES2015 default parameters with Babel and JSPM

I'm setting up JSPM / Babel for the first time, and trying out a few ES6 features.我是第一次设置 JSPM / Babel,并尝试了一些 ES6 特性。

It seems only certain features work with the default transform, so I've tried to enable the ES2015 preset, using both package.json and the .babelrc file, as below:似乎只有某些功能适用于默认转换,所以我尝试启用 ES2015 预设,同时使用 package.json 和 .babelrc 文件,如下所示:

"presets": ["es2015"]

This doesn't seem to have any effect.这似乎没有任何影响。

I'm trying to add a default parameter to a function, so want to replace the following function我正在尝试向函数添加默认参数,因此想替换以下函数

exports.width = function(x) {
    if (!arguments.length) return width;
    width = x;
    return this;
};

with

exports.width = function(x = width) {
    width = x;
    return this;
};

I keep getting a Uncaught SyntaxError: Unexpected token = error however.我不断收到Uncaught SyntaxError: Unexpected token = error。

I've installed the preset npm install babel-preset-es2015 --save-dev , as well as trying to install the individual plugin, but no dice.我已经安装了预设npm install babel-preset-es2015 --save-dev ,以及尝试安装单个插件,但没有骰子。

Ah, so it seems JSPM will only transpile ES6 modules , so you need to pass a flag to any non ES6 modules to force it to transpile, so "format es6";啊,所以看起来JSPM 只会转译 ES6 模块,所以你需要向任何非 ES6 模块传递一个标志来强制它转译,所以"format es6"; at the top of the file.在文件的顶部。

Which is odd as it was transpiling the lambda => functions but not the default params.这很奇怪,因为它转译了 lambda =>函数而不是默认参数。

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

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