2016-05-31 140 views
0

我一直在嘗試將大量Babel功能引入到我主要是手工編寫的React/GraphQL項目中。但是我試圖用這個構建腳本通過schema.graphql文件生成:NodeJS/Babel SyntaxError意外的令牌

import fs from 'fs'; 
    import path from 'path'; 
    import { graphql } from 'graphql'; 
    import { introspectionQuery, printSchema } from 'graphql/utilities'; 

    import schema from '../server/schema'; 

    let file_schema_json = path.join(__dirname, '../graphql/schema.json'); 
    let file_schema_graphql = path.join(__dirname, '../graphql/schema.graphql'); 

    // Save JSON of full schema introspection for Babel Relay Plugin to use 
    async function createJson() 
    { 
    var result = await(graphql(schema, introspectionQuery)); 
    if(result.errors) 
    { 
     console.error('ERROR! '); 
     console.error('ERROR introspecting schema: ', JSON.stringify(result.errors, null, 2)); 
    } 
    else 
    { 
     fs.writeFileSync(file_schema_json, JSON.stringify(result, null, 2)); 
     console.log('Written: ' + file_schema_json); 
    } 
    } 

    // Save user readable type system shorthand of schema 
    fs.writeFileSync(file_schema_graphql, printSchema(schema)); 
    console.log('Written: ' + file_schema_graphql); 

    createJson(); 

,然後我結束了:

/Users/lorm/projects/aggregated_centralized_api/node_modules/babel-core/lib/transformation/file/index.js:591 
     throw err; 
     ^

    SyntaxError: /Users/lorm/projects/aggregated_centralized_api/scripts/build-schema.js: Unexpected token (12:6) 
    10 | 
    11 | // Save JSON of full schema introspection for Babel Relay Plugin to use 
    > 12 | async function createJson() 
     |  ^
    13 | { 
    14 | var result = await(graphql(schema, introspectionQuery)); 
    15 | if(result.errors) 

但我複製了所有我能想到的依賴關係:

"dependencies": { 
"babel-cli": "^6.8.0", 
"babel-core": "^6.8.0", 
"babel-eslint": "^6.0.4", 
"babel-loader": "^6.2.4", 
"babel-plugin-react-transform": "^2.0.2", 
"babel-plugin-transform-async-to-generator": "^6.8.0", 
"babel-polyfill": "^6.8.0", 
"babel-preset-es2015": "^6.6.0", 
"babel-preset-react": "^6.5.0", 
"babel-preset-stage-0": "^6.5.0", 
"babel-relay-plugin": "^0.8.1", 
"babel-relay-plugin-loader": "^0.8.1", 
"babel-runtime": "^6.6.1", 
"body-parser": "1.15.1", 
"dataloader": "1.1.0", 
"dotenv": "2.0.0", 
"express": "4.13.4", 
"express-graphql": "^0.5.3", 
"graphql": "0.4.18", 
"graphql-relay": "0.3.6", 
"mongodb": "2.0.39", 
"q": "1.4.1" 
}, 
"devDependencies": { 
"babel": "^5.8.3", 
"babel-core": "^5.8.3", 
"babel-eslint": "^4.0.5", 
"babelify": "^6.1.1", 
"babel-cli": "^6.8.0", 
"babel-jest": "^12.0.2", 
"babel-plugin-transform-runtime": "^6.8.0", 
"babel-polyfill": "^6.8.0", 
"eslint": "^0.24.0", 
"eslint-plugin-react": "^3.2.2", 
"flow-bin": "^0.22.1" 
}, 

但仍然,它好像巴別沒有被觸發?或者不知道如何閱讀這種Javascript?什麼會導致這個錯誤?

回答

2

我認爲你沒有異步語法所需的插件支持。我有一個快速瀏覽一下目前有以下預置和插件安裝http://babeljs.io/docs/plugins/

您:

  • 巴貝爾預設-ES2015
  • 巴貝爾預設反應的
  • 巴貝爾預設-0級

如果您轉到上面的鏈接並單擊它們,您會看到哪些插件捆綁在這些預設中。看起來這些異步語法不包括在內。

有一個有用的帖子在這裏得到它的工作:http://madole.xyz/async-await-es7/

有了一個更新的帖子在這裏:http://madole.xyz/babel-plugin-transform-async-to-module-method-gotcha/


我猜你的另一種選擇是去掉異步代碼,並將其用別的東西。 graphql api是否會返回承諾?如果是這樣,你可以重構它來使用promises語法。或發電機。

哦,這裏是一個偉大的電子書涵蓋的承諾和發電機: https://github.com/getify/You-Dont-Know-JS/tree/master/async%20%26%20performance

而且在異步/等待下一個博客:https://ponyfoo.com/articles/understanding-javascript-async-await

+0

「異步」在這裏工作:https://github.com/ codefoundries/UniversalRelayBoilerplate/blob/master/scripts/build-schema.js,但不在我的項目中。而且我從那裏複製了.babelrc,並且我已經複製了package.json的大部分內容。我不確定我錯過了什麼。 – lorm

+0

您是否嘗試創建基於React Native的項目? – ctrlplusb

+0

不,我只是想創建一個GraphQL API。 – lorm