Skip to main content

Bring `postcss-modules` to your django project, transpiling static .cssm files on the fly, without NodeJS require!

Project description

django-postcss-modules

Bring postcss-modules to your django project, transpiling static .cssm files on the fly, without NodeJS require!

Install

pip install django-postcss-modules

Static files transpiling config

  1. Add postcss_modules to your django INSTALLED_APPS
  2. Add postcss_modules.middlewares.PostCSSModulesMiddleware to your django MIDDLEWARE

    note the order

    [
    ...
    'postcss_modules.middlewares.PostCSSModulesMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ... 
    ]
    
  3. Config django STATICFILES_STORAGE = 'postcss_modules.storage.PostCSSModulesStorage'

Global Options

Default options is:

{
    'polyfill': 'postcss-modules-v8-polyfill.js',
    'transpiler': 'npm/postcss-modules-standalone@1.0.1/index.bundle.min.js',
    'setup': 'postcss-modules-setup.js',
    'extensions': ['.cssm'],
    'mimetypes': {
        '.cssm': 'text/css'
    },
    'max_time': 3 # transpiling max wait time, in second
}

You can customize by provide POSTCSS_MODULES in your django settings, for example, custom max time:

POSTCSS_MODULES = {
    'max_time': 30
}

Template Support

Sometimes transpiling in your template file is more make sense than static file, you can use postcssmodules tag to do that.

{% load static postcss_modules %}
...
<style id="indexStyles">
{% postcssmodules %}
:global .page {
  padding: 20px;
}

@value hi from '{% static "myapp/css/consts.cssm" %}';

.title {
  composes: title from "{% static "myapp/css/mixins.cssm" %}";
  color: green;
}

.article {
  font-size: 16px;
  color: hi;
}
{% endpostcssmodules %}
</style>
...
<script>
  const _styles = document.querySelector('#indexStyles').sheet
  const styles = JSON.parse(_styles.cssRules[_styles.cssRules.length - 1].style.getPropertyValue('--json'));
  element.innerHTML = '<div class="' + styles.title + '">';
</script>
...

Template tag also support use custom transpiling option, for example:

{% postcssmodules max_time=30 %}
.title {
  composes: title from "{% static "myapp/css/mixins.cssm" %}";
  color: green;
}
{% endpostcssmodules %}

FAQ

  1. Static file not get transpiled

    if you use django runserver command to run server and the setting DEBUG=True, please add --nostatic option to command

  2. I want use other storage

    you should write your own storage to inherit PostCSSModulesStorage

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-postcss-modules-0.1.2.tar.gz (339.9 kB view details)

Uploaded Source

Built Distribution

django_postcss_modules-0.1.2-py3-none-any.whl (342.6 kB view details)

Uploaded Python 3

File details

Details for the file django-postcss-modules-0.1.2.tar.gz.

File metadata

  • Download URL: django-postcss-modules-0.1.2.tar.gz
  • Upload date:
  • Size: 339.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.6.9

File hashes

Hashes for django-postcss-modules-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2f5c7de462a62124632d7a1f6d1d15260f65587208983a59a3682c268886ef7a
MD5 feae3ce4d613ea959c3eed39442adef3
BLAKE2b-256 6dcf64f425afa09f65949fb6b9d04c2289965262f85a458b2a9e83b8cd0fa167

See more details on using hashes here.

File details

Details for the file django_postcss_modules-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: django_postcss_modules-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 342.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.6.9

File hashes

Hashes for django_postcss_modules-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9cd2f7da8e997decc00ec2646bf0bba24b366d2eaee6a67faeeb13d4ea8c104f
MD5 1624db4f5df21e9d2fe0cb3fe8ba80d6
BLAKE2b-256 ebe86d22c15faaff4b050708b6cb2e6b4a5402dac77bfffc71aef1da643ab51b

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