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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

Details for the file django-compressor-requirejs-0.3.tar.gz.

File metadata

File hashes

Hashes for django-compressor-requirejs-0.3.tar.gz
Algorithm Hash digest
SHA256 5d8fa40e4122d85404268dbc2d1f1ac9d6405855242602eb041593b60bc48414
MD5 aeba6f45394235a55e9cf5863ef49d26
BLAKE2b-256 ab23cfdf7d2f1fa33065e2d07c68f7c6e6fb048e8f6616eb7be0598ae59efe73

See more details on using hashes here.

File details

Details for the file django_compressor_requirejs-0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_compressor_requirejs-0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ba3dd0bc2ce6bc7c7c1cee072d4949d154b74ef798f466beae6a8dbb23e79873
MD5 58eb623c6715a08ecd79bc7da5cd032c
BLAKE2b-256 aa3e0a31d9fa18024424fa0e7c347656ba17423c4484c2cd5700a078be1255fb

See more details on using hashes here.

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