protibimbok / django-vite-plugin

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

Ability to pass different DJANGO_SETTINGS_MODULE value from command line when running npm run dev? #14

Closed unkleara closed 1 year ago

unkleara commented 1 year ago

Hello. I have been trying to cater this to a project I am working on with different sites that run under a single Django project. However, it looks like running npm run dev needs to access manage.py and It only works with the manage.py that has a value already set for DJANGO_SETTINGS_MODULE in the root folder. If I try and pass another setting to use, DJANGO_SETTINGS_MODULE='config.site1.dev' or DJANGO_SETTINGS_MODULE='config.site2.dev' it doesn't work and I'm not sure which manage.py it's even trying to use as any any debug logging in the original manage.py that works with "npm run dev" is not output.

The only warning I see in this scenario is"(!) Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency pre-bundling." but any console logging statements in either node or Django are not output from the expected files.

Since it's trying to access manage.py even from the node/vite environment. Is it possible to pass command line arguments at the moment just like you would when running the Django app? "manage.py runserver 8000 DJANGO_SETTINGS_MODULE='config.site1.dev'" or is this configurable somehow?

Thank you.

protibimbok commented 1 year ago

Please provide me a way to replicate your environment and problem.

unkleara commented 1 year ago

I am still using the example project for now to experiment but honestly the main question is it possible to pass a command line argument while running "npm run dev" and have your plugin use it at the moment? Example: "npm run dev DJANGO_SETTINGS_MODULE='another.config".

The quickest way to replicate it is. Duplicate another config file in your example application is currently setup as "config.settings". This settings file copy and rename this file to settings2.py in the same folder and have both the Django app use it by running "manage.py runserver 8000 DJANGO_SETTINGS_MODULE=config.settings2" and then have your django-vite-plugin work with this new config file. The big difference is you are passing an explicit config to use instead of having it just use what is declared in manage.py. From the testing I've done running "npm run dev" only works with the value set in manage.py.

protibimbok commented 1 year ago

Inshallah this will be fixed in the next npm release. To achieve this you have to do this in your vite.config.js

export default defineConfig({
    plugins: [
        djangoVite({
            // Other options
            pyArgs: ['--settings', 'config.settings2']
        })
    ],
});