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

weare-requirejs-npm

v0.1.3

Published

Prepare Require.js config with files from NPM modules.

Downloads

4

Readme

Repository https://npm.dev.studioweare.com

WE_ARE Require.js NPM

Prepare Require.js config with files from NPM modules.

Installation

$ npm install weare-requirejs-npm

Usage (with "weare-npm-assets")

Say we're in a app named module and we use the module weare-npm-assets.

var express = require('express');
var app = express();
var npmAssets = require('weare-npm-assets')();
var requirejsNpm = require('weare-requirejs-npm')(npmAssets);

var requirejsScript = npmAssets.getFilePath(require, 'requirejs', 'require.js');
var myAppScriptPath = npmAssets.getFilePath(require, null, 'javascripts/app.js', 'src');

var requirejsConfig = requirejsNpm.getConfig(require, [
  { name: 'lodash' },
  { name: 'ractive', alias: 'Ractive', src: 'ractive.legacy.min.js' },
]);
/*
Config produced, ready to be use in browser's "requirejs".
{
  context: 'app',
  paths: {
    lodash: '/app/modules/lodash/index.js',
    Ractive: '/app/modules/ractive/ractive.legacy.min.js'
  }
}
*/

npmAssets.expressServe(app);
// Will create 4 virtual paths:
//   "/app/modules/requirejs" real path -> "/{root-of-the-app}/node_modules/requirejs"
//   "/app/src"               real path -> "/{root-of-the-app}/src"
//   "/app/modules/lodash"    real path -> "/{root-of-the-app}/node_modules/lodash"
//   "/app/modules/ractive"   real path -> "/{root-of-the-app}/node_modules/ractive"

app.use('/', function (req, res) {
  // Add the "baseUrl" property if your app is mounted.
  requirejsConfig.baseUrl = req.app.mountpath;

  res.send(
    '<script>' +
      'var requirejsConfig = '+JSON.stringify(requirejsConfig)+';' +
    '</script>' +
    '<script src="'+requirejsScript+'" data-main="'+myAppScriptPath+'" />'
  );
});

Usage (without "weare-npm-assets")

The module can be initialized without passing it the npmAssets if you don't plan to use it in your app.

Beware that if you use it and don't pass it, you'll end up with 2 npmAssets instances that can have same virtual paths. You'll also have to serve both. Say we're in a app named module and we don't use the module weare-npm-assets.

var express = require('express');
var app = express();
var requirejsNpm = require('weare-requirejs-npm')();

var requirejsConfig = requirejsNpm.getConfig(require, [
  { name: 'lodash' },
]);

requirejsNpm.expressServe(app);
// Will create a virtual path:
//   "/app/modules/lodash"    real path -> "/{root-of-the-app}/node_modules/lodash"

app.use('/', function (req, res) {
  // Add the "baseUrl" property if your app is mounted.
  requirejsConfig.baseUrl = req.app.mountpath;

  res.send(
    '<script src="http://requirejs.org/docs/release/2.1.20/minified/require.js" />' +
    '<script>' +
      'var appRequire = require.config('+JSON.stringify(requirejsConfig)+');' +
      'appRequire([\'lodash\'], function (_) {)' +
        'console.log(_.VERSION);' +
      '});'
    '</script>'
  );
});

Function references

Most useful:

Expert mode:

getConfig(cRequire, modulesList)

/**
 * Compute the basic configs for requirejs. The config will contain a "baseUrl"
 * and "paths" properties. Virtual static paths will be created and the config
 * will based on them.
 *
 * @param Function cRequire
 *   The contextual "require" function.
 * @param Array modulesList
 *   List of object representing modules/ressources.
 *   
 *   Properties for the objects.
 *   
 *   For NPM module:
 *   name:   (required)  The name of the corresponding NPM module;
 *   alias:  (optionnal) The key to retrieve the module.
 *                       Default to the NPM module name.
 *   src:    (optionnal) The specific file you target in the NPM module folder;
 *   relPath:(optionnal) Relative path to "src". Useful for not exposing the
 *                       entire module folder.
 *   require (optionnal) Use it if the dependency is not from this module.
 *
 *   For the contextual module (module calling "getConfig"):
 *   name:   (required)  Set it to "." to specify the current module;
 *   alias:  (optionnal) The key to retrieve the module.
 *                       Default to the NPM module name.
 *   src:    (optionnal) The specific file you target in the NPM module folder;
 *   relPath:(optionnal) Relative path to "src". Useful for not exposing the
 *                       entire module folder.
 *   require (optionnal) Use it if the dependency is not from this module.
 *
 *   For URL/folder path:
 *   alias:  (required)  The key to retrieve the ressource.
 *   src:    (required)  The URL/folder path;
 */

getBuildConfig(cRequire, modulesList)

/**
 * Compute the basic configs for "r.js". The config will contain a "baseUrl" and
 * "paths" properties. The config will be based on absolute "hard paths" of the
 * modules.
 *
 * @param Function cRequire
 *   The contextual "require" function.
 * @param Array modulesList
 *   List of object representing modules/ressources.
 *   
 *   Properties for the objects.
 *   
 *   For NPM module:
 *   name:   (required)  The name of the corresponding NPM module;
 *   alias:  (optionnal) The key to retrieve the module.
 *                       Default to the NPM module name.
 *   src:    (optionnal) The specific file you target in the NPM module folder;
 *   relPath:(optionnal) Relative path to "src". Will prepend "src". Useful if
 *                       you want to use the same module list as with "getConfig".
 *   require (optionnal) Use it if the dependency is not from this module.
 *
 *   For the contextual module (module calling "getConfig"):
 *   name:   (required)  Set it to "." to specify the current module;
 *   alias:  (optionnal) The key to retrieve the module.
 *                       Default to the NPM module name.
 *   src:    (optionnal) The specific file you target in the NPM module folder;
 *   relPath:(optionnal) Relative path to "src". Will prepend "src". Useful if
 *                       you want to use the same module list as with "getConfig".
 *   require (optionnal) Use it if the dependency is not from this module.
 *
 *   For URL/folder path:
 *   alias:  (required)  The key to retrieve the ressource.
 *   src:    (required)  The URL/folder path;
 */

expressServe(app)

If you pass a reference of the instance of npmAssets at the module initialization, that will trigger the instance expressServe function. If not, it'll trigger his own instance.

/**
 * Serve all the static path with Express.
 *
 * @param Function express
 *   Express reference.
 * @param Object app
 *   The Express application.
 */