Babel 转译 es7 class 装饰器意外的令牌错误
Babel transpiling es7 class decorators Unexpected token error
我正在开发一个使用 es6 和 es7 代码的 Aurelia 应用程序,我正在尝试使用 babel 转换代码。我的 packages.json 文件
中有以下内容
"scripts": {
"babel": "babel --stage 1 -d AureliaWeb/ ../Test/Aurelia/ --extends babelrc",
我安装了以下软件包:
"devDependencies": {
"babel-core": "^6.10.4",
"babel-plugin-syntax-decorators": "^6.8.0",
"babel-plugin-syntax-flow": "^6.8.0",
"babel-plugin-transform-es2015-modules-amd": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.10.3",
"babel-plugin-transform-es2015-modules-systemjs": "^6.9.0",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-es2015-loose": "^7.0.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.11.0",
"babel-preset-stage-3": "^6.11.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0"
},
"dependencies": {
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015-loose": "^7.0.0",
"core-js": "^2.4.0",
"lodash": "^4.13.1"
}
我正在尝试转译 es7 代码,如下所示:
import {inject, noView} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@noView() <-- THIS IS CAUSING THE ERROR!!
@inject(HttpClient)
export class CompanyDataManager {
我收到有关 class 装饰器 @noView 的错误语法错误意外标记。我检查了阶段 1 预设,我知道 class 装饰器已被删除 http://babeljs.io/docs/plugins/preset-stage-1/
因此,我在遗留装饰器中添加了 'babel-plugin-transform-decorators-legacy found here 'https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy
所以我的 .babelrc(位于我项目的根目录)看起来像这样:
{
"presets": [ "es2015-loose", "stage-1" ],
"plugins": [
"syntax-decorators",
"syntax-flow",
"babel-plugin-transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
}
可是我还是找不到快乐!问题是
- 当我在 npm 中执行 babel 脚本时,如何确认我的 babelrc 文件被提取?我尝试添加 --extends babelrc 以试图强制执行它,但我不确定这是否正确。
我也试过像这样在 babel 命令中指定一个插件:
"babel --plugins babel-plugin-transform-decorators-legacy --stage 1 -d AureliaWeb/ ../Test/Aurelia/"
仍然没有运气,任何帮助将不胜感激。
更新 1
在弄乱了 .bablerc 文件之后,我想我可能把它放在了错误的位置。我的结构如下(它是一个 mvc 应用程序)。
- Test.UnitTest(项目)<-- 我是 运行 来自这里的 babel 脚本
- AureliaWeb <-- 结束跟踪位置
- 测试(项目)
- Aurelia <-- 包含实际的 Aurelia 代码
将 .bablerc 文件移动到测试 > Aurelia 后,我开始收到以下错误
未知插件 "transform-decorators-legacy" 在 "C:\Code\src\Test\Aurelia\.babelrc" 中指定为 0,尝试相对于 "C:\Code\src\Test\Aurelia"
进行解析
我已经尝试在两个项目中安装 transform-decorators-legacy 包,看看它是否有任何不同
更新 2
我现在可以确认这确实是问题所在,看来 babel 文件需要放在源文件夹中,而不是目标文件夹或执行 babel 的位置。
我相信你只需要删除括号:
@noView
@inject(HttpClient)
export class CompanyDataManager {
参见,例如,point 2 under "Best Effort":
class Example {
@dec
static prop = i++;
}
______
注意:当您在 .babelrc
(或 package.json
)中声明一个 babel 插件时,您可以删除前缀 babel-plugin-
:
[
"syntax-decorators",
"syntax-flow",
"transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
______
How can i confirm that my babelrc file is being picked up when im executing the babel script
Babel 在为 .babelrc
或 package.json
和 "babel"
属性 调用它的目录中查找。只要你有其中任何一个并且在同一目录中执行 Babel,Babel 就会找到它。
我已经回答了我自己的问题,详情在主要post。
babel 文件需要放在源文件夹中,而不是目标文件夹或执行 babel 的位置。
我正在开发一个使用 es6 和 es7 代码的 Aurelia 应用程序,我正在尝试使用 babel 转换代码。我的 packages.json 文件
中有以下内容"scripts": {
"babel": "babel --stage 1 -d AureliaWeb/ ../Test/Aurelia/ --extends babelrc",
我安装了以下软件包:
"devDependencies": {
"babel-core": "^6.10.4",
"babel-plugin-syntax-decorators": "^6.8.0",
"babel-plugin-syntax-flow": "^6.8.0",
"babel-plugin-transform-es2015-modules-amd": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.10.3",
"babel-plugin-transform-es2015-modules-systemjs": "^6.9.0",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-es2015-loose": "^7.0.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.11.0",
"babel-preset-stage-3": "^6.11.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0"
},
"dependencies": {
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015-loose": "^7.0.0",
"core-js": "^2.4.0",
"lodash": "^4.13.1"
}
我正在尝试转译 es7 代码,如下所示:
import {inject, noView} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@noView() <-- THIS IS CAUSING THE ERROR!!
@inject(HttpClient)
export class CompanyDataManager {
我收到有关 class 装饰器 @noView 的错误语法错误意外标记。我检查了阶段 1 预设,我知道 class 装饰器已被删除 http://babeljs.io/docs/plugins/preset-stage-1/
因此,我在遗留装饰器中添加了 'babel-plugin-transform-decorators-legacy found here 'https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy
所以我的 .babelrc(位于我项目的根目录)看起来像这样:
{
"presets": [ "es2015-loose", "stage-1" ],
"plugins": [
"syntax-decorators",
"syntax-flow",
"babel-plugin-transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
}
可是我还是找不到快乐!问题是
- 当我在 npm 中执行 babel 脚本时,如何确认我的 babelrc 文件被提取?我尝试添加 --extends babelrc 以试图强制执行它,但我不确定这是否正确。
我也试过像这样在 babel 命令中指定一个插件:
"babel --plugins babel-plugin-transform-decorators-legacy --stage 1 -d AureliaWeb/ ../Test/Aurelia/"
仍然没有运气,任何帮助将不胜感激。
更新 1
在弄乱了 .bablerc 文件之后,我想我可能把它放在了错误的位置。我的结构如下(它是一个 mvc 应用程序)。
- Test.UnitTest(项目)<-- 我是 运行 来自这里的 babel 脚本
- AureliaWeb <-- 结束跟踪位置
- 测试(项目)
- Aurelia <-- 包含实际的 Aurelia 代码
将 .bablerc 文件移动到测试 > Aurelia 后,我开始收到以下错误
未知插件 "transform-decorators-legacy" 在 "C:\Code\src\Test\Aurelia\.babelrc" 中指定为 0,尝试相对于 "C:\Code\src\Test\Aurelia"
进行解析我已经尝试在两个项目中安装 transform-decorators-legacy 包,看看它是否有任何不同
更新 2
我现在可以确认这确实是问题所在,看来 babel 文件需要放在源文件夹中,而不是目标文件夹或执行 babel 的位置。
我相信你只需要删除括号:
@noView
@inject(HttpClient)
export class CompanyDataManager {
参见,例如,point 2 under "Best Effort":
class Example {
@dec
static prop = i++;
}
______
注意:当您在 .babelrc
(或 package.json
)中声明一个 babel 插件时,您可以删除前缀 babel-plugin-
:
[
"syntax-decorators",
"syntax-flow",
"transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
______
How can i confirm that my babelrc file is being picked up when im executing the babel script
Babel 在为 .babelrc
或 package.json
和 "babel"
属性 调用它的目录中查找。只要你有其中任何一个并且在同一目录中执行 Babel,Babel 就会找到它。
我已经回答了我自己的问题,详情在主要post。
babel 文件需要放在源文件夹中,而不是目标文件夹或执行 babel 的位置。