Webpack Babel ES7 异步函数错误 "TypeError: (0 , _typeof3.default) is not a function"

4
我尝试在api.js中引用,但出现“TypeError:(0,_typeof3.default)不是函数”的错误。尝试调试此问题表明,删除“async”单词会使错误消失,但我要使用async。我已经安装了所有的babel async插件,在package.json中定义,并包含在webpack.config.js和.babelrc的插件中。请帮忙。

my api.js

let config = require('config'),
  $ = require('jquery')

module.exports = {
  loggedIn: false,
  api: async (endpoint, params={}, method="GET") => {
    console.log(endpoint, params, method)
    let request,
      url = config.urls.api + endpoint

    switch(method){
      case 'GET': request = $.get(url); break;
      case 'POST': request = $.post(url); break;
    }

    try{
      let res = await request
      return res
    }catch(e){
      if(e == 'not logged-in'){
        this.loggedIn = false
      }
    }
  }
}

package.json

{
  "name": "background",
  "version": "1.0.0",
  "description": "",
  "main": "background.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "NODE_ENV=dev webpack --progress --colos --watch"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "webpack": "^1.12.9",
    "babel-plugin-syntax-async-functions": "^6.0.14",
    "babel-plugin-syntax-decorators": "^6.1.18"
  },
  "dependencies": {
    "babel-plugin-transform-runtime": "^6.1.18",
    "babel-polyfill": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-stage-3": "^6.1.18",
    "babel-runtime": "^6.2.0",
    "jquery": "^2.1.4"
  }
}

.babelrc

{
  "plugins": ["syntax-decorators","syntax-async-functions"]
}

webpack.config.js

const path = require('path')

console.log(process.env.NODE_ENV)

module.exports = {
  entry: ['babel-polyfill', './background.js'],
  output: {
    filename: 'background-bundle.js',
    publicPath: 'http://localhost:8090/assets',
    path: '../dist'
  },
  module: {
    loaders: [
      {
        loader: 'babel-loader',
        test: /.jsx?$/,
        exclude: path.resolve(__dirname, "node_modules"),
        query: {
          plugins: ['transform-runtime','syntax-async-functions', 'syntax-decorators'],
          presets: ['es2015','stage-3']
        }
      }
    ]
  },
  externals: {
    //don't bundle the 'react' npm package with our bundle.js
    //but get it from a global 'React' variable
//    'react': 'React'
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
      config: path.join(__dirname, 'config', process.env.NODE_ENV)
    }
  }
}


尝试在你的 api.js 文件顶部加入 require('babel-polyfill') - laggingreflex
1个回答

5

通过移除插件transform-runtime,问题已被解决。我仍然不确定原因所在,希望能得到一些评论。

const path = require('path')

console.log(process.env.NODE_ENV)

module.exports = {
  entry: ['babel-polyfill', './background.js'],
  output: {
    filename: 'background-bundle.js',
    publicPath: 'http://localhost:8090/assets',
    path: '../dist'
  },
  module: {
    loaders: [
      {
        loader: 'babel-loader',
        test: /.jsx?$/,
        exclude: path.resolve(__dirname, "node_modules"),
        query: {
          plugins: ['transform-runtime','syntax-async-functions', 'syntax-decorators'],
          presets: ['es2015','stage-3']
        }
      }
    ]
  },
  externals: {
    //don't bundle the 'react' npm package with our bundle.js
    //but get it from a global 'React' variable
//    'react': 'React'
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
      config: path.join(__dirname, 'config', process.env.NODE_ENV)
    }
  }
}


这句话的意思是要做相反的事情 - https://dev59.com/tFoV5IYBdhLWcg3wY9_6#39502362 问题似乎相似。 - Abhinav Singi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接