Features

Installation

Checkout the latest django-require release and copy or symlink the
require directory into your PYTHONPATH. If using pip, run
pip install django-require.

Add 'require' to your INSTALLED_APPS setting.

Set your STATICFILES_STORAGE setting to
'require.storage.OptimizedStaticFilesStorage',
'require.storage.OptimizedCachedStaticFilesStorage' or
'require.storage.OptimizedManifestStaticFilesStorage'.

Available settings

Available settings, and their default values, are shown below. You
should configure this to match the layout of your project’s static
files. Please consult the RequireJS
documentation for more information about how to build javascript using
RequireJS.

# The baseUrl to pass to the r.js optimizer, relative to STATIC_ROOT.REQUIRE_BASE_URL="js"# The name of a build profile to use for your project, relative to REQUIRE_BASE_URL.# A sensible value would be 'app.build.js'. Leave blank to use the built-in default build profile.# Set to False to disable running the default profile (e.g. if only using it to build Standalone# Modules)REQUIRE_BUILD_PROFILE=None# The name of the require.js script used by your project, relative to REQUIRE_BASE_URL.REQUIRE_JS="require.js"# A dictionary of standalone modules to build with almond.js.# See the section on Standalone Modules, below.REQUIRE_STANDALONE_MODULES={}# Whether to run django-require in debug mode.REQUIRE_DEBUG=settings.DEBUG# A tuple of files to exclude from the compilation result of r.js.REQUIRE_EXCLUDE=("build.txt",)# The execution environment in which to run r.js: auto, node or rhino.# auto will auto-detect the environment and make use of node if available and rhino if not.# It can also be a path to a custom class that subclasses# require.environments.Environment and defines some "args" function that# returns a list with the command arguments to execute.REQUIRE_ENVIRONMENT="auto"

Generating require.js

As a shortcut to downloading a copy of require.js from the internet, you
can simply run the require_init management to copy a version of
require.js into your STATICFILES_DIRS, at the location specified by
your REQUIRE_BASE_URL and REQUIRE_JS settings.

$ ./manage.py require_init

Generating build profiles

In almost all cases, you’ll want to create a custom build profile for
your project. To help you get started, django-require can generate a
default build profile into your STATICFILES_DIRS. Just set your
REQUIRE_BUILD_PROFILE setting to a build profile name, and run
require_init. A good name for a build profile would be
'app.build.js'.

Any standalone modules that your specify with a build profile will also
have a default build profile generated when you run this command.

Running javascript modules in templates

You can run javascript modules in templates by using the
{% require_module %} template tag.

Building standalone modules

As a further optimization to your code, you can build your modules to
run independently of require.js, which can often speed up page load
times. Standalone modules are built using the almond.js shim, so consult
the almond.js documentation to
make sure that it’s safe to build your module in standalone mode.

To specify standalone modules, simply add them to your
REQUIRE_STANDALONE_MODULES setting, as below:

REQUIRE_STANDALONE_MODULES={"main":{# Where to output the built module, relative to REQUIRE_BASE_URL."out":"main-built.js",# Optional: A build profile used to build this standalone module."build_profile":"main.build.js",}}

Running the r.js optimizer

The r.js optimizer is run automatically whenever you call the
collectstatic management command. The optimizer is run as a
post-processing step on your static files.

django-require provides three storage classes that are ready to use with
the r.js optimizer:

require.storage.OptimizedStaticFilesStorage - A filesystem-based
storage that runs the r.js optimizer.

require.storage.OptimizedCachedStaticFilesStorage - As above, but
fingerprints all files with an MD5 hash of their contents for HTTP
cache-busting.

require.storage.OptimizedManifestStaticFilesStorage - As above, but
fingerprints all files with an MD5 hash of their contents for HTTP
cache-busting and stores the fingerprints in a JSON file on disk instead
of using a cache. Please note that the
OptimizedManifestStaticFilesStorage is only available in Django 1.7 and
above.

Creating your own optimizing storage classes

You can add r.js optimization to any Django staticfiles storage class by
using the require.storage.OptimizedFilesMixin. For example, to make
an optimizing storage that uploads to Amazon S3 using S3BotoStorage
from
django-storages: