Skip to main content

Asset packager for Django.

Project description

DPack

DPack is a static asset packager, written primarily for Django applications.

Installation

pip install dpack

DPack has a number of optional processors for things like minification and compilation:

  • pip install dpack[cssmin]
  • pip install dpack[jsmin]
  • pip install dpack[sass]

Or get everything with pip install dpack[all].

Configuration

DPack can be configured either via a dpack.yaml file, or via a DPACK setting if using Django. The following options are available:

  • assets - a dictionary whose keys are paths of files to be created (relative to output), and whose values are lists of files to process and concatenate into said file. Each input file in the list may be prefixed by one or more processors by specifying the processor name followed by a colon. For instance, cssmin:sass:somefile.scss tells DPack to first compile somefile.scss (found by searching in search directories) using SASS, then minify it using the cssmin processor.
  • defaults - a dictionary whose keys are file extensions (without the .), and whose values are lists of processors to use by default for input files of that type.
  • output - the path to store packed assets under. If not specified, this will be a temporary directory created using tempfile.mkdtemp(prefix="dpack-").
  • prefix - the URL prefix compiled assets will ultimately be served from, used when rewriting url and @import declarations in CSS files via the rewrite processor. If using DPackFinder, this defaults to STATIC_URL.
  • register - a dictionary whose keys are processor names you wish to register (or override), and whose values are dotted-path strings that resolve to a callable. See processors below.
  • search - a list of directories to search for input files in. If using DPackFinder, input files will be searched by using any STATICFILES_FINDERS that are not DPackFinder itself.

Example dpack.yaml

assets:
  compiled/site.css:
    - app1/first.css
    - app2/second.css
    - cssmin:sass:app3/third.scss
  compiled/site.js:
    - app1/first.js
    - app2/second.js
defaults:
  css:
    - rewrite
    - cssmin
  js:
    - jsmin
output: ./build
prefix: /static/
register:
  custom: myapp.processors.custom
search:
  - ./app1/static
  - ./app2/static

Example DPACK Setting

DPACK = {
    "assets": {
        "compiled/site.css": [
            "app1/first.css",
            "app2/second.css",
            "cssmin:sass:app3/third.scss",
        ],
        "compiled/site.js": [
            "app1/first.js",
            "app2/second.js",
        ],
    },
    "defaults": {
        "css": ["rewrite", "cssmin"],
        "js": ["jsmin"]
    },
    "output": "./build",
    "register": {
        "custom": "myapp.processors.custom"
    },
}

Using DPackFinder With Django

Simply add dpack.finders.DPackFinder to your STATICFILES_FINDERS setting, and DPack will search for inputs using Django's staticfiles app, output compiled assets when collectstatic is called, and generate assets on-the-fly when serving with runserver (DEBUG=True) or via the django.contrib.staticfiles.views.serve view.

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "dpack.finders.DPackFinder",
)

If you compile an asset to compiled/css/site.css, you can reference it as you would any other static asset, with {% static "compiled/css/site.css" %} in your templates. These assets are also then post-processed by your STATICFILES_STORAGE, so you can use things like Whitenoise's CompressedManifestStaticFilesStorage with no extra configuration.

Command Line Interface

DPack comes with a command-line utility, unsurprisingly named dpack. Run by itself, it will look for a dpack.yaml config file and pack any assets it finds according to the config. You can specify a config file (-c) or Django settings module (-s), and dump out the loaded config using dpack -y. Run dpack -h for a full list of options.

Processors

Processors are simply Python callables that take three arguments: text (the processed text so far), input (the dpack.base.Input object containing things like relative name and absolute path), and packer (an instance of dpack.DPack containing things like prefix). For example, the cssmin processor is implemented as:

def process(text, input, packer):
    return rcssmin.cssmin(text)

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

dpack-0.6.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dpack-0.6.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file dpack-0.6.1.tar.gz.

File metadata

  • Download URL: dpack-0.6.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dpack-0.6.1.tar.gz
Algorithm Hash digest
SHA256 875443db366939c3c54b60affce601f1ac40a1b826d85c63446c138ecc324f3a
MD5 a7a6fb4e3eac76f3fa262852787db3de
BLAKE2b-256 fe57a6d40042c35c9fb9971e7abd28d9206005cb175c7ce3af190c681cd225fd

See more details on using hashes here.

File details

Details for the file dpack-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: dpack-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dpack-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e198e93e6db10ec261a6bc09c2b6c612810b491ed65e3ae6e2b1801040c0929f
MD5 a12b11c383c76aaec4587dcfcab430d3
BLAKE2b-256 5a794f2632eac8646cfa4f164de93e27f739ae01159a39b1b6da0aae41f8db10

See more details on using hashes here.

Supported by

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