语法错误:意外标记:punc ()) [英] SyntaxError: Unexpected token: punc ())

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

问题描述

我收到:

SyntaxError: Unexpected token: punc ()) 来自 UglifyJS

SyntaxError: Unexpected token: punc ()) from UglifyJS

指向全局变量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 变量中,具体取决于环境(developmentproduction).您需要做的就是将 development.jsonproduction.json 放在定义 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"
}

最后在你的代码中:

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

它就像一个魅力.

希望对某人有所帮助.

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

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