protibimbok / django-vite-plugin

This plugin configures Vite for use with Django backend.
112 stars 14 forks source link

Can we prevent build from requiring Python? #63

Closed kevinob11 closed 2 months ago

kevinob11 commented 3 months ago

I need to run my vite build process in CI using a simple node docker container, but right now npm run build requires python and bootstrapping the django app. What do you think about adding an optional config var to the vite.config.ts options that prevents it from running python? It looks like it is only really doing that to pull config from settings.py and get the django version. I'm happy to just enter those manually if we can avoid loading python / django.

cpontvieux-systra commented 3 months ago

Here is what I do, for now:

export default defineConfig({ server: { host: 'localhost', port: 1 * (env.PORT || '8080'), }, plugins: [ vue(), djangoVitePlugin({ pyPath: env.PYPATH || 'python', // hack to skip python in unit test CI. ... }), ], ... })



That works… but frankly it’s a bit complicated and I’m pretty sure my team’s collegues may no follow…
kevinob11 commented 3 months ago

Yeah, in the end I just used a separate vite config that didn't rely on their module for the build:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  build: {
    manifest: true,
    outDir: 'app/static/vite',
    rollupOptions: {
      input: [
        'path/file1.tsx',
        'path/file2.tsx',
      ]
    }
  }
})

Then my build is just vite --config [path to config file] and that seems to work fine. I don't love having the file list in multiple places, but I prefer this simpler approach vs the build process relying on python in any way.

protibimbok commented 2 months ago

Yeah, in the end I just used a separate vite config that didn't rely on their module for the build:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  build: {
    manifest: true,
    outDir: 'app/static/vite',
    rollupOptions: {
      input: [
        'path/file1.tsx',
        'path/file2.tsx',
      ]
    }
  }
})

Then my build is just vite --config [path to config file] and that seems to work fine. I don't love having the file list in multiple places, but I prefer this simpler approach vs the build process relying on python in any way.

This should be the way to go for now. We need python not just to get the plugin version. There are configurations to get from the django project.

Now, whether to give an option to build without requiring the django is a different discussion. We must know how often this problem is encountered. If it is a reasonable percentage, we can start playing with the different ways of implementing it.

Inshallah I'll keep this issue open for a month or two, to see if people are interested.

cpontvieux-systra commented 2 months ago

Using @kevinob11 idea, I came up with this vite.config.js file:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { djangoVitePlugin } from 'django-vite-plugin'
import { env } from 'process'
[...]

const plugins = env.NO_DJANGO ? [
  vue(),
] : [
  vue(),
  djangoVitePlugin({
    input: [
      'front/main.js',
    ],
  }),
]

export default defineConfig({
  server: {
    host: 'localhost',
    port: 1 * (env.PORT || '8080'),
    proxy,
  },
  plugins,
  [...]
})

So I just have to define NO_DJANGO variable in my CI test and it then does not require any python/django.