Search code examples
javascriptmeteorcoffeescriptpackagestrict-mode

meteor package with coffeescript (and inheritance and strict mode)


I'm trying to build a meteor package in strict mode with coffeescript. The main problem is the use of share as described in the coffeescript meteor package. It seems that I misunderstood the explanation in the link because I get the following error:

 ReferenceError: __coffeescriptShare is not defined

The packaged worked well in javascript. I just put the definition of NotificationCommon before the 'use strict'.

The goal is

  • NotificationCommon, scope package
  • NotificationClient and NotificationServer: scope file
  • Notification: export

This is my version with 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

and the 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');
});

Any help will be much appreciated.


Solution

  • The solution is to add the package fds:coffeescript-share in package.js:

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

    See the link for an explanation.

    It would be helpful if the explanation of the package coffeescript would describe the problem (related to use strict) and inform about the existence of the above-mentioned package.

    EDIT

    Thank you @jorjordandan for your comment. I may conclude that at the moment there is no complete solution for combining

    • meteor package
    • coffeescript
    • strict mode

    EDIT 2

    I've analysed deeper the problem and I thing that there is no solution to be found inside the coffeescript package.

    The problem should be solved by the meteor team in the package building code. I could imagine a solution as follows. A new instruction is added to the Meteor package API: beside export another named something like scopePackage. The variables declared with scopePackage will be listed under /* Package-scope variables */ (in the concatenated js). But they will be not exported (code under /* Exports */ in the concatenated js).