npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fmd2

v0.1.1

Published

Factory Module Definition a simple factory module wrapper (Node, AMD, Browser, ...).

Downloads

5

Readme

Factory Module Definition

Requirements:

  • Node.js
  • npm (Node.js package manager)

Install

npm install fmd

Use

var Fmd = require("fmd");

Quick start

require("fmd")({
    target: "dist/lib",
    trim_whitespace: true,
    new_line: "unix",
    indent: 2
  })
  .vendor("bar", "Bar")
  .define("foo", ["source/foo.js"], {
    depends: {
      "bar": "Baz"
    },
    exports: "Foo",
    global: "Foo"
  })
  .build();

Files:

Input: "source/foo.js"

function Foo () {

}

Foo.prototype = new Baz();

Foo.prototype.qux = "foo";

Foo.prototype.bar = function () {

};

Foo.prototype.baz = function () {

};

Output: "dist/lib/foo.js"

;(function (root, factory) {
  if (typeof exports === "object") {
    // CommonJS
    module.exports = exports = factory(require("bar"));
  }
  else if (typeof define === "function" && define.amd) {
    // AMD
    define(["bar"], factory);
  }
  else {
    // Global (browser)
    root.Foo = factory(root.Bar);
  }
}(this, function (Baz) {

  function Foo () {

  }

  Foo.prototype = new Baz();

  Foo.prototype.qux = "foo";

  Foo.prototype.bar = function () {

  };

  Foo.prototype.baz = function () {

  };

  return Foo;

}));

Usage

New Factory Module Definition object

var fmd = require("fmd")(settings);
  • settings.target destination directory of modules
  • settings.factories (optional) orderd array of definition types to use, at least one type is required Default: ["commonjs", "amd", "global"]
  • settings.trim_whitespace (optional) boolean indication whether to trim trailing whitespace Default: null
  • settings.new_line (optional) new line char type "unix", "mac", "windows", "\n", "\r", "\r\n" (null: don't change) Default: null
  • settings.indent (optional) "\t", " ", 1, 2, 3, ... (null: don't add indent, number: count of spaces) Default: null
  • settings.amd_not_anonymous (optional) boolean indication whether to define amd not anonymous Default: null

Factory

Factory adds a "Factory Module Definition" handle. Predefined: commonjs, amd and global.

.factory( name, when, define )
  • name name/identifier of the factory definiton handle
  • when method which returns the if condition to check the enviroment
  • define method which returns the definition

Example:

fmd
  .factory("commonjs", function () {
    return "typeof exports === \"object\""
  }, function () {
    var depends = [],
      requires = this.getModule().require;

    // Compose dependencies
    _.each(requires, function (require) {
      depends.push("require(\"" + require + "\")");
    });

    return "// CommonJS\nmodule.exports = exports = " + this.factory() + "(" + depends.join(", ") + ");";
  });
// Or multiple factory definition
fmd
  .factory({
    "commonjs": [
      function () {
        return "typeof exports === \"object\""
      },
      function () {
        var depends = [],
          requires = this.getModule().require;

        // Compose dependencies
        _.each(requires, function (require) {
          depends.push("require(\"" + require + "\")");
        });

        return "// CommonJS\nmodule.exports = exports = " + this.factory() + "(" + depends.join(", ") + ");";
      }
    ]
  });

Vendor

Vendor doesn't create/build files, it adds just the ability to inherit (especially for global mode) from modules I don't want to create/build by my self.

.vendor( module [, global] )
  • module path/name of the module
  • global (optional) name of the global provided variable

Example:

fmd
  .vendor("foo", "Foo");
  .vendor("foo/bar", "FooBar");
// Or multiple vendor definition
fmd
  .vendor({
    "foo": ["Foo"],
    "foo/bar": ["FooBar"]
  });

Import

Import loads/copies modules which include a definiton already.

.import( module, sources [, global] [, concat(source1, sourceN)] )
  • module path/name of the module
  • sources array of file paths or (local, http or https locations), at least one source is required
  • global (optional) name of the global provided variable
  • concat (optional) an own method to concat the received contents of sources

Example:

fmd
  .import("foo/bar", ["http://example.com/foo/bar.js", "/soruces/foo/bar-extension.js"], "FooBar", function (base, ext) {
    return base + "\n\n/* Special extension */\n\n" + ext;
  })
  .import("foo", ["/soruces/foo.js"], "Foo")
  .import("bar", ["/soruces/bar.js"]);
// Or multiple import
fmd
  .import({
    "foo/bar": [
      ["http://example.com/foo/bar.js", "/soruces/foo/bar-extension.js"],
      "FooBar",
      function (base, ext) {
        return base + "\n\n/* Special extension */\n\n" + ext;
      }
    ],
    "foo": [
      ["/soruces/foo.js"],
      "Foo"
    ],
    "bar", [
      ["/soruces/bar.js"]
    ]
  });

Define

Define loads/copies scripts and wraps them in a factory module definition.

.define( module, sources, options [, concat] )
  • module path/name of the module
  • sources array of file paths or (local, http or https locations)
  • options.depends (optional) key / value (module / local variable name) paired dependencies
  • options.exports (optional) name of the variable to expose
  • options.global (optional) name of the property to expose on global object (window in browser)
  • concat (optional) an own method to concat the received contents of sources

Example:

fmd
  .define("bar", ["/soruces/bar.js"], {
    exports: "Bar",
    global: "Bar"
  })
  .define("foo", ["/soruces/foo.js"], "Foo", {
    exports: "Foo",
    global: "Foo"
  })
  .define("foo/bar", ["http://example.com/foo/bar.js", "/soruces/foo/bar-extension.js"], {
    depends: {
      "foo": "Foo",
      "bar": "Bar"
    },
    exports: "FooBar",
    global: "FooBar"
  }, function (base, ext) {
    return base + "\n\n/* Speciale extension */\n\n" + ext;
  });
// Or multiple define
fmd
  .define({
    "bar": [
      ["/soruces/bar.js"],
      {
        exports: "Bar",
        global: "Bar"
      }
    ],
    "foo": [
      ["/soruces/foo.js"],
      {
        exports: "Foo",
        global: "Foo"
      }
    ],
    "foo/bar": [
      ["http://example.com/foo/bar.js", "/soruces/foo/bar-extension.js"],
      {
        depends: {
          "foo": "Foo",
          "bar": "Bar"
        },
        exports: "FooBar",
        global: "FooBar"
      },
      function (base, ext) {
        return base + "\n\n/* Speciale extension */\n\n" + ext;
      }
    ]
  });

Build

Build starts the (async) process of creating defined modules and copying imported modules.

.build( [callback(createdFiles)] )
  • callback (optional) a callback handle which receive a list of the created file (relative to the target path)

Example:

fmd
  .build(function (createdFiles) {
    // Log the list of created files
    console.log(createdFiles);
  });