An open API service indexing awesome lists of open source software.

https://github.com/peterroe/vite-plugin-vue-style-in-template

Extract the style tags from the Vue component into basic style tags
https://github.com/peterroe/vite-plugin-vue-style-in-template

vite-plugin vue3

Last synced: 3 months ago
JSON representation

Extract the style tags from the Vue component into basic style tags

Awesome Lists containing this project

README

          

## Vite Plugin Vue Style In Template

Extract the style tags from the Vue component into basic style tags.

## Usage

```bash
pnpm install vite-plugin-vue-style-in-template
```

```ts
import vueStyleInTemplate from 'vite-plugin-vue-style-in-template'

export default defineConfig({
plugins: [vueStyleInTemplate()],
})
```

In [vitepress](https://vitepress.dev/)

```ts
import vueStyleInTemplate from 'vite-plugin-vue-style-in-template'

export default defineConfig({
plugins: [
vueStyleInTemplate({
include: [/\.vue$/, /\.md$/],
}),
],
})
```

## Example

`` tags within the template will be extracted into corresponding `<style>` tags based on whether they contain the `scoped` attribute. If the original file does not have `<style>` tags, new `<style>` tags will be inserted.

### Basic Usage

transform:

```vue
<template>
<div>
<h1 class="main-title">Hello World</h1>
<style scoped>
.main-title {
color: red;
}

Some text



.text {
color: blue;
font-size: 16px;
}

.body {
background-color: #f0f0f0;
}

```

to:

```vue


Hello World


Some text


.body {
background-color: #f0f0f0;
}
.text {
color: blue;
font-size: 16px;
}

.main-title {
color: red;
}

```

### With `slot`

You can use `` tags within the `<slot>` tags to style the content of the slot.

`comp.vue`:

```vue
<template>
<div>
<div>
<slot name="header" />
</div>
<div>
<slot name="content" />
</div>
</div>
</template>
```

`app.vue`:

```vue
<script setup lang="ts">
import Comp from './comp.vue'
</script>
<template>
<Comp>
<template #header>
<div class="header">Header</div>
<style scoped>
.header {
color: red;
}



Content


.content {
color: blue;
}


```