流星自定义包中的 CoffeeScript 命名空间导出 [英] CoffeeScript namespace exports in Meteor custom packages
问题描述
有几个小时,我无法让我的 Meteor 包导出在 CoffeeScript 文件中定义的变量.
For a few hours, I couldn't get my Meteor package to export variables defined in CoffeeScript files.
在 foo.coffee
中,例如,我尝试使用 @Foo = {foo: 1}
, Foo = {foo: 1}
、exports.Foo = {foo: 1}
等等,但 nothing 会起作用.
In foo.coffee
, for example, I tried using @Foo = {foo: 1}
, Foo = {foo: 1}
, exports.Foo = {foo: 1}
, and so on and so forth, but nothing would work.
最后,看了 Meteor coffeescriptgithub上的测试包,我放置了 api.export();
调用 before api.on_use()
调用包它工作了.
Finally, after looking at the Meteor coffeescript test package on github, I placed the api.export();
call before the api.on_use()
call for that package and it worked.
我的包设置如下:
foo/
.meteor/
.build/
foo.coffee
package.js
foo.coffee
class FooBar
constructor: ->
Foo =
FooBar: FooBar
package.js
Package.describe({
summary: "A package that makes foo with foobar"
});
Package.on_use(function(api) {
api.use("coffeescript", "client");
api.export("Foo", "client"); // <-- Moved this to *before* the on_use declaration
api.add_files("foo.coffee", "client");
});
推荐答案
Coffeescript 编译
Coffeescript compiles
@Foo =
FooBar: FooBar
到
(function() {
this.Foo = {
FooBar: FooBar
};
}).call(this);
你需要去掉Foo
之前的this.
,看看命名空间,但这不是一个好主意,因为您可能需要在修改原始咖啡文件后对其进行编译.
You need to remove the this.
before Foo
, take a look at namespace, but it's not a good idea since you might need to compile it once you modify the original coffee files.
这是我的诀窍:
添加一个名为 global_variables.js
的文件:
Add a file, named global_variables.js
:
Foo = this.Foo;
然后将它添加到你的 package.js 中:
Then add it in your package.js:
api.add_files('xxx', 'xxx', 'global_variables.js');
然后它就可以工作了!
这篇关于流星自定义包中的 CoffeeScript 命名空间导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!