Skip to content

Commit ac657cf

Browse files
committed
feat(plugin): supports custom plugin(#33)
1 parent de026c8 commit ac657cf

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

packages/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styled from './src/styled'
33
export * from './src/providers/theme'
44
export * from './src/helper'
55
export * from './src/hooks'
6+
export * from './src/plugins'
67

78
export * from './src/styled'
89

packages/core/src/plugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './plugin'

packages/core/src/plugins/plugin.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Element } from 'stylis'
2+
3+
type beforeBuildCallback = (element: Element, index: number, children: Element[]) => string | void
4+
type afterBuildCallback = (compiledCss: string) => string | void
5+
6+
interface PluginHooks {
7+
beforeBuild?: beforeBuildCallback | beforeBuildCallback[]
8+
afterBuild?: afterBuildCallback | afterBuildCallback[]
9+
}
10+
11+
class Plugin {
12+
private beforeBuildHooks: beforeBuildCallback[] = []
13+
private afterBuildHooks: afterBuildCallback[] = []
14+
15+
register(plugin: PluginHooks) {
16+
if (plugin.beforeBuild) {
17+
if (Array.isArray(plugin.beforeBuild)) {
18+
this.beforeBuildHooks.push(...plugin.beforeBuild)
19+
} else {
20+
this.beforeBuildHooks.push(plugin.beforeBuild)
21+
}
22+
}
23+
if (plugin.afterBuild) {
24+
if (Array.isArray(plugin.afterBuild)) {
25+
this.afterBuildHooks.push(...plugin.afterBuild)
26+
} else {
27+
this.afterBuildHooks.push(plugin.afterBuild)
28+
}
29+
}
30+
}
31+
32+
runBeforeBuild(element: Element, index: number, children: Element[]) {
33+
for (const hook of this.beforeBuildHooks) {
34+
hook(element, index, children)
35+
}
36+
}
37+
38+
runAfterBuild(compiledCss: string) {
39+
for (const hook of this.afterBuildHooks) {
40+
const result = hook(compiledCss)
41+
if (result) {
42+
compiledCss = result
43+
}
44+
}
45+
return compiledCss
46+
}
47+
}
48+
49+
export type { Element }
50+
export const plugin = new Plugin()

packages/core/src/utils/styleManagement.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { applyExpressions, ExpressionType } from '@/src/utils/index'
2-
import { compile, middleware, prefixer, serialize, stringify } from 'stylis'
2+
import { compile, Element, middleware, prefixer, serialize, stringify } from 'stylis'
3+
import { plugin } from '../plugins'
34

45
const MAX_SIZE = 65536
56

@@ -54,7 +55,17 @@ export function injectStyle<T>(className: string, cssWithExpression: ExpressionT
5455
if (className !== '') {
5556
cssString = `.${className}{${appliedCss}}`
5657
}
57-
const compiledCss = serialize(compile(cssString), middleware([prefixer, stringify]))
58+
let compiledCss = serialize(
59+
compile(cssString),
60+
middleware([
61+
(element: Element, index: number, children: Element[]) => {
62+
return plugin.runBeforeBuild(element, index, children)
63+
},
64+
prefixer,
65+
stringify,
66+
]),
67+
)
68+
compiledCss = plugin.runAfterBuild(compiledCss)
5869
insert(className, compiledCss)
5970

6071
return tailwindClasses

0 commit comments

Comments
 (0)