How to build a Meteor smart package

前端 未结 5 1768
半阙折子戏
半阙折子戏 2020-12-01 00:54

How can one build a Meteor smart package that would show up in meteor list?

Building Atmosphere packages is reasonably well documented, but building Met

5条回答
  •  遥遥无期
    2020-12-01 01:21

    See cobberboy's answer below

    Below is outdated information:

    See info about the new meteor packaging system: https://meteorhacks.com/meteor-weekly-meteor-09-rc-meteor-new-logo-underscore-in-templates.html

    ** older information **

    There is updated information about writing your own package and about repackaging existing 3rd party libraries. The API wont be stable till 1.0 though, so be prepared to make many changes.

    I have included boiler plate to help w/ making it both a node and a meteor usable library at once. This took me quite some time to figure out, open to suggestions.

    package: /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: /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 app: some file in the proper client/server context (as defined in package.js)

    var my = new My('a property');
    my.doStuff(); // console logs 'hello world' on the server
    

    meteor app: smart.json , add your file to the packages list

    {
        packages:{
            "node-my": {
                "git": "git@github.com:myAccount/node-my.git"
            }
        }
    }
    

    Finally run mrt install on the command line to get it to install the package .. Whew!

提交回复
热议问题