[英]SyntaxError: Unexpected token: punc ())
我收到了:
SyntaxError:來自UglifyJS的意外標記:punc())
它指向全局變量API_URL
的第一個字母。 我用這種方式實現了它:
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
下的全局變量:
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會拋出那種錯誤?
Uglify不完全支持ES6, 包括模板文字 。 您可以在Github上跟蹤對話 。 ES6支持有一個harmony
分支 。 您可以通過將package.json
條目替換為以下來使用該分支:
"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"
或者,您可能希望在縮小之前首先通過轉換器傳遞代碼。 這樣,所有語法都將是ES5,Uglify非常了解它。 如果您希望某些ES6語法保持不變,您可能需要調整您的轉發器配置。
我決定在這里寫一個解決方案。 我沒有必要安裝其他uglify-js
包版本。 重點是以適當的方式解決對象的導入問題。 就我而言, API_URL
是一個全局變量。 因此,Uglify不確定它是否已定義,這就是它拋出錯誤的原因。
為了解決這個問題,我用這種方式使用了webpack externals :
// ------------------------------------
// Externals
// ------------------------------------
webpackConfig.externals = {
config: JSON.stringify(require(`./${__DEV__ ? 'development' : 'production'}.json`)),
}
它只是將JSON配置對象放入config
變量中,具體取決於環境( development
或production
)。 您需要做的就是將development.json
和production.json
放在您定義webpackConfig.externals
文件webpackConfig.externals
。
然后就像在我的情況下,你定義它讓我們說在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()
})
它就像一個魅力。
希望能幫到別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.