使用以下工具编译js时出现JSC_NON_GLOBAL_DEFINE_INIT_ERROR:
- webpack:3.5.6
- Google 闭包编译器 JS:20170806.0.0
- Firebase JS SDK:4.3.1
环境
- MacOS X:10.11.6
- node.js:v7.10.0
- npm:4.2.0
这是错误消息:
ERROR in app.bundle.js:5325 (JSC_NON_GLOBAL_DEFINE_INIT_ERROR) @define variable assignment must be global
使用 webpack.config.js 如下所示:
module.exports = {
plugins: [
new ClosureCompiler({
options: {
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT3',
compilationLevel: 'SIMPLE_OPTIMIZATIONS',
warningLevel: 'QUIET'
}
})
]
此错误可能是由于 firebase/utils/constants.js 内的 @define
注释造成的.
var CONSTANTS = exports.CONSTANTS = {
/**
* @define {boolean} Whether this is the client Node.js SDK.
*/
NODE_CLIENT: false,
/**
* @define {boolean} Whether this is the Admin Node.js SDK.
*/
NODE_ADMIN: false,
/**
* Firebase SDK Version
*/
SDK_VERSION: '4.3.1'
};
我不太清楚 Firebase
使用 @define
注释的原因,但我决定使用 UglifyjsWebpackPlugin作为暂定措施。
module.exports = {
plugins: [
new UglifyJSPlugin({
output: {
comments: false
}
}),
new ClosureCompiler({
options: {
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT3',
compilationLevel: 'SIMPLE_OPTIMIZATIONS',
warningLevel: 'QUIET'
}
})
]
幸运的是,UglifyjsWebpackPlugin 似乎解决了这个问题。
我的问题是,
- 有人知道更好的方法来解决这个问题吗?
- 您认为有任何不良副作用吗?
最佳答案
@define
注释必须是全局的。在 webpack 中,一切都是模块,因此它不是全局的。
最好的选择可能是编写一个自定义加载程序来删除该注释。
关于javascript - 使用 Google Closure Compiler、webpack 和 Firebase JS SDK 编译 js 时出现 JSC_NON_GLOBAL_DEFINE_INIT_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167869/