如何在Coffeescript中编写Meteor包?

aza*_*tar 2 meteor

我在我的流星源中使用CoffeeScript [CS] /重/.事实上,我的项目中的所有内容都是使用CS编写的.我想用同一个令牌写包.它们应该如何组织,声明和书写,以便利用CS方言的强大功能,同时最大限度地提高可测试性和可移植性?

Dav*_*don 6

总之,你只需要api.use('coffeescript');在你Package.onUsePackage.onTest为了写你的软件包中的CoffeeScript.有关命名空间怪癖的概述,请参阅文档.

这是一个safe包含以下四个文件的包的简单示例:

package.js

Package.describe({
  name: 'safe',
  summary: 'Encrypt strings to keep them safe (or not)'
});

Package.onUse(function(api) {
  api.versionsFrom('1.1.0.3');
  api.export('Safe');
  api.use('coffeescript');
  api.addFiles('encrypt.coffee');
  api.addFiles('safe.coffee');
});

Package.onTest(function(api) {
  api.use('tinytest');
  api.use('safe');
  api.use('coffeescript');
  api.addFiles('tests.coffee');
});
Run Code Online (Sandbox Code Playgroud)

encrypt.coffee

# use the share object to export code to other files in the package
share.encrypt = (string) ->
  # a super strong encryption :)
  string.replace /[a-zA-Z]/g, (c) ->
    String.fromCharCode (if ((if c <= "Z" then 90 else 122)) >= (c = c.charCodeAt(0) + 13) then c else c - 26)
Run Code Online (Sandbox Code Playgroud)

safe.coffee

{encrypt} = share

class Safe
  constructor: (@string) ->

  encrypt: ->
    encrypt @string
Run Code Online (Sandbox Code Playgroud)

tests.coffee

Tinytest.add 'safe encryption', (test) ->
  safe = new Safe 'pandapants'
  test.equal safe.encrypt(), 'cnaqncnagf'
Run Code Online (Sandbox Code Playgroud)

这应该为您提供一个模板来开始.如果您需要进一步说明,请在评论中提问,我会根据需要更新答案.