在使用Google Closure Compiler,webpack和Firebase JS SDK编译js时出现JSC_NON_GLOBAL_DEFINE_INIT_ERROR
使用下面的工具编译js时会出现JSC_NON_GLOBAL_DEFINE_INIT_ERROR:
- webpack:3.5.6
- Google Closure Compiler 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' } }) ]
这个错误是由于@define
/ 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中,一切都是一个模块,因此它不是全局的。
最好的select可能是编写一个自定义加载器,将注释去掉。