最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import file as text into vite.config.js? - Stack Overflow

programmeradmin3浏览0评论

I have simple scss file in my project directory and I wanna get it's content on the piling 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

I have simple scss file in my project directory and I wanna get it's content on the piling 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

Share Improve this question edited Aug 14, 2022 at 0:26 tony19 139k23 gold badges277 silver badges347 bronze badges asked Aug 14, 2022 at 0:20 GorilkaGorilka 5146 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论