如何在带有装饰器的本机0.56(Babel 7)中使用Mobx

时间:2018-07-06 14:29:52

标签: reactjs react-native babel mobx

我已将使用Babel 7的RN应用程序从0.55.4升级到0.56。

在0.55.4中为MOBX使用装饰器时,我使用“ babel-plugin-transform-decorators-legacy”,但与Babel 7不兼容...

本机版本:0.56.0 mobx版本:5.0.3 mobx反应版本:5.2.3

有人解决吗?

谢谢

更新:

该应用程序在DEBUG中可以使用此配置

package.json

...
"devDependencies": {
    "@babel/core": "7.0.0-beta.47",
    "@babel/plugin-proposal-decorators": "7.0.0-beta.47"
    ...
}

.babelrc

{
  "presets": [
    ["react-native"]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

但是在RELEASE xCode中,由于以下错误而崩溃:

babelHelpers.applyDecoratedDescriptor is not a function.

更新2,工作配置:

这是我的工作配置:

package.json

...
"devDependencies": {
   "@babel/core": "7.0.0-beta.47",
   "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
   "@babel/runtime": "7.0.0-beta.47",
   "babel-jest": "23.2.0",
   "babel-preset-react-native": "5.0.2",
   "jest": "23.3.0",
   "react-test-renderer": "16.4.1"
}
...

.babelrc

{
  "presets": [
    ["react-native"]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

然后在index.js(主应用程序起始文件)中,我需要导入装饰器babel库:

index.js

import applyDecoratedDescriptor from '@babel/runtime/helpers/es6/applyDecoratedDescriptor';
import initializerDefineProperty from '@babel/runtime/helpers/es6/initializerDefineProperty';

Object.assign(babelHelpers, {applyDecoratedDescriptor, initializerDefineProperty});

require('./app.js');

app.js

import {AppRegistry} from 'react-native';
import AppName from './app/index';

AppRegistry.registerComponent(appName, () => AppName);

2 个答案:

答案 0 :(得分:11)

好吧,我通过添加@babel/runtime解决了所有错误,现在该应用程序也可以在DEBUGRELEASE中工作。

这里是正确的配置:

package.json

...
"devDependencies": {
  "@babel/core": "7.0.0-beta.47",
  "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
  "@babel/plugin-transform-runtime": "7.0.0-beta.47",
  "@babel/runtime": "7.0.0-beta.47",
  "babel-jest": "23.2.0",
  "babel-preset-react-native": "5.0.2",
  "jest": "23.3.0",
  "react-test-renderer": "16.4.1"
}
...

.babelrc

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "polyfill": false,
      "regenerator": false
    }]
  ]
}

感谢@Hkan。

答案 1 :(得分:1)

我通过安装@babel/plugin-proposal-decorators@7.0.0-beta.47@babel/plugin-transform-runtime@7.0.0-beta.47解决了这个问题。

版本可能会因您而异。我使用这些版本是因为@babel/core也位于7.0.0-beta.47

当前.babelrc是:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "polyfill": false,
      "regenerator": false
    }]
  ]
}