SyntaxError:意外的标记:punc()) [英] SyntaxError: Unexpected token: punc ())

查看:216
本文介绍了SyntaxError:意外的标记:punc())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在收到:

来自UglifyJS的语法错误:意外的令牌:punc())

并指向全局变量 API_URL 的第一个字母。
我以这种方式实现它:

and it points to the first letter of global variable API_URL. I have it implemented in this way:

export default reduxApi({
  campaigns: {
    url: `${API_URL}/api/v1/whatever`,
    transformer (response) {
      if (!response) return {}
      return response.data
    }
  } 
}).use('fetch', adapterFetch(fetch)).use('options', {
  headers: getRequestHeaders()
})

如果我删除键 url 下的全局变量:

If I remove global variable under key url:

export default reduxApi({
  campaigns: {
    url: `/api/v1/whatever`,
    transformer (response) {
      if (!response) return {}
      return response.data
    }
  } 
}).use('fetch', adapterFetch(fetch)).use('options', {
  headers: getRequestHeaders()
})

然后一切正常。有任何想法吗?为什么uglify会抛出那种错误?

then everything works fine. Any ideas? Why uglify throws that kind of error?

推荐答案

我决定在这里写一个解决方案。我没有必要安装其他 uglify-js 软件包版本。重点是以适当的方式解决对象的导入问题。就我而言, API_URL 是一个全局变量。因此,Uglify不确定它是否已定义,这就是它抛出错误的原因。

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.

为了解决这个问题,我使用了 webpack externals

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

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

它只是将JSON配置对象放入 config 变量中,具体取决于环境(开发生产)。您需要做的就是在您定义<$ c的文件旁边放置 development.json production.json $ C> webpackConfig.externals 。

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.

然后在我的情况下,你定义它,让我们说在 development.json

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

它就像一个魅力。

希望能帮助某人。

这篇关于SyntaxError:意外的标记:punc())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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