Skip to main content

A Django package to use npm.js dependencies and transpile ES2015+

Project description

django-npm-mjs

A Django package to use npm.js dependencies and transpile ES2015+

This package is used by Fidus Writer to bundle JavaScript. We try to keep it as generic as possible, so if there is something that seems very odd and specific to Fidus Writer, it is likely just an oversight from us. Please contact us and we'll see what we can do about it.

This package similar to django-compressor in that it treats JavaScript files before they are served to the user. But there are some differences:

  • It does not mix different JavaScript module entry files. It only bundles everything imported from one entry file. With ES2015+ there is not as much need to have lots of JavaScript files operating in the global namespace.

  • It allows importing from one django app in another app within the same project as if they were in the same folder similar to how static files and templates are handled by Django.

  • It includes handling of npm.js imports.

  • The JavaScript entry files' base names do not change and an automatic version query is added to be able to wipe the browser cache (/js/my_file.mjs turns into /js/my_file.js?v=239329884). This way it is also possible to refer to the URL from JavaScript (for example for use with web workers).

  • It allows for JavaScript plugin hooks between django apps used in cases when a django project can be used both with or without a specific app, and the JavaScript from one app needs to import things from another app.

Quick start

  1. Install "npm_mjs"

     pip install django-npm-mjs
    
  2. Add "npm_mjs" to your INSTALLED_APPS setting like this::

     INSTALLED_APPS = [
         ...
         'npm_mjs',
     ]
    
  3. Define a PROJECT_PATH in the settings as the root folder of the project (PROJECT_DIR will also be accepted)::

     PROJECT_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
    
  4. Define a SETTINGS_PATHS in the settings to contain the paths of all setting files (settings.py + any local_settings.py or similar file you may have defined) - this is to transpile again whenever settings have changed::

     SETTINGS_PATHS = [os.path.dirname(__file__), ]
    
  5. Add the static-transpile folder inside the PROJECT_PATH to the STATICFILES_DIRS like this::

     STATICFILES_DIRS = (
         os.path.join(PROJECT_PATH, 'static-transpile'),
         ...
     )
    
  6. Load transpile, and use static template tags to your templates to refer to JavaScript files. All entry files to ES2015+ modules need to have *.mjs endings. Entries can look like this::

     {% load transpile %}
     ...
     <script type="text/javascript" src="{% static "js/index.mjs" %}"></script>
    

You can continue to load other resources such as CSS files as before using the static template tag::

    <link type="text/css" rel="stylesheet" href="{% static "css/fonts.css" %}" />
  1. Run ./manage.py transpile.

  2. Run ./manage.py runserver. Your ES2015+ modules will be served as browser compatible JS files and all static files will have a versioned ending so that you can set your static server to let browsers cache static files indefinitely as long as DEBUG is set to False.

NPM.JS dependencies

  1. Add package.json or package.json5 files into one or more of your apps. All package files will be merged.

  2. Import in your JS files from any of the npm modules specified in your package files.

  3. Run ./manage.py transpile.

  4. Run ./manage.py runserver.

Referring to the transpile version within JavaScript sources

In your JavaScript sources, you can refer to the version string of the last transpile run like this::

    transpile.VERSION

For example::

    let downloadJS = `download.js?v=${transpile.VERSION}` // Latest version of transpiled version of download.mjs

ManifestStaticFilesStorage

If you use ManifestStaticFilesStorage, import it from npm_mjs.storage like this:

from npm_mjs.storage import ManifestStaticFilesStorage

If you use that version, you can refer to other static files within your JavaScript files using the staticUrl() function like this:

const cssUrl = staticUrl('/css/document.css')

Note that you will need to use absolute paths starting from the STATIC_ROOT for the staticUrl() function. Different from the default ManifestStaticFilesStorage, our version will generally interprete file urls starting with a slash as being relative to the STATIC_ROOT.

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_npm_mjs-3.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

django_npm_mjs-3.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file django_npm_mjs-3.1.0.tar.gz.

File metadata

  • Download URL: django_npm_mjs-3.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for django_npm_mjs-3.1.0.tar.gz
Algorithm Hash digest
SHA256 9e3e090d300edb8411ad87bf4dbe574b9ee6343435aa183b6c3b306bf0f3546b
MD5 49436537030dd78fe6a85789b9e446a2
BLAKE2b-256 da3146ac79cae95a0867a46e952ecbc6254ff9e039957c3cec2094234f39e4fe

See more details on using hashes here.

File details

Details for the file django_npm_mjs-3.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_npm_mjs-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe7a9b4d8af803ca09a73488dac4bbc8d5c1e802e6e003af2f32c213b7b7d0b
MD5 68ff2d7005777115761069ff945cd277
BLAKE2b-256 32145cc4bb7ee71275b89e79720eec33edee83e4b5d86e4722fefe53a0186fd7

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