[英]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
变量中,具体取决于环境( development
或production
)。 All you need to do is to put development.json
and production.json
next to file where you define webpackConfig.externals
. 您需要做的就是将
development.json
和production.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.