简体   繁体   English

SyntaxError:意外的标记:punc())

[英]SyntaxError: Unexpected token: punc ())

I'm recieving: 我收到了:

SyntaxError: Unexpected token: punc ()) from UglifyJS SyntaxError:来自UglifyJS的意外标记:punc())

and it points to the first letter of global variable API_URL . 它指向全局变量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()
})

If I remove global variable under key url : 如果我删除密钥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()
})

then everything works fine. 一切正常。 Any ideas? 有任何想法吗? Why uglify throws that kind of error? 为什么uglify会抛出那种错误?

Uglify doesn't fully support ES6, template literals included . Uglify不完全支持ES6, 包括模板文字 You can track the conversation on Github . 您可以在Github上跟踪对话 There's a harmony branch for ES6 support. ES6支持有一个harmony分支 You can use the branch by replacing your package.json entry for uglify to: 您可以通过将package.json条目替换为以下来使用该分支:

"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. 这样,所有语法都将是ES5,Uglify非常了解它。 You might want to tweak your transpiler config if you want some of the ES6 syntax to remain intact. 如果您希望某些ES6语法保持不变,您可能需要调整您的转发器配置。

I decided to write here a solution. 我决定在这里写一个解决方案。 I didn't have to install other uglify-js package versions. 我没有必要安装其他uglify-js包版本。 The point was to solve imports to objects in proper way. 重点是以适当的方式解决对象的导入问题。 In my case the API_URL was a global variable. 就我而言, API_URL是一个全局变量。 So Uglify wasn't sure if it's defined, that's why it threw an error. 因此,Uglify不确定它是否已定义,这就是它抛出错误的原因。

To solve that problem I used webpack externals in this way: 为了解决这个问题,我用这种方式使用了webpack externals

// ------------------------------------                                                                                               
// 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 ). 它只是将JSON配置对象放入config变量中,具体取决于环境( developmentproduction )。 All you need to do is to put development.json and production.json next to file where you define webpackConfig.externals . 您需要做的就是将development.jsonproduction.json放在您定义webpackConfig.externals文件webpackConfig.externals

Then as in my case, you define it let's say in development.json : 然后就像在我的情况下,你定义它让我们说在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. 希望能帮到别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uglify SyntaxError:意外的标记:punc ()) - Uglify SyntaxError: Unexpected token: punc ()) 意外的令牌运算符«=»,预期的punc«,» - Unexpected token operator «=», expected punc «,» ExecJS :: RuntimeError:SyntaxError:意外的令牌:get @(execjs)中的punc()):3538:630 - ExecJS::RuntimeError: SyntaxError: Unexpected token: punc ()) from get@(execjs):3538:630 错误:在最小化angularjs应用程序时出现了意外的标记punc«)»,预期的punc«,» - Error: Unexpected token punc «)», expected punc «,» while minifying angularjs app 意外的标记 punc «(»,从 UglifyJS 创建块时预期的 punc - Unexpected token punc «(», expected punc when creating chunk from UglifyJS ExecJS::ProgramError: 运行 rake 资产时出现意外的标记 punc «(»,预期的 punc «:»:在生产中预编译 - ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production 来自 UglifyJs 的 build.js 中的错误意外标记:punc (() - ERROR in build.js from UglifyJs Unexpected token: punc (() 消息:'意外的令牌:punc(。)',同时在grunt中使用uglify - message: 'Unexpected token: punc (.)', while using uglify in grunt 未捕获到的SyntaxError:意外令牌, - Uncaught SyntaxError: Unexpected token , 语法错误:意外的令牌 } - SyntaxError : Unexpected token }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM