webpack babel es7 async function error" TypeError:(0,_typeof3.default)不是函数“ [英] webpack babel es7 async function error "TypeError: (0 , _typeof3.default) is not a function"

查看:253
本文介绍了webpack babel es7 async function error" TypeError:(0,_typeof3.default)不是函数“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在api.js中要求,但收到错误TypeError:(0,_typeof3.default)不是函数。尝试调试此问题表明删除异步字会使错误消失,但我正在尝试使用异步。我安装了所有babel异步插件,在package.json中定义,并包含在webpack.config.js和.babelrc的插件中。请帮助。

I am trying to require in api.js, but receiving the error "TypeError: (0 , _typeof3.default) is not a function". Attempting to debug this issue shows that removing "async" word makes the error disappear but I'm trying to use async. I have all the babel async plugins installed, defined in package.json and included in the plugins of webpack.config.js and .babelrc. Please help.

我的api.js

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

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

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)
    }
  }
}

推荐答案

这实际上是通过删除插件,transform-runtime来解决的。我仍然不确定为什么会这样。我很想得到一些关于原因的评论。

This was actually fixed by removing the plugin, transform-runtime. I'm still not sure why this is the case. I would love to get some comments on why.

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)
    }
  }
}

这篇关于webpack babel es7 async function error" TypeError:(0,_typeof3.default)不是函数“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆