Skip to main content

django-babel-transpiling

Bring babel to your django project, transpiling static .jsx files on the fly, without NodeJS require!

Install

pip install django-babel-transpiling

Static files transpiling config

  1. Add babel_transpiling to your django INSTALLED_APPS
  2. Add babel_transpiling.middlewares.StaticFilesTranspilingMiddleware to your django MIDDLEWARE

    note the order

    [
    ...
    'babel_transpiling.middlewares.StaticFilesTranspilingMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ... 
    ]
    
  3. Config django STATICFILES_STORAGE = 'babel_transpiling.storage.StaticFilesTranspilingStorage'

Global Options

Default options is:

{
    'transpiler': 'npm/@babel/standalone@7.12.15/babel.min.js',
    'extensions': ['.jsx'],
    'options': {
        'plugins': ['transform-import-cssm'],
        "presets": ["react"],
        "generatorOpts": {
            "jsescOption": {
                "minimal": True
            }
        }
    },
    'mimetypes': {
        '.jsx': 'application/javascript'
    },
    'setup': ['npm/babel-plugin-transform-import-cssm@1.0.0/index.standalone.js']
}

You can customize by provide BABEL_TRANSPILING in your django settings, for example, custom babel preset:

BABEL_TRANSPILING = {
    'options': {
        "filename": "index.ts",
        "presets": ["typescript"]
    }
}

Babel API

You can control more by set setup option, for example, write custom plugin and register:

$ cat path/to/setup.js

// Simple plugin that converts every identifier to "LOL"
function lolizer() {
  return {
    visitor: {
      Identifier(path) {
        path.node.name = 'LOL';
      }
    }
  }
}
Babel.registerPlugin('lolizer', lolizer);

Then config options:

BABEL_TRANSPILING = {
    'options': {
        'plugins': ['lolizer']
    },
    'setup': ['path/to/setup.js']
}

Template Support

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

{% load babel_transpiling %}
...
<script>
    {% transpiling %}
      ReactDOM.render(<App/>, document.querySelector('#root'))
    {% endtranspiling %}
</script> 
...

Template tag also support use custom transpiling option, for example, in context there exists a ts option:

{% transpiling options=ts %}
  const anExampleVariable: string = "Hello World"
  console.log(anExampleVariable)
{% endtranspiling %}

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 StaticFilesTranspilingStorage

Release files for django-babel-transpiling 0.3.2

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

Source distribution (sdist)

Source distribution for django-babel-transpiling 0.3.2
File Size Uploaded
django-babel-transpiling-0.3.2.tar.gz 390.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-babel-transpiling 0.3.2
File Interpreter ABI Platform
django_babel_transpiling-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 782.8 kB

Release files / django-babel-transpiling-0.3.2.tar.gz

Download URL django-babel-transpiling-0.3.2.tar.gz
Size 390.2 kB
Tags Source
SHA-256 checksum
How to use checksums
75e0f05e59d7670e2cd40ef35375ae34759130407450a031f01511a28c5d9795
BLAKE2b-256 checksum
How to use checksums
3d23904135e8f015f44af36298a12be568f80c9faeaac40ea417bf81665a6c81
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / django_babel_transpiling-0.3.2-py3-none-any.whl

Download URL django_babel_transpiling-0.3.2-py3-none-any.whl
Size 392.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bd568548e78a32ed63dcf8307acd9e1ca95812a899a58393602f704ec37adaa
BLAKE2b-256 checksum
How to use checksums
49bdc58616698a0772ed6c68c3d2ba4dcf6e15721165361a04121c1de27e5202
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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