Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config.py enhancement please? #321

Closed
kenanmasri opened this issue Jun 23, 2022 · 4 comments
Closed

Config.py enhancement please? #321

kenanmasri opened this issue Jun 23, 2022 · 4 comments

Comments

@kenanmasri
Copy link

Hello, thanks for the package its awesome.
I had some issues trying to use the utils from the package inside my python apps.

Due to this line:

from django.conf import settings

It means we might be using Django's lazy settings I think. Thus the settings.py would not be available:

user_config = getattr(settings, 'WEBPACK_LOADER', DEFAULT_CONFIG)

This would in turn give the default configs. At least in my implementation (which is templatetags specifically).

This is my code for reference

a file called templatetags/core.py

  from webpack_loader.utils import get_as_tags as webpack_loader_get_as_tags

  @register.simple_tag(name="render_bundle_prefetch")
  def do_render_bundle_prefetch(
          bundle_name, extension=None, config='DEFAULT', suffix='',
          attrs=''):
      rendered_bundle = webpack_loader_get_as_tags(
          bundle_name, extension=extension, config=config, suffix=suffix,
          attrs=attrs, is_preload=True)
      rendered_bundle = "\n".join(rendered_bundle)
      rendered_bundle = mark_safe(rendered_bundle.replace('rel="preload"', 'rel="prefetch"'))
      return rendered_bundle

Anyway this is my suggestion:

import re

def load_config(name):
    from django.conf import settings

    DEFAULT_CONFIG = {
        'DEFAULT': {
            'CACHE': not settings.DEBUG,
            'BUNDLE_DIR_NAME': 'webpack_bundles/',
            'STATS_FILE': 'webpack-stats.json',
            # FIXME: Explore usage of fsnotify
            'POLL_INTERVAL': 0.1,
            'TIMEOUT': None,
            'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
            'LOADER_CLASS': 'webpack_loader.loader.WebpackLoader',
            'INTEGRITY': False,
            # Whenever the global setting for SKIP_COMMON_CHUNKS is changed, please
            # update the fallback value in get_skip_common_chunks (utils.py).
            'SKIP_COMMON_CHUNKS': False,
        }
    }

    user_config = getattr(settings, 'WEBPACK_LOADER', DEFAULT_CONFIG)
    user_config = dict(
        (name, dict(DEFAULT_CONFIG['DEFAULT'], **cfg))
        for name, cfg in user_config.items()
    )

    for entry in user_config.values():
        entry['ignores'] = [re.compile(I) for I in entry['IGNORE']]

    return user_config[name]

I moved everything inside load_config because it doesn't want to expose other things:

__all__ = ('load_config',) 

Could you please make this change? I couldn't run tests though.

@kenanmasri
Copy link
Author

Made a pull request (my first!) #322

@fjsj
Copy link
Member

fjsj commented Jun 27, 2022

Hi @kenanmasri
This is strange, because it should be safe for external Django apps to import from django.conf import settings. Otherwise your app will not integrate with many third-party Django apps.

Have you added this app to your INSTALLED_APPS?

INSTALLED_APPS = (
  ...
  'webpack_loader',
  ...
)

@fjsj
Copy link
Member

fjsj commented Jun 27, 2022

Also, load_config is internal code called by get_loader. Where are you calling get_loader that causes Django settings to not be loaded yet?

@kenanmasri
Copy link
Author

Hi @fjsj
I am sorry because it appeared to me that I was importing:
from webpack_loader.utils import get_as_tags as webpack_loader_get_as_tags
twice. One is in settings.py
I have no idea how it slipped into the settings.py file.
Thanks for your time to help... No changes are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants