Skip to content

feat: add extractStyle #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ const handleMessage = () => {
}
</script>
<template>
<a-button @click="handleMessage">
button
</a-button>
<a-extract-style>
<a-button @click="handleMessage">
button
</a-button>
</a-extract-style>
</template>
```
Reference [Nuxt documentation](https://nuxt.com/docs/guide/directory-structure/components) and [playground](./playground/app.vue) use.
Expand Down Expand Up @@ -99,6 +101,22 @@ If you wish to add automatically import content from Ant Design Vue, you can add
If there are components that are not imported automatically from @ant-design/icons-vue, you need to add the component name here.


### extractStyle

* Type: `boolean`

Extract css on demand, start by default

```vue
<!-- If launched, we can wrap the component or page in the app.vue outermost layer -->
<template>
<a-extract-style>
<!-- Your page or component -->
</a-extract-style>
</template>
```


## Development

```bash
Expand Down
144 changes: 73 additions & 71 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,81 +1,83 @@
<template>
<a-config-provider :theme="theme">
<div class="container">
<div>
<a-extract-style>
<a-config-provider :theme="theme">
<div class="container">
<div>
<a-space>
<a-button @click="changeTheme('dark')">
dark
</a-button>
<a-button @click="changeTheme('light')">
light
</a-button>
</a-space>
</div>
<a-alert
message="Success Text"
type="success"
/>
<div>
icon:
<AlertFilled />
<LoadingOutlined />
</div>
<a-table v-bind="tableProps" />
<a-space>
<a-button @click="changeTheme('dark')">
dark
<a-button
type="primary"
@click="handleMessage('success')"
>
message success
</a-button>
<a-button @click="changeTheme('light')">
light
<a-button @click="handleMessage('info')">
message info
</a-button>
</a-space>
</div>
<a-alert
message="Success Text"
type="success"
/>
<div>
icon:
<AlertFilled />
<LoadingOutlined />
</div>
<a-table v-bind="tableProps" />
<a-space>
<a-button
type="primary"
@click="handleMessage('success')"
>
message success
</a-button>
<a-button @click="handleMessage('info')">
message info
</a-button>
</a-space>
<a-space>
<a-button
type="primary"
@click="handleModal('success')"
>
modal success
</a-button>
<a-button @click="handleModal('info')">
modal info
</a-button>
</a-space>
<a-space>
<a-button
type="primary"
@click="handleNotification('success')"
<a-space>
<a-button
type="primary"
@click="handleModal('success')"
>
modal success
</a-button>
<a-button @click="handleModal('info')">
modal info
</a-button>
</a-space>
<a-space>
<a-button
type="primary"
@click="handleNotification('success')"
>
notification success
</a-button>
<a-button @click="handleNotification('info')">
notification info
</a-button>
</a-space>
<a-flex
gap="middle"
vertical
>
notification success
</a-button>
<a-button @click="handleNotification('info')">
notification info
</a-button>
</a-space>
<a-flex
gap="middle"
vertical
>
<a-radio-group v-model:value="value">
<a-radio value="horizontal">
horizontal
</a-radio>
<a-radio value="vertical">
vertical
</a-radio>
</a-radio-group>
<a-flex :vertical="value === 'vertical'">
<div
v-for="(item, index) in new Array(4)"
:key="item"
:style="{ ...baseStyle, background: `${index % 2 ? '#1677ff' : '#1677ffbf'}` }"
/>
<a-radio-group v-model:value="value">
<a-radio value="horizontal">
horizontal
</a-radio>
<a-radio value="vertical">
vertical
</a-radio>
</a-radio-group>
<a-flex :vertical="value === 'vertical'">
<div
v-for="(item, index) in new Array(4)"
:key="item"
:style="{ ...baseStyle, background: `${index % 2 ? '#1677ff' : '#1677ffbf'}` }"
/>
</a-flex>
</a-flex>
</a-flex>
</div>
</a-config-provider>
</div>
</a-config-provider>
</a-extract-style>
</template>

<script setup lang="ts">
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export { allComponents } from "./antdv"
export const defaults:Options = {
components: allComponents,
icons: allIcons,
imports: allImports
imports: allImports,
extractStyle: true
}
26 changes: 25 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNuxtModule } from '@nuxt/kit'
import { addComponent, createResolver, defineNuxtModule, useNuxt } from '@nuxt/kit'
import { libraryName,defaults } from "./config"
import { resolveComponents,resolveImports } from "./core"
import type { Options } from './types'
Expand All @@ -18,9 +18,33 @@ export default defineNuxtModule<Partial<Options>>({
nuxt.options.imports.autoImport !== false && resolveImports(options)
nuxt.options.components !== false && resolveComponents(options)

if (options.extractStyle) {
extractStyle()
}

// const resolver = createResolver(import.meta.url)
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
// addPlugin(resolver.resolve('./runtime/plugin'))
}
})

function extractStyle() {
const nuxt = useNuxt()
// The spa does not need to be injected with css.
if (!nuxt.options.ssr) {
return;
}
// When generating, replace process.env.NODE_ENV to production (defaults to prerender).
// And antd relies on process.env.NODE_ENV when generating css prefixes.
if (nuxt.options.dev === false && nuxt.options.nitro.static) {
nuxt.options.nitro.replace ??= {};
nuxt.options.nitro.replace["process.env.NODE_ENV"] = "'production'";
}

// Adding auxiliary components
const resolver = createResolver(import.meta.url)
addComponent({
name: 'AExtractStyle',
filePath: resolver.resolve('./runtime/components/AExtractStyle.vue')
})
}
21 changes: 21 additions & 0 deletions src/runtime/components/AExtractStyle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts" setup>
import { createCache, extractStyle, StyleProvider } from "ant-design-vue"

const cache = createCache()

const app = useNuxtApp()

// Real-time style extraction at ssr
// Pre-rendered extraction styles for ssg
app.hook('app:rendered', () => {
useHead({
style: [extractStyle(cache, true)]
})
})
</script>

<template>
<StyleProvider :cache="cache">
<slot />
</StyleProvider>
</template>
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Options {
icons: false | PresetImport[]
components: false | PresetImport[]
imports: PresetImport[]
extractStyle: boolean
}


Expand Down