Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  191] [ 2]  / answers: 1 / hits: 23822  / 2 Years ago, fri, february 18, 2022, 12:00:00

By default Vite generates files in the source directory under dist.


my-app/
├─ node_modules/
├─ dist/
│ ├─ assets/
| | | index.js
| | | index.css
│ ├─ index.html
├─ index.html
├─ main.js
├─ style.scss
├─ package.json

I need to create a different folder for js and css files under assets. In other words, I need to put js and css filer under /assets/js and /assets/css folders respectively.


my-app/
├─ node_modules/
├─ dist/
│ ├─ assets/
| | |-js/
| | | index.js
| | |-css/
| | | index.css

This is my config file.


import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgrPlugin from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig({
base: "./",
plugins: [react(), svgrPlugin()],
server: {
open: true,
proxy: {
"/base": {
target: "http://localhost:19000",
changeOrigin: true,
rewrite: (path) => path.replace(/^/base/, ""),
},
},
},
});


How to do so?


More From » reactjs

 Answers
128

The output filenames are configured in Rollup with build.rollupOptions:



  1. Set output.assetFileNames to configure the asset filenames (for media files and stylesheets).



  2. Set output.chunkFileNames to configure the vendor chunk filenames.



  3. Set output.entryFileNames to configure the index.js filename.




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

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
output: {
1️⃣
assetFileNames: (assetInfo) => {
let extType = assetInfo.name.split('.').at(1);
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
extType = 'img';
}
return `assets/${extType}/[name]-[hash][extname]`;
},
2️⃣
chunkFileNames: 'assets/js/[name]-[hash].js',
3️⃣
entryFileNames: 'assets/js/[name]-[hash].js',
},
},
},
});

demo


[#50066] Friday, December 31, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;