Gorilka
Gorilka

Reputation: 504

How to import file as text into vite.config.js?

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

Upvotes: 3

Views: 8260

Answers (1)

tony19
tony19

Reputation: 138326

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

Upvotes: 6

Related Questions