13

I built a multi-page app with Vue CLI and Vue 2 by changing vue.config.js like below:

pages: {
    index: {
      entry: './src/pages/index/main.js',
      template: 'public/index.html',
      title: 'index page',
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    admin: {
      entry: './src/pages/admin/main.js',
      template: 'public/index.html',
      title: 'admin page',
      chunks: ['chunk-vendors', 'chunk-common', 'admin']
    }
},
...

But how do I build a multi-page app with Vite and Vue 3?

enter image description here

This is my Directory Structure. I edited the vite.config.js like this:

import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')
/**
 * @type {import('vite').UserConfig}
 */
export default {
  plugins: [vue()],
  build:{
    rollupOptions:{
      input:{
        main:resolve(__dirname,'index.html'),
        admin:resolve(__dirname,'src/admin/index.html')
      }
    }
  }
}

But it returns errors when i build and i counld not open the admin page by localhost:3000/admin/index.html.

tony19
  • 125,647
  • 18
  • 229
  • 307
radiorz
  • 1,459
  • 4
  • 18
  • 36

1 Answers1

16

You could create two separate index.html for each entry point. For example, <projectRoot>/index.html imports <projectRoot>/main.js, while a nested <projectRoot>/admin/index.html imports <projectRoot>/admin/main.js.

Consider the following directory structure:

|-package.json
|-vite.config.js
|-index.html
|-main.js
|-admin/
|---index.html
|---main.js

You'd use this config to create the multi-page app:

// vite.config.js
const { resolve } = require('path')

module.exports = {
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        admin: resolve(__dirname, 'admin/index.html')
      }
    }
  }
}
tony19
  • 125,647
  • 18
  • 229
  • 307
  • This works very well, but do you know how to specify the chunk names? If the nested page shares any code with the main can you get 2 modules/chunks generated ie index.js and admin.js which both may use common.js? I want to know if you can either duplicate common.js or set a predefined name? Thanks. – Jonathan Counihan Mar 16 '23 at 05:10