zoukankan      html  css  js  c++  java
  • Meteor package.js

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

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

    C:UsersAdministratorDesktopmeteorApppackages>meteor create --package username:package-name
    

    添加包

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

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

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

    包文件

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

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

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

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

    要测试包,需要运行在命令提示符下此代码。
     C:UsersAdministratorDesktop>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"
    });
  • 相关阅读:
    常用SQL Server数据修复命令DBCC一览(转载)
    多线程编程的注意事项
    [转载]Openlayers中使用TileCache加载预切割图片作为基础地图图层
    一个关于工具条可以移动和在四周停留的测试
    SQL Server同名表的判断
    SQL Server 中调整自增字段的当前初始值
    IIS and ASP.NET: The Application Pool
    Server.MapPath(path)
    自动化selenium 鼠标键盘操作& 按键用法
    多层窗口定位&多层框架定位
  • 原文地址:https://www.cnblogs.com/h2zZhou/p/7390081.html
Copyright © 2011-2022 走看看