requirejs-configurator
v1.3.1
Published
A generic require.js configuration generator for npm style projects
Downloads
4
Readme
requirejs-configurator
A generic require.js map/package configuration generator. It can generate require.js from arbitrary package layout on disk.
requirejs-configurator is not intended as the best workflow in terms of web app development (check out webpack, browserify, Rave.js). It's more of a building block for other tools. It's quite a specialised package so to speak. It supports npm
's package layout out of the box so it could prove to be useful for simple Require.js/Cajon based projects.
Programmatic usage
requirejs-configurator
can be used to generate configuration for npm based projects.
var rc = require("requirejs-configurator");
There is a promise API
rc.npm("path/to/my/project").then(function () {
console.log(config);
});
And the node style callback API
rc.npm("path/to/my/project", function (err, config) {
console.log(config);
});
Use generate
for more advanced use cases, e.g.
rc.generate({
dependencies: ["foo@^1.0.0", "bar@*"],
resolve: function (name, version, basedir, cb) {
// npm config generator uses the `node-resolve` which uses node"s
// `node_modules` traversal algorithm. By providing a custom resolver
// it's possible to support different package layouts, e.g. a flat
// structure with package@version directories, etc.
cb(null path.resolve(__dirname, "base", name, version));
}
});
Note that the options.resolve
function can either take a callback or return a promise.
Command line usage
There's a small command line tool bundled with requirejs-configurator
which can generate a configuration file for npm based projects, use it like so:
npm install -g requirejs-configurator
and then
requirejs-configurator --npm . > config.js
This assumes that node_modules
will be served at your require.js baseUrl. If that's not the case, you'll have to use the programmatic API and modify the generated configuration by looping over the packages array and updating the location fields.
Include devDependencies
NPM package.json devDependencies can be included programatically:
```js
rc.npm("path/to/my/project", { includeDevDeps: true }).then(function () {
console.log(config);
});
or
rc.npm("path/to/my/project", { includeDevDeps: true }, function (err, config) {
console.log(config);
});
And using the CLI
```sh
requirejs-configurator --npm --include-dev-deps . > config.js
Example output
Here is what the output of generating configuration for requirejs-configurator
itself looks like (+ Backbone just to demonstrate nested dependencies). With such configuration, it's then possible to call require("lodash")
or require("[email protected]")
or require("lodash@^2.4.1")
and all nested dependencies are resolved correctly since they're configured in the map.
{
map: {
'[email protected]': {
'underscore': '[email protected]'
},
'*': {
'backbone': '[email protected]',
'lodash': 'lodash@^2.4.1',
'lodash@^2.4.1': '[email protected]',
'ramda': 'ramda@^0.4.0',
'ramda@^0.4.0': '[email protected]',
'resolve': 'resolve@^1.0.0',
'resolve@^1.0.0': '[email protected]',
'when@^3.4.4': '[email protected]',
'when': 'when@^3.4.4',
'underscore@>=1.5.0': '[email protected]'
}
},
packages: [{
name: '[email protected]',
main: 'backbone.js',
location: 'node_modules/backbone'
}, {
name: '[email protected]',
main: 'dist/lodash.js',
location: 'node_modules/lodash'
}, {
name: '[email protected]',
main: 'ramda.js',
location: 'node_modules/ramda'
}, {
name: '[email protected]',
main: 'index.js',
location: 'node_modules/resolve'
}, {
name: '[email protected]',
main: 'when',
location: 'node_modules/when'
}, {
name: '[email protected]',
main: 'underscore.js',
location: 'node_modules/backbone/node_modules/underscore'
}]
};
Example project
See a fully working example over here.