简体   繁体   English

如何将文件作为文本导入 vite.config.js?

[英]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?我的项目目录中有简单的 scss 文件,我想在编译阶段获取它的内容,这样我就可以在 vite.config.js 上进行转换,但我怎样才能得到它的内容? 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?在 App.vue 中,但在 vite.config.js 中不起作用(与 webpack 一起使用)有没有办法获取文件内容? test == {} when I'm debugging... vite.config.js and works well in App.vue test == {} 当我调试时... vite.config.js 并且在 App.vue 中运行良好

vite.config.js is run as a Node script, so you could usefs.readFileSync() to read the file from disk into a string: vite.config.js作为 Node 脚本运行,因此您可以使用fs.readFileSync()将文件从磁盘读取到字符串中:

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

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

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM