django webpack loader render_bundle error

626 views Asked by At

We have a legacy Django-webpack app using Django webpack loader. The app code moved into a /app directory and the render_bundle stopped working. The error message is

  File "/usr/local/lib/python3.9/site-packages/webpack_loader/templatetags/webpack_loader.py", line 22, in render_bundle
    tags = utils.get_as_tags(
  File "/usr/local/lib/python3.9/site-packages/webpack_loader/utils.py", line 71, in get_as_tags
    bundle = _get_bundle(loader, bundle_name, extension)
  File "/usr/local/lib/python3.9/site-packages/webpack_loader/utils.py", line 47, in _get_bundle
    bundle = loader.get_bundle(bundle_name)
  File "/usr/local/lib/python3.9/site-packages/webpack_loader/loader.py", line 116, in get_bundle
    filtered_chunks = self.filter_chunks(chunks)
  File "/usr/local/lib/python3.9/site-packages/webpack_loader/loader.py", line 58, in filter_chunks
    ignore = any(regex.match(chunk)
  File "/usr/local/lib/python3.9/site-packages/webpack_loader/loader.py", line 58, in <genexpr>
    ignore = any(regex.match(chunk)
TypeError: expected string or bytes-like object

Versions: django-webpack-loader: 1.6.0 webpack-bundle-tracker: 0.4.3

webpack-stats.json

{"status":"done","publicPath":"http://localhost:8001/","chunks":{"app":[{"name":"app.js","publicPath":"http://localhost:8001/app.js","path":"/app/static/dist/app.js"}]}}

I hard-coded our STATICS_URL to try to match the documentation on django-webpack-loader DJANGO STATICS_URL: /app/static/dist/ WEBPACK PATH(output.path): /app/static/dist

WEBPACK_LOADER Code:

BASE_DIR = "/app"
STATIC_URL = "/app/static/dist/"
STATIC_ROOT = Path(BASE_DIR, "public")
STATICFILES_DIRS = [
    Path(BASE_DIR, "static"),
    Path(BASE_DIR, "static", "dist"),
]
WEBPACK_LOADER = {"DEFAULT": {"CACHE": not DEBUG}}

Code that triggers the error:

 {% render_bundle 'app' %} inside a index.html
2

There are 2 answers

0
user3647449 On

I have same issue from legacy code...

TypeError at / expected string or bytes-like object

this is ./manage.py runserver output...

tags = utils.get_as_tags( File "/home/dave/skyscanner/venv/lib/python3.10/site-packages/webpack_loader/utils.py", line 71, in get_as_tags bundle = _get_bundle(loader, bundle_name, extension) File "/home/dave/skyscanner/venv/lib/python3.10/site-packages/webpack_loader/utils.py", line 47, in _get_bundle bundle = loader.get_bundle(bundle_name) File "/home/dave/skyscanner/venv/lib/python3.10/site-packages/webpack_loader/loader.py", line 116, in get_bundle filtered_chunks = self.filter_chunks(chunks) File "/home/dave/skyscanner/venv/lib/python3.10/site-packages/webpack_loader/loader.py", line 58, in filter_chunks ignore = any(regex.match(chunk) File "/home/dave/skyscanner/venv/lib/python3.10/site-packages/webpack_loader/loader.py", line 58, in ignore = any(regex.match(chunk) TypeError: expected string or bytes-like object

0
user11265011 On

I just upgraded my legacy python project and encountered the exact same problem. The issue was that my package.json was using an old version of webpack-bundle-tracker, which was v0.7.0.

After upgrading my webpack-bundle-tracker to v2.0.1 (the same as django-webpack-loader) and updating my webpack.config.js according to the latest 'configuring-webpack-bundle-tracker' section in the readme, I reran the following command:

npx webpack --config webpack.config.js

Subsequently, I successfully solved the problem.