0

我的反应本机代码直到昨天都运行良好,但今天当我试图编译代码时,我遇到了这个奇怪的错误。

node /Users/aragorn/relay-react-native-app/node_modules/react-native/local-cli/cli.js bundle --entry-file index.ios.js --platform ios --dev true --reset-cache --bundle-output check.js
[04/23/2017, 18:48:40] <START> Initializing Packager
[04/23/2017, 18:48:49] <START> Building Haste Map
[04/23/2017, 18:48:50] <END>   Building Haste Map (751ms)
[04/23/2017, 18:48:50] <END>   Initializing Packager (9888ms)
[04/23/2017, 18:48:50] <START> Transforming files
Warning: The transform cache was reset.

TransformError: /Users/aragorn/relay-react-native-app/index.ios.js: Unexpected token import.

以下是我添加的 .babelrc 文件,因为我需要添加,因为我正在使用带有 react-native 的中继。

{
  "passPerPreset": true,
  "presets": [
    {
      "plugins": [
        "./plugins/babelRelayPlugin"
      ]
    },
    "react-native"
  ]
}

我为此苦苦挣扎了一段时间。有人可以帮忙吗?

另外,我在 package.json 中添加了所有 babel 依赖项:-

 "devDependencies": {
    "babel-jest": "18.0.0",
    "babel-preset-react-native": "1.9.1",
    "babel-cli": "6.18.0",
    "babel-core": "6.21.0",
    "babel-relay-plugin": "0.10.0",
    "jest": "18.0.0",
    "react-test-renderer": "15.4.1",
    "babel-eslint": "7.1.1",
    "eslint": "3.13.1",
    "eslint-config-eslint": "3.0.0",
    "eslint-friendly-formatter": "2.0.7",
    "eslint-loader": "1.6.1",
    "eslint-plugin-import": "2.2.0",
    "eslint-plugin-jsx-a11y": "3.0.2",
    "eslint-plugin-react": "6.9.0"
  },
4

2 回答 2

3

.babelrc错了。您必须为 添加顶级键,plugins但不能将其嵌套到presets. 因此正确的是:

{
  "passPerPreset": true,
  "plugins": [
    "./plugins/babelRelayPlugin"
  ],
  "presets": [
    "react-native"
  ]
}
于 2017-04-23T16:02:51.513 回答
0

所以,在深入研究之后,我终于弄清楚出了什么问题。

我添加了 react-native-config 包来处理不同的环境(开发和生产)。对于我的中继插件,我调用 graphql 进行模式验证。

现在,我从 react-native-config 包中获取了这个 graphql API,因为它会因开发和生产而改变。

React-native-config 完全基于 es2015,因此导入失败,我得到错误 import is unexpected config 因为 react native 预设是在中继插件之后指定的。

.babelrc(工作正常)

{
  "passPerPreset": true,
  "presets": [
    {
      "plugins": [
        "./plugins/babelRelayPlugin"
      ]
    },
    "react-native"
  ]
}

babelRelayPlugin.js(有问题的代码)

const babelRelayPlugin = require("babel-relay-plugin");
const introspectionQuery = require("graphql/utilities").introspectionQuery;
const request = require("sync-request");
//const {SCHEMA} = require("../js/network/api"); //code with issue

const SCHEMA = "http://dev.api.wealthydev.in/gapi/graphiql/";

const response = request("GET", SCHEMA, {
  "qs": {
    "query": introspectionQuery
  }
});

const schema = JSON.parse(response.body.toString("utf-8"));

module.exports =  babelRelayPlugin(schema.data);

api.js

const {serverURL,devServerURL} = require("../env");
const SCHEMA = `${devServerURL}/gapi/graph-schema/`;

module.exports ={
  // other urls
  SCHEMA,
}

环境.js

"use strict";
import Config from "react-native-config"; // main issue lies here

module.exports = {
  "serverURL": Config.API_URL,
  "devServerURL": Config.DEV_SERVER_API_LEVEL,
  "mobileWebURL": Config.MOBILE_WEB_URL,
  "version": "1.0",
  "apiLevel": "v0"
};

所以现在我直接在 babelRelayPlugin 文件本身中指定 SCHEMA URL。计划在归档之前更改 URL。它是一个黑客,但应该工作,直到我找到更好的东西。

希望它可以帮助某人:)

于 2017-04-23T18:13:53.340 回答