Skip to main content

Compress requirejs-modules into bundles.

Project description

Precompiler for django-compressor to integrate RequireJS-modules into bundles.

This library does not use the r.js compressor, but instead collects the dependencies from your templates and resolves them into bundles, letting django-compressor do the compressing.

Installation

First, install django-compressor into your Django project and configure it. Then install django-compressor-requirejs:

pip install django-compressor-requirejs

and add requirejs.RequireJSCompiler to your COMPRESS_PRECOMPILERS setting:

COMPRESS_PRECOMPILERS = (
    ('text/requirejs', 'requirejs.RequireJSCompiler'),
)

You can now use the content type text/requirejs on your main RequireJS script tag:

{% compress js %}
    <script type="text/requirejs" src="{% static "website/js/libs/require.min.js" %}"></script>
{% endcompress %}

The library will by default generate a single bundle with all modules found in templates and their dependencies. Dynamic dependencies will not be found. It also sets the baseUrl to your STATIC_URL.

If COMPRESS_ENABLED is False, only the config will be added and RequireJS will load modules one by one, without bundles.

Settings

You can control RequireJS with three options:

REQUIREJS_PATHS is a dict in the same style as the RequireJS path config. This can be used for example to make jQuery available in the main namespace ("jquery": "<some path>/jquery.min").

REQUIREJS_BUNDLES is a dict to specify which modules get bundled together:

REQUIREJS_BUNDLES = {
   'abovethefold': ['website/awesome', 'website/evenmoreawesome'],
}

Every module not mentioned in this setting will end up in the main catch-all bundle.

REQUIREJS_APP_ALIAS (default None) allows the Javascript directory inside your static root to be addressed by just the app name. Require/define calls to website/some_module will be searched as {{ STATIC_URL }}/website/<alias>/some_module.js if not found in {{ STATIC_URL }}/website/.

Under the hood

django-compressor-requirejs makes use of RequireJS’s bundles configuration option, making it possible to bundle modules together and let RequireJS fetch the bundle when it needs one of the modules. The philosophy is that modules bundled together are really often used together, which lowers the amount of requests the browser has to do.

It uses the filter mechanism in django-compressor on the script tag used to load RequireJS itself, injecting a configuration pointing RequireJS to STATIC_URL and – if compression is enabled – the compressed bundle(s).

Discovery of modules is done by searching all template directories for calls to RequireJS and parsing their dependencies. This is a plain text search, no real parsing of Javascript or HTML is done (similar to makemessages). All found modules are then similarly scanned for their dependencies.

Since no parsing or evaluation is done, any dynamic loading of dependencies with variables is not supported. If you want to let django-compressor-requirejs pick it up, annotate the require() call with all options (if feasible, of course).

So django-require and compressor_requirejs exist.

Yes, they do; and if you want to use the (otherwise excellent) r.js compressor with django-compressor, please take a look at compressor_requirejs. And if you do not use django-compressor, visit etianen’s django-require.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-compressor-requirejs-0.3.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

django_compressor_requirejs-0.3-py2.py3-none-any.whl (13.8 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page