javascript - 使用 Google Closure Compiler、webpack 和 Firebase JS SDK 编译 js 时出现 JSC_NON_GLOBAL_DEFINE_INIT_ERROR

标签 javascript node.js firebase webpack google-closure-compiler

使用以下工具编译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/

相关文章:

javascript - 图像在点击时跳跃 5 个像素

javascript - VS2015错误 "Application is not currently attached to a script debug target that supports script diagnostics"

c++ - Node native 模块 - 链接静态库

java - Firebase实时数据库: How to stop offline write?(最佳方法)

javascript - react-native-dropdown-picker,如何修复其他组件上的下拉选择器覆盖

javascript - 如何测试 Google 电子表格脚本中范围参数的类型?

node.js - 来自 node.js 应用程序的 ArangoDB 事务请求返回 403 : forbidden

javascript - Mongoose 中 "SELECT * WHERE city != null"的模拟

ios - 哪些 Firebase 离线功能可用于测验应用程序 (ios)

javascript - 无法取消订阅实时更新 : Uncaught Error in onSnapshot: Error: Missing or insufficient permissions