【发布时间】:2012-04-24 06:21:30
【问题描述】:
如何构建一个Meteor smart package 会显示在meteor list 中?
构建 Atmosphere 包相当不错 documented,但构建 Meteor 包不是。
【问题讨论】:
-
通知提示 - 已更新
如何构建一个Meteor smart package 会显示在meteor list 中?
构建 Atmosphere 包相当不错 documented,但构建 Meteor 包不是。
【问题讨论】:
Meteor 现在支持 create --package 命令。
示例(将您自己的meteor developer account 替换为“cunneen”):
meteor create --package cunneen:foo
输出:
cunneen:foo: created in your app
结果:
packages/cunneen:foo/package.js
Package.describe({
name: 'cunneen:foo',
version: '0.0.1',
// Brief, one-line summary of the package.
summary: '',
// URL to the Git repository containing the source code for this package.
git: '',
// By default, Meteor will default to using README.md for documentation.
// To avoid submitting documentation, set this field to null.
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.0.3.1');
api.addFiles('cunneen:foo.js');
});
Package.onTest(function(api) {
api.use('tinytest');
api.use('cunneen:foo');
api.addFiles('cunneen:foo-tests.js');
});
packages/cunneen:foo/foo.js(空文件)
// Write your package code here!
packages/cunneen:foo/foo-tests.js
// Write your tests here!
// Here is an example.
Tinytest.add('example', function (test) {
test.equal(true, true);
});
packages/cunneen:foo/README.md(空文件)
# cunneen:foo package
对于一个好的(非常全面的)示例,请查看iron-router。
【讨论】:
mrt create-package 似乎已经过时了。使用meteor create --package package-name
见鞋匠的answer below
以下是过时的信息:
查看有关新流星包装系统的信息: https://meteorhacks.com/meteor-weekly-meteor-09-rc-meteor-new-logo-underscore-in-templates.html
** 旧信息 **
有关于writing your own package 和关于repackaging existing 3rd party libraries 的更新信息。不过,API 在 1.0 之前都不会稳定,因此请准备好进行许多更改。
我已经包含了样板来帮助它同时成为一个节点和一个流星可用的库。这花了我相当长的时间来弄清楚,对建议持开放态度。
包:/lib/my.js
if (typeof Meteor === 'undefined) {
// Not Running In Meteor (nodejs code)
// example NPM/Node Dependencies that we'll use
var async = require('async');
var debug = require('debug')('my:package');
var mongodb = require('mongodb');
var http = require('http');
} else {
// Running as Meteor Package
var async = Npm.require('async');
var debug = Npm.require('debug')('my:package');
var mongodb = Npm.require('mongodb');
// node core module 'http'
// use Npm.require to require node core modules
// but doesnt need Npm.depends in the package.js file
var http = Npm.require('http');
}
var constructor = function(property1) {
this.property1 = property1; // or whatever in your constructor.
};
if (typeof Meteor === 'undefined') {
// Export it node style
My = exports = module.exports = constructor; // Limit scope to this nodejs file
} else {
// Export it meteor style
My = constructor; // Make it a global
}
// Proceed defining methods / properties as usual.
My.prototype.doStuff = function() { console.log('hello world'); }
包:/package.js
Package.describe({
summary: "My Meteor Package"
});
/**
* Ex: Some NPM Dependencies
*/
Npm.depends({
'async': '0.2.9',
'debug': '0.7.2',
'mongodb': '1.3.18'
});
/**
* On use we'll add files and export our tool
*/
Package.on_use(function (api) {
/**
* Add all the files, in the order of their dependence (eg, if A.js depends on B.js, B.js must be before A.js)
*/
api.add_files([
'lib/my.js' // <-- include all the necessary files in the package
],
'server'); // Can be 'server', 'client' , ['client','server']
/**
* Only expose the My constructor, only export if meteor > 0.6.5
*/
api.export && api.export(['My'], 'server'); // 1st arg can be array of exported constructors/objects, 2nd can be 'server', 'client', ['client', 'server']
});
meteor 应用程序:适当的客户端/服务器上下文中的某个文件(如 package.js 中所定义)
var my = new My('a property');
my.doStuff(); // console logs 'hello world' on the server
meteor app: smart.json ,将你的文件添加到包列表中
{
packages:{
"node-my": {
"git": "git@github.com:myAccount/node-my.git"
}
}
}
终于在命令行运行mrt install让它安装包..哇!
【讨论】:
mrt之前运行mrt add node-my
注意:包开发目前没有记录,API 将会改变。您已被警告!
也就是说,它实际上很容易上手:
首先,git clone 一份流星 repo 的副本。在 /packages 中为自己创建一个新目录。将 package.js 文件放在目录中(参见其他包的示例)。现在你有一个包裹!
接下来,从您的结帐处(不是安装程序安装的那个)运行流星脚本。从结帐运行时,脚本将使用结帐中的本地包目录。当您更改包中的代码时,它甚至会热重载。
查看其他包中的示例并了解 API 的作用。
编辑:在第三方软件包方面取得了很大进展。查看http://oortcloud.github.com/meteorite/ 和https://atmosphere.meteor.com/
【讨论】:
meteor 文件夹,下载自定义 jquery 构建并将生成的 js 文件放在 packages 内的新子文件夹中。我从现有的jquerypackage 复制/粘贴了一个package.js 文件并编辑了它的内容以反映我的自定义jquerybuild 的名称。接下来我走到我克隆的meteor文件夹的根目录并运行./meteor,我得到了Installed dependency kit v0.1.4 in dev_bundle.。到目前为止,一切都很好。但是运行meteor list 并没有显示我的新包。想法?
EventedMind 上有关于此主题的精彩截屏视频。
【讨论】:
这是 2013 年 6 月 12 日。这是当时的正确答案,仍然是替代解决方案:
就像 n1mmy 说的。它是无证的,你应该使用陨石。
如果你坚持用流星创建一个包,我找到了一个很好的非官方 How-to,但你真的不应该这样做。 Meteor 将在即将发布的版本中提供一种创建包的方法。
构建 Meteor 包: https://coderwall.com/p/ork35q
我会用 Meteorite 做到这一点
显然你有 node,我假设你有 node 包管理器 (npm),所以迄今为止制作流星包的最佳方法是制作陨石智能包。
npm install meteorite
Meteorite 智能包包含创建包所必需的 2 个关键文件 - 包.js - smart.json
Meteorite 文件存储在您系统登录的用户帐户下:~/.meteorite/
但符号链接到您当前创建流星应用程序的位置:project/.meteor/meteorite/
示例 package.js:
Package.describe({
summary: "User analytics suite for meteor"
});
Package.on_use(function (api) {
api.add_files('user_analytics.js', 'client');
});
示例 smart.json
{
"name": "User analytics",
"description": "User Analytics",
"homepage": "http://yourHomepage.com",
"author": "Eric Leroy",
"version": "0.1",
"git": "https://github.com/yipyo",
"packages" : {}
}
如果你需要更多信息,你应该从列表中安装一个 mrt 包:
mrt list
然后分析你的 app/.meteor/meteorite/ 目录下的文件。
希望这会有所帮助,并继续开发未来最好的语言。
这里有一些有用的链接:
【讨论】:
mrt list 实际上将list 命令传递给meteor,因此您将获得智能包,而不是“mrt 包”(即 Atmosphere 上的那些)