带有 coffeescript 的流星包(以及继承和严格模式)

meteor package with coffeescript (and inheritance and strict mode)

我正在尝试使用 coffeescript 在严格模式下构建一个 meteor 包。 主要问题是 share 的使用,如 coffeescript meteor package 中所述。看来我误解了link中的解释,因为我得到以下错误:

 ReferenceError: __coffeescriptShare is not defined

打包在 javascript 中运行良好。我只是把 NotificationCommon 的定义放在 'use strict'.

之前

目标是

这是我使用 coffeescript 的版本:

notification_common.coffee:

'use strict'

class share.NotificationCommon
  constructor: ->
    @collection = new (Meteor.Collection)('notification')

    @SUCCESS = 'success'
    @ERROR = 'error'
    @INFO = 'info'
    @WARNING = 'warning'

notification_client.coffee:

'use strict'

class NotificationClient extends share.NotificationCommon
  constructor = ->
    self = @

    Meteor.subscribe 'user_notif'
    toastr.options = positionClass: 'toast-bottom-full-width'

    @collection.find({}).observe 'added': (notif) ->
      self.notify notif
      return

  notify = (notif) ->
    toastr[notif.type] notif.message, notif.title, notif.options
    @collection.update { _id: notif._id }, $set: notified: true
    return

Notification = new NotificationClient

notification_server.coffee:

'use strict'

class NotificationServer extends share.NotificationCommon
  constructor = ->
    self = @

    @collection.allow update: (userId, doc, fields, modifier) ->
          return doc.userId is userId

    Meteor.publish 'user_notif', ->
      if @userId
        return self.collection.find( userId: @userId, notified: false)
      return

  notify = (user_id, notif) ->
    self = @
    checkType = Match.Where((x) ->
      check x, String
      check x, Match.OneOf('success', 'error', 'info', 'warning')
      true
      )
    check notif,
      type: checkType
      message: String
      title: Match.Optional(String)
      options: Match.Optional(Match.Any)
      context: Match.Optional(Match.Any)
    if user_id isnt null
      if not Meteor.users.findOne(_id: user_id)
        throw new (Meteor.Error)('User id not found.')
      notif.userId = user_id
    notif.notified = false
    self.collection.insert notif
    return

Notification = new NotificationServer

package.js:

Package.describe({
  name: 'my:notification',
  version: '0.0.1',
  summary: 'User and system wide notification',
  git: '',
  documentation: 'README.md'
});

Package.onUse(function(api) {
  api.versionsFrom('1.1.0.2');

  api.use(['coffeescript','jquery']);
  api.use('chrismbeckett:toastr');

  api.export('Notification');

  api.addFiles('notification_common.coffee');
  api.addFiles('notification_server.coffee','server');
  api.addFiles('notification_client.coffee','client');

});

Package.onTest(function(api) {
  api.use(['tinytest','coffeescript']);
  api.use('my:notification');
  api.addFiles('notification-tests.js');
});

任何帮助将不胜感激。

解决方法是添加包 fds:coffeescript-sharepackage.js:

  api.use('fds:coffeescript-share');

请参阅 link 以获得解释。

如果包coffeescript的解释能够描述问题(与use strict相关)并告知上述问题的存在,将会很有帮助包。

编辑

感谢 的评论。 我可能会得出结论,目前还没有完整的解决方案来组合

  • 流星包
  • 咖啡脚本
  • 严格模式

编辑 2

我已经更深入地分析了这个问题,我认为 coffeescript 包中没有找到解决方案。

该问题应该由流星团队在包构建代码中解决。 我可以想象一个解决方案如下。在 Meteor 包 API 中添加了一条新指令:在 export 旁边还有一条类似 scopePackage 的指令。 使用 scopePackage 声明的变量将列在 /* Package-scope variables */ 下(在串联的 js 中)。 但是它们不会被导出(连接js中/* Exports */下的代码)。