vite 里面怎么使用sass和less
时间: 2024-06-12 08:06:07 浏览: 158
Vite 支持使用 Sass 和 Less 预处理器,你可以在 Vite 配置文件中添加相应的插件来实现。
使用 Sass:
1. 安装 sass 插件
```bash
npm install -D sass
```
2. 在 Vite 配置文件中添加插件
```javascript
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import sass from 'sass'
export default defineConfig({
plugins: [
vue(),
{
apply: 'build',
name: 'sass',
async load(id) {
if (id.endsWith('.scss')) {
const { css } = await sass.render({ file: id })
return css
}
},
},
],
})
```
3. 在组件中引入 .scss 文件
```html
<template>
<div class="app">
<h1>{{ msg }}</h1>
</div>
</template>
<style lang="scss">
.app {
color: red;
}
</style>
```
使用 Less:
1. 安装 less 插件
```bash
npm install -D less
```
2. 在 Vite 配置文件中添加插件
```javascript
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import less from 'less'
export default defineConfig({
plugins: [
vue(),
{
apply: 'build',
name: 'less',
async load(id) {
if (id.endsWith('.less')) {
const { css } = await less.render(await fs.readFile(id, 'utf8'))
return css
}
},
},
],
})
```
3. 在组件中引入 .less 文件
```html
<template>
<div class="app">
<h1>{{ msg }}</h1>
</div>
</template>
<style lang="less">
.app {
color: blue;
}
</style>
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)