andreyfedoseev / django-static-precompiler

Django Static Precompiler provides template tags and filters to compile CoffeeScript, LiveScript, SASS / SCSS, LESS, Stylus, Babel and Handlebars. It works with both inline code and external files.
Other
222 stars 60 forks source link

Error when inline compiling babel #135

Closed cptx032 closed 3 years ago

cptx032 commented 3 years ago

Hello! When I do, in my django template file:

<script type="text/javascript">
  {% inlinecompile "babel" %}
    alert("Hey!")
  {% endinlinecompile %}
</script>

I'm getting:

StaticCompilationError at /
babel:
  stdin compilation requires either -f/--filename [filename] or --no-babelrc

Looks like the way the script is passed to babel is not working. How can I fix that?

My .babelrc

{
  "presets": [
    [
    "@babel/preset-env",
      {
        "modules": false,
        "targets": {
          "browsers": ["defaults"]
        },
        "useBuiltIns": false,
        "corejs": "3.6.5"
      }
    ]
  ],
  "sourceType": "script"
}

My settings.py

STATIC_PRECOMPILER_COMPILERS = [
    ("static_precompiler.compilers.Babel", {
        "executable": "/.../node_modules/.bin/babel",
    }),
]

My babel version: 7.12.10 (@babel/core 7.12.10) My Node version: v15.5.1 My Django Version: 1.11.27 My Python Version: 2.7

Thanks for your time!

cptx032 commented 3 years ago

Hi! I've found that babel needs the filename argument to compile from stdin as source. So what I do was to create a new compiler with:

from static_precompiler.compilers import Babel

class InlineBabel(Babel):
    name = "inlinebabel"

    def get_extra_args(self, *args, **kwargs):
        return super(InlineBabel, self).get_extra_args(
            *args, **kwargs
        ) + ["-f", "source"]

That worked for me