1

I'm building a couple of apps with vue3 and vite, using some shared components. The production build process works OK but I'm having a problem with dev/debug. The Vite docs (for multi-page apps) says

"During dev, simply navigate or link to /nested/ - it works as expected, just like for a normal static file server."

but I don't know what this means - how do I link to a sub folder? I have added /src/app1 to url in launch.json, but it has no effect. I have also tried using cd src\app1 in the terminal before running npm run dev

"version": "0.2.0",
    "configurations": [
        {
          "type": "firefox",
          "request": "launch",
          "name": "vuejs: firefox  -width 300 -height 600 -P default",
          "url": "http://localhost:5173/src/app1",
          "webRoot": "${workspaceFolder}",
          "pathMappings": [
            {
              "url": "file:///C:",
              "path": "c:"
            }
          ]
        }
      ]

(This launch.json works well with a simple single-page app).

What happens with trying to debug one of the apps is that the project launches but with an empty index.html (i.e. a blank screen with no errors). I'm pretty sure the basic project structure is OK because (as I said) the build process works; I get two separate outputs of html+css+js both of which work as expected, with the correct shared components.

Also, if I tell the Vite server to automatically open the page (as I have done in my vite.config.js below) the page opens correctly - although without a debugger attached of course. So I guess that the settings in launch.json are incorrect.

The project structure is:

-src
 -app1
  -app.vue
  -index.html
  -main.js
 -app2
  -app.vue
  -index.html
  -main.js
 -assets
  ...
 -shared
  -components
   -shared.vue

If I have just one index.html, moved up a level, I can debug each app but only by editing it every time to point to a different main.js and to change the title, which seems a laborious way of approaching the task.

Below is my vite config. The extra lines in alias were added as an attempt to solve the problem but they are probably incorrect (or unneccesary)

import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'


// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    base: '/src/app1',
    open: '/src/app1/index.html',
  },
  resolve: {
    alias: {
      vue: 'vue/dist/vue.esm-bundler.js',
       '@': fileURLToPath(new URL('./src', import.meta.url)),

      app1: fileURLToPath(new URL('./src/app1', import.meta.url)),
      app2: fileURLToPath(new URL('./src/app2', import.meta.url)),
      // shared: fileURLToPath(new URL('./src/shared/components', import.meta.url)),
    }
  },
  build: {
    rollupOptions: {
      input: {
        app1: resolve(__dirname, './src/app1/index.html'),
        app2: resolve(__dirname, './src/app2/index.html'),
      },
    },
  },
})
quilkin
  • 874
  • 11
  • 31

1 Answers1

1

I've made things more complex than neccesary because I missed the important setting. In vite.config.js, it's important to define root to point to where each index.html is found. So for my structure above, the config file looks like

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

export default defineConfig({
  plugins: [vue() ],
  root: "src\app1",
  resolve: {
    alias: {
      vue: 'vue/dist/vue.esm-bundler.js',
    }
  }
})

In the process I've also swapped from Firefox to Chrome for debug (I was getting a lot of irrelevant error messages from Firefox), and my launch.json is now simply

{
    "version": "0.2.0",
    "configurations": [
       {
          "type": "chrome",
          "request": "launch",
          "name": "Launch Chrome against localhost",
          "url": "http://localhost:5173",
          "webRoot": "${workspaceFolder}",
        }
      ]
}

It doesn't really matter what the file structure looks like, as long as within each file its dependencies are correctly addressed. The only important bit is the root. Simply by changing that from 'app1' to 'app2' will switch both the debug and the build to the correct folders, with the release (built) files being stored in subfolders of 'app1' and 'app2' independently.

It would be easy to extend this to have more than 2 apps each sharing the same common components.

quilkin
  • 874
  • 11
  • 31