SyntaxError: Unexpected token: punc ())

后端 未结 2 592
野的像风
野的像风 2021-01-20 14:48

I\'m recieving:

SyntaxError: Unexpected token: punc ()) from UglifyJS

and it points to the first letter of global variable API_URL

2条回答
  •  -上瘾入骨i
    2021-01-20 15:35

    I decided to write here a solution. I didn't have to install other uglify-js package versions. The point was to solve imports to objects in proper way. In my case the API_URL was a global variable. So Uglify wasn't sure if it's defined, that's why it threw an error.

    To solve that problem I used webpack externals in this way:

    // ------------------------------------                                                                                               
    // Externals
    // ------------------------------------
    webpackConfig.externals = {
      config: JSON.stringify(require(`./${__DEV__ ? 'development' : 'production'}.json`)),                                                
    }
    

    It just puts JSON configuration object into the config variable, depending on environment (development or production). All you need to do is to put development.json and production.json next to file where you define webpackConfig.externals.

    Then as in my case, you define it let's say in development.json:

    {
      "apiUrl": "http://localhost:5000"
    }
    

    then finally in your code:

    ... // other imports
    import config from "config"
    
    export default reduxApi({
      campaigns: {
        url: `${config.apiUrl}/api/v1/whatever`,
        transformer (response) {
          if (!response) return {}
          return response.data
        }
      } 
    }).use('fetch', adapterFetch(fetch)).use('options', {
      headers: getRequestHeaders()
    })
    

    and it works like a charm.

    Hope that helps somebody.

提交回复
热议问题