Home > Software design >  How to import file as text into vite.config.js?
How to import file as text into vite.config.js?

Time:08-14

I have simple scss file in my project directory and I wanna get it's content on the compiling stage so I could transform in on vite.config.js but how can I get it's content? I mean I'm able to get content using

import test from "./src/extensions/sites/noscript/test.css";
console.log(test);

in App.vue but that doesn't work in vite.config.js (that works with webpack) Is there any ways to get file content? test == {} when I'm debugging... vite.config.js and works well in App.vue

CodePudding user response:

vite.config.js is run as a Node script, so you could use fs.readFileSync() to read the file from disk into a string:

// vite.config.js
import fs from 'fs'

const styleRaw = fs.readFileSync('./src/extensions/sites/noscript/test.css', 'utf-8')
console.log(styleRaw)

demo

  • Related