7

I'm making a static multipage website with ViteJS (html, scss and JS) and I can't find the way to change the build path of html files to put them into the root of dist folder.

My project structure is:

├── dist
    └──...
    └──src
       └── pages
             └── some-page.html
    └──...
├── node_modules
├── src
    └── pages
        └── some-page.html
├── .gitignore
├── index.html
├── package-lock.json
├── package.json
└── vite.config.js

and I want:

├── dist
    └── ...
    └── some-page.html
    └── ...
├── node_modules
├── src
    └── pages
        └── some-page.html
├── .gitignore
├── index.html
├── package-lock.json
├── package.json
└── vite.config.js

my vite.config.js (as the documentation recommends) is:

const { resolve } = require('path')
const { defineConfig } = require('vite')

module.exports = defineConfig({
    build: {
        rollupOptions: {
            input: {
                main: resolve(__dirname, 'index.html'),
                multiminerales: resolve(__dirname, 'src/pages/some-page.html')
            }
        }
    }
})

So, I think I need to change the input object but I can't find any information about it, I know about public directory but it will break all my folders structure. What can I do?

  • Relevant, I think: [Changing the input and output directory for multipage site - vitejs/vite GitHub](https://github.com/vitejs/vite/discussions/2771) – Darryl Noakes Jan 27 '23 at 03:57
  • [This answer to "Vite - change ouput directory of assets"](https://stackoverflow.com/a/71190586/15261914) seems to indicate that you can change the entire path and filename of built files as you like, so that could be worth looking into. I could try it out and write up an answer? – Darryl Noakes Jan 27 '23 at 04:03
  • I have tried, but I can't find how to make Vite actually use Rollup to bundle anything in a vanilla project. I may look into it more sometime if I get a chance. – Darryl Noakes Jan 27 '23 at 17:21

2 Answers2

1

Yeah, I faced such problem, too.

For me it works to move index.html to src/pages

Then in vite.config.js specify root: './src/pages'

So, your config may look like:

const { resolve } = require('path')
const { defineConfig } = require('vite')

module.exports = defineConfig({
    root: './src/pages',
    build: {
        rollupOptions: {
            input: {
                main: resolve(__dirname, 'src/pages/index.html'),
                multiminerales: resolve(__dirname, 'src/pages/some-page.html')
            }
        }
    }
})
KyleMit
  • 30,350
  • 66
  • 462
  • 664
toddscottik
  • 510
  • 2
  • 3
-1

You can use this configuration -

import { fileURLToPath, URL } from 'node:url'

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

    export default defineConfig({
      root: resolve(path, 'templates'),
      base: 'https://aaronchristian.test/wp-content/themes/portfolio/',
      plugins: [vue(), vueJsx()],
      resolve: {
        alias: {
          '@': fileURLToPath(new URL('./src', import.meta.url))
        }
      },
      build: {
        rollupOptions: {
          input: {
            index: fileURLToPath(new URL('./src/main.js', import.meta.url)),
            'index.html': fileURLToPath(new URL('./templates/index.html', import.meta.url))
          },
          output: {
            dir: 'dist',
            entryFileNames: 'assets/[name].js',
            assetFileNames: 'assets/[name].[extname]',
            chunkFileNames: 'assets/[name].js'
          }
        }
      }
    })
Captain Hat
  • 2,444
  • 1
  • 14
  • 31
  • Having that code as a snippet makes no sense, as it cannot be run on its own. Please edit it to be just a plain code block (with the `javascript` language identifier). – Darryl Noakes Mar 24 '23 at 23:04