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.
*/