Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

javascript - webpack babel es7 async function error "TypeError: (0 , _typeof3.default) is not a function"

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.

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
      }
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...