protibimbok / django-vite-plugin

This plugin configures Vite for use with Django backend.
98 stars 13 forks source link

Exception in get_apps #60

Closed hfroot closed 1 month ago

hfroot commented 1 month ago

Hi, thanks for the plugin! It's very helpful, we managed to get a first version up and running.

We've now integrated wagtail into our project, and following the official documentation we have added wagtail to the list of INSTALLED_APPS.

Except this causes an exception when trying to build or run vue 3. LookupError: No installed app with label 'wagtail'. Did you mean 'wagtailcore'? I don't get this exception when running python manage.py runserver.

I cannot change the app name in the list to wagtailcore. When I do I get ModuleNotFoundError: on python manage.py runserver.

I can solve the issue if I comment out the line CONFIG["INSTALLED_APPS"] = self.get_apps(). But of course I don't know if that will break something later down the line, or whether this bug is a problem with wagtail rather than this plugin. What are your thoughts? Thanks in advance.

The full stack trace:

  File "/my/project/directory/vue3-frontend/../manage.py", line 24, in <module>
    main()
  File "/my/project/directory/vue3-frontend/../manage.py", line 20, in main
    execute_from_command_line(sys.argv)
  File "/my/project/directory/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/my/project/directory/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/my/project/directory/venv/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/my/project/directory/venv/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/my/project/directory/venv/lib/python3.10/site-packages/django_vite_plugin/management/commands/django_vite_plugin.py", line 29, in handle
    self.print_config()
  File "/my/project/directory/venv/lib/python3.10/site-packages/django_vite_plugin/management/commands/django_vite_plugin.py", line 61, in print_config
    CONFIG['INSTALLED_APPS'] = self.get_apps()
  File "/my/project/directory/venv/lib/python3.10/site-packages/django_vite_plugin/management/commands/django_vite_plugin.py", line 49, in get_apps
    APPS[app] = apps.get_app_config(app).path
  File "/my/project/directory/venv/lib/python3.10/site-packages/django/apps/registry.py", line 165, in get_app_config
    raise LookupError(message)
hfroot commented 1 month ago

I think that apps.get_app_config expects the app label, but it is being passed the app name. In the case of wagtail, the app config is defined as the following:

class WagtailAppConfig(AppConfig):
    name = "wagtail"
    label = "wagtailcore"

If CONFIG["INSTALLED_APPS"] is necessary, would it be possible to get the paths from apps.get_app_configs() ? e.g

def get_apps(self) -> Dict[str, str]:
    APPS={}
    app_configs = apps.get_app_configs()
    for app_config in app_configs:
        name = app_config.name
        if '.' in name or name == 'django_vite_plugin':
            continue
        APPS[name] = app_config.path
hfroot commented 1 month ago

I wanted to open a PR but I am struggling to get my local environment setup. Figured I'd let you know in case there is something that can be added to the docs. I did the following steps:

forked the repository, checked that out locally cd example/output virtualenv -p python3 venv source ./venv/bin/activate pip install django_vite_plugin pip install Django python manage.py migrate python manage.py runserver

Which went fine, I struggled with the JS side...

npm install npm run dev

gives me

✘ [ERROR] Failed to resolve entry for package "django-vite-plugin". The package may have incorrect main/module/exports specified in its package.json. [plugin externalize-deps]

...

The plugin "externalize-deps" was triggered by this import vite.config.js:2:33: 2 │ import { djangoVitePlugin } from 'django-vite-plugin'

The docs say

In your vite.config.js file, change ./django-vite-plugin/index.js to django-vite-plugin

but I don't see any instances of ./django-vite-plugin/index.js in the codebase.