Skip to main content

Stimulus and Webpack Tools for Django

Make the usage of stimulus and webpack in Django fun (again)!

Introduction

This module comes with two django management commands to help with the setup as well as the execution of stimulus / webpack in django by using the well-known convention over configuration approach.

Getting started

Setup

First, you need a Django project. If you dont already have an app, create a new one. E.g. using

python manage.py startapp demo apps/demo

and add it as apps.demo in your settings.

Installation

Next, install the webpack-tools module in your python runtime, e.g. using

pip install webpack-tools 

or via your requirements.txt.

Add 'webpack_tools' to your INSTALLED_APPS in your `settings``

Install Webpack

To prepare your project (not just your app!) for the usage of webpack (and npm if not already done) just type

python manage.py webpack --init-full

This will

  • initialise npm (if no package.json is found)
  • create a default webpack.config.js (if none exists)
  • install all necessary npm packages for the execution of the webpack config

After that, you can always execute webpack simply by typing

python manage.py webpack --build

Note: Both commands can also be combined.

Create Stimulus Controllers (and View Templates)

Sadly, we have nothing to pack yet. So its time to create a Stimulus controller and integrate it in a view template.

Therefore, we have the python manage.py stimulus command.

To install one (or more) Controllers in your app, just execute

python manage.py stimulus {app.name} {controller1} .. {controllerN}

where {app.name} refers to the name of your app, as installed in your settings. You can give zero or more controllers to be created. Those should be named all lower with no special characters!

The plugin will then

  • Create a folder /javascript in your app root folder (if it doesnt already exist).
  • Create an application.js with sensible defaults (auto detection of controllers during webpack execution) if it doesnt exist.
  • Create a subfolder /controllers (if it doestn exist)
  • Create a stub for each controller named {controller}_controller.js (the default convention, in stimulus).

Optionally, you can also create one (or more) view template which has the proper setup to get started even quicker. For that, just append --templates {template1} .. {templateN} to the command above, i.e.

python manage.py stimulus {app.name} {controller1} .. {controllerN} --templates {template1} .. {templateN}

This will then create the respective templates in your apps /template folder and create ready to use templates.

Bundling with webpack

Now its the right time to come back to webpacks --bundle command. So just execute

python manage.py webpack --bundle

to get your Entrypoint (the application.js created above) with all controllers available. Those will be bundled in a single application.js file which will be copied to static/js/application.js where it can be imported in your templates (see the auto generated templates for the exact syntax).

Final words

Now, just take the stub template, integrate it in a view, wire up an url and... boom, your stimulus controllers are up and running, and ready to use.

Note: After changes in your controllers, dont forget to call python manage.py webpack --bundle to recreate the bundled asset.

Installing Controllers from other Apps

When you have a modular structure in your modules (or even install third-party modules) you may come accross the case where you want to include controllers from other modules into your entrypoint (application.js). There are two approaches. One is that the other modules provide npm packages and the other is to use relative imports to specify the path of the controllers js-file relative to your entrypoint.

The install_controller helps you in the second case and does all necessary changes in your application.js file. If one app defines a Controller (from the structure defines above) and it should be added to an entrypoint in another app, one can use the install_controller command. The Syntax is

python manage.py install_controller <target-app> <source-app> <controller-name>

If you have two apps apps/demo and apps/demo2 and want to install athe controller Mycontroller from apps/demo/javascript/controllers/mycontroller_controller.js into apps/demo2s entrypoint (which is apps/demo2/javascript/application.js) then you have to use the command

python manage.py install_controller apps.demo2 apps.demo mycontroller

Release Notes

Release 0.2.0

  • Added the install_controller command

Release 0.1.1

  • Fixed a bug where stimulus only works with --templates option.

Release 0.1.0

  • Initial Release

Community and Discussion

Please feel free to join the project on github: https://github.com/hotwire-django/stimulus-webpack-tools or join our Slack: https://join.slack.com/t/hotwire-django/shared_invite/zt-kl0e0plt-uXGQ1PUt5yRohLNYcVvhhQ (if its not working just ping j.feinauer@pragmaticminds.de for an invite or open an issue on github).

Metadata

Release files for webpack-tools 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for webpack-tools 0.2.0
File Size Uploaded Provenance
webpack-tools-0.2.0.tar.gz 12.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for webpack-tools 0.2.0
File Interpreter ABI Platform
webpack_tools-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.8 kB

Release files / webpack-tools-0.2.0.tar.gz

Download URL webpack-tools-0.2.0.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c4cfe2682665c07780a9d0dd72fa4f718d6eacf8f1d375d75cc74e8c9add6793
BLAKE2b-256 checksum
How to use checksums
71b7c089e5808fdd8e02b4d58841fc06796b4ae3db369c949781ab1437bb8859
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.0

Release files / webpack_tools-0.2.0-py3-none-any.whl

Download URL webpack_tools-0.2.0-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a505c2feb157d033101bea45669d0837728c7676fd78705fd540ccc6d6565442
BLAKE2b-256 checksum
How to use checksums
c5549b1a174131bbbab826851b94481c6294cf3606b78e9de96f12d45cbccfbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.0

Security for webpack-tools 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page