使用babelify的透明异步函数不超过npm

时间:2015-08-25 15:47:03

标签: javascript npm async-await babeljs

我正在尝试使用Babelify来转换异步并等待ES7功能,但我不想使用gulp,grunt或类似的构建工具。到目前为止,我只使用npm取得了巨大的成功,而这一步额外的步骤似乎不值得采用更复杂的构建工具链。

这是我的最小package.json文件,只要我不使用async / await就可以运行:

{
  "main": "main.js",
  "scripts": {
    "compile": "browserify . --outfile bundle.js"
  },
  "browserify": {
    "transform": [
      [
        "babelify",
        {
          "optional": [
            "es7.asyncFunctions"
          ]
        }
      ]
    ]
  },
  "devDependencies": {
    "babelify": "^6.2.0",
    "browserify": "^11.0.1"
  }
}

这是我尝试转换的少量代码(main.js):

import "babelify/polyfill";

let asPromised = await fetch();
console.log(asPromised);

function fetch() {
  return Promise.resolve("fetched");
}

当我执行npm run compile

时,这是一个不幸的结果
> @ compile ~/project
> browserify . --outfile bundle.js

SyntaxError: ~/project/main.js: Unexpected token (3:23)
  1 | import "babelify/polyfill";
  2 | 
> 3 | let asPromised = await fetch();
    |                        ^
  4 | console.log(asPromised);
  5 | 
  6 | function fetch() {
    at Parser.pp.raise (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:24:13)
    at Parser.pp.unexpected (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:82:8)
    at Parser.pp.semicolon (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:69:81)
    at Parser.pp.parseVarStatement (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:371:8)
    at Parser.pp.parseStatement (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:99:19)
    at Parser.parseStatement (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/plugins/flow.js:621:22)
    at Parser.pp.parseTopLevel (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:30:21)
    at Parser.parse (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/index.js:70:17)
    at Object.parse (~/project/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/index.js:45:50)
    at Object.exports.default (~/project/node_modules/babelify/node_modules/babel-core/lib/helpers/parse.js:36:18)

npm ERR! @ compile: `browserify . --outfile bundle.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ compile script.
npm ERR! This is most likely a problem with the  package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     browserify . --outfile bundle.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls 
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 4.0.2-stable
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "run" "compile"
npm ERR! cwd ~/project
npm ERR! node -v v0.10.30
npm ERR! npm -v 1.4.21
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     ~/project/npm-debug.log
npm ERR! not ok code 0

(gistified:https://gist.github.com/au-phiware/34376f64f9ea6777eefd

我还从命令行尝试了以下操作,它们都给出了同样的错误:

browserify . --outfile bundle.js -t [ babelify --optional es7.asyncFunctions ]
babel --out-file bundle.js --optional es7.asyncFunctions main.js
babel --out-file bundle.js --stage 2 main.js

我觉得我在这里错过了一些东西......或者这可能是一个Babel bug? TIA。

0 个答案:

没有答案