Skip to content Skip to sidebar Skip to footer

Syntaxerror: Unexpected Token: Punc ())

I'm recieving: SyntaxError: Unexpected token: punc ()) from UglifyJS and it points to the first letter of global variable API_URL. I have it implemented in this way: export default

Solution 1:

Uglify doesn't fully support ES6, template literals included. You can track the conversation on Github. There's a harmony branch for ES6 support. You can use the branch by replacing your package.json entry for uglify to:

"uglify-js":"git+https://github.com/mishoo/UglifyJS2.git#harmony"

Alternatively, you might want to pass the code through a transpiler first before minification. That way, all the syntax will be ES5 which Uglify understands very well. You might want to tweak your transpiler config if you want some of the ES6 syntax to remain intact.

Solution 2:

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 importsimport config from"config"exportdefaultreduxApi({
  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.

Post a Comment for "Syntaxerror: Unexpected Token: Punc ())"