A bedrock module that provides a client-side module loader and autoconfiguration capabilities for bower components.
- npm v3+
npm install bedrock-requirejs
Start a simple express server with bedrock-requirejs support:
var bedrock = ;;;;bedrockstart;
Start an express server with bedrock-requirejs support and an AngularJS frontend:
bower install bedrock-angular
var bedrock = ;;;;;bedrockstart;
bedrock-requirejs uses bedrock's configuration to expose its own
options and to expose a base RequireJS configuration for your application.
The base RequireJS configuration can be found at
bedrock.config.requirejs.config. There is also a base RequireJS
optimization configuration, which is used when using the
optimize API; it
is found at
bedrock.config.requirejs.optimize.config. These configurations
are used as a base on which any auto-generated configuration will be layered.
Any custom configuration information will not be overridden.
All other configuration variables are specific to bedrock-requirejs and more documentation about their usage can be found in config.js.
How It Works
With bedrock-requirejs, you should be able to
bower install a bower
component, restart your bedrock server, and then when you next visit
your website, the bower component will be served when requested.
To make this possible, bedrock-requirejs will scan your project's
bower components directory when bedrock starts. The value of
bedrock.config.requirejs.bower.bowerrc.directory will be used to determine
the bower components directory; it defaults to
bower_components. Using the
results of the scan and any manually configured options, bedrock-requirejs
generates a RequireJS project configuration that it serves via
bedrock-express using the route
also serves your bower components directory via the URL specified in
bedrock.config.requirejs.bower.componentsUrl, which defaults to
Manually configured options can be specified via
bedrock.config.requirejs.config, which is a basic RequireJS configuration
object. There are also special
used when generating
main.js, that can be replaced, if necessary, via
bedrock.config.requirejs.configSuffix. It's unlikely that these files need
to be replaced; if you find yourself thinking about replacing them, make sure
there isn't another feature that could be used instead to accomplish what
you're trying to do.
bedrock-requirejs also includes an API for running RequireJS's
optimizer. This feature can be used to concatenate (in dependency-sorted order)
into a single. minified file for use in production systems. The RequireJS
configuration that is fed into the optimizer is generated using any custom
bedrock.config.requirejs.optimize.config, which represents a
RequireJS optimization configuration object, combined with the
results of auto-scanning the bower components directory. A special, simplified
RequireJS module loader, almond is also injected into the optimized
file will be written to
bedrock-requirejs will also serve this file, via
To run the optimizer:
var brRequire = ;brRequire;
var brRequire = ;brRequire
Note: If you're using bedrock-views, it exposes a subcommand
that can run this API for your project for you, and it provides the HTML
pages that will serve either
based on your bedrock-views configuration options.
bedrock-requirejs also serves a simple event emitter API at
main.js uses to emit events listeners can use to
execute behavior before or after certain client-side modules are loaded.
There are two phases during which
main.js will load modules: early
and autoload. The modules that are loaded during the early phase
include all of those listed in
modules that are loaded during the autoload phase include those listed
bedrock.config.requirejs.autoload. The following events are emitted:
- Emitted before the autoload modules are loaded. This event allows
any early loaded modules to run initialization code after all early
loaded modules have been required but before any of the autoload
modules are required. In order to receive this event, a module must be
added to the
bedrock.config.requirejs.config.depsarray so it will be loaded during the early phase. The first parameter passed to any listener function will be an object where the keys are the values from
bedrock.config.requirejs.config.depsand where the values are their associated module APIs.
- Emitted before the autoload modules are loaded. This event allows any early loaded modules to run initialization code after all early loaded modules have been required but before any of the autoload modules are required. In order to receive this event, a module must be added to the
- Emitted after the autoload modules have all been required. Listeners can use this event to perform initialization/start up code that must be deferred until all autoload modules have been loaded. For example, a module that allows other modules to register with it before running initialization code may find listening for this event to be useful. An example of a module that uses this event is bedrock-angular, which waits for autoload modules to register themselves with angular prior to bootstrapping the main AngularJS application.