Meteor package.js

在本章中,我们将学习如何创建自己的 meteor 包。
创建包
让我们添加在桌面上的新文件夹用来创建新的包。使用命令提示符窗口执行如下命令。
C:\Users\Administrator\Desktop\meteorApp> mkdir packages 

现在我们可以在上面创建的文件夹中创建你的包。运行命令提示符运行以下命令。Username 是您的 Meteor 开发者的用户名和package-name是你的包的名称。

C:\Users\Administrator\Desktop\meteorApp\packages>meteor create --package username:package-name

添加包

为了能够本地包添加到我们的应用程序,需要设置环境变量(ENVIRONMENT VARIABLE),将告诉 meteor 从本地文件夹加载包。右键单击"计算机"图标,然后选择属性/高级系统设置/环境变量/新建。

变量名应该是PACKAGE_DIR变量值路径应该是指向创建的文件夹。在我们的示例中是:C:\Users\Administrator\Desktop\meteorApp\packages.

注意:不要忘记在添加新的环境变量后重新启动命令提示符。
现在,我们可以运行下面的代码将包添加到我们的应用程序-
C:\Users\Administrator\Desktop\meteorApp>meteor add username:package-name

包文件

如果打开创建软件包的文件夹,你会看到四个文件。
  • package-name-test.js
  • package-name.js
  • package.js
  • README.md

测试包(package-name-test.js)

Meteor 提供 tinytest 包用于测试。让我们先从命令提示符窗口来安装它。
C:\Users\Adminitrator\Desktop\meteorApp>meteor add tinytest

如果你打开 package-name-test.js, 你会看到默认的测试例子。我们将用这个例子来测试应用程序。开发 Meteor 包时,你应该要写写自己的测试。

要测试包,需要运行在命令提示符下此代码。
 C:\Users\Administrator\Desktop>meteor test-packages packages/package-name
你应该得到以下结果。
Meteor Package Test

package.js文件

我们可以在这个文件中编写代码。现在我们在包中实现一些简单的功能。包将日志记录一些文本到控制台。

packages/package.js

myPackageFunction = function() {
   console.log('This is simple package...');
}

package-name.js文件

这是我们可以设定一些程序包配置文件。 我们一会再回到它,但现在需要导出myPackageFunction,就可以在应用程序中使用它了。我们需要添加这些到 Package.onUse 函数内。该文件将是这个样子。

packages/package-name.js

Package.describe({
   name: 'username:package-name',
   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.2.1');
   api.use('ecmascript');
   api.addFiles('mypackage.js');
   api.export('myPackageFunction'); // We are exporting the function we created above...
});

Package.onTest(function(api) {
   api.use('ecmascript');
   api.use('tinytest');
   api.use('username:package-name');
   api.addFiles('package-name-tests.js');
});
使用软件包
现在,我们终于可以从 app.js 文件调用myPackageFunction()函数了。

packages/package.js

if(Meteor.isClient) {
   myPackageFunction();
}
控制台将日志记录包中的文本,其结果如下:
Meteor Package Log
为了更好地理解package.js文件如何配置,我们将使用Meteor 官方文档的例子。

这是一个例子文件...看看一就知道了

/* Information about this package */
Package.describe({
   // Short two-sentence summary.
   summary: "What this does",
   // Version number.
   version: "1.0.0",
   // Optional.  Default is package directory name.
   name: "username:package-name",
   // Optional github URL to your source repository.
   git: "https://github.com/something/something.git",
});

/* This defines your actual package */
Package.onUse(function (api) {
   // If no version is specified for an 'api.use' dependency, use the
   // one defined in Meteor 0.9.0.
   api.versionsFrom('0.9.0');
   // Use Underscore package, but only on the server.
   // Version not specified, so it will be as of Meteor 0.9.0.
   api.use('underscore', 'server');
   // Use iron:router package, version 1.0.0 or newer.
   api.use('iron:router@1.0.0');
   // Give users of this package access to the Templating package.
   api.imply('templating')
   // Export the object 'Email' to packages or apps that use this package.
   api.export('Email', 'server');
   // Specify the source code for the package.
   api.addFiles('email.js', 'server');
});

/* This defines the tests for the package */
Package.onTest(function (api) {
   // Sets up a dependency on this package
   api.use('username:package-name');
   // Allows you to use the 'tinytest' framework
   api.use('tinytest@1.0.0');
   // Specify the source code for the package tests
   api.addFiles('email_tests.js', 'server');
});

/* This lets you use npm packages in your package*/
Npm.depends({
   simplesmtp: "0.3.10",
   "stream-buffers": "0.2.5"
});

 

posted @ 2017-08-18 15:50  h2z  阅读(382)  评论(0编辑  收藏  举报