Skip to content

Commit b9d3dfc

Browse files
committed
chore: add postcss enforce
1 parent e234649 commit b9d3dfc

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`plugins > enforce 1`] = `".red { color: red; .blue { color: blue } }"`;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import postcss from 'postcss'
2+
3+
// 不动态添加
4+
// 1 Once
5+
// 2 Once
6+
// 1 Root
7+
// 2 Root
8+
// 1 Rule
9+
// 2 Rule
10+
// 1 RuleExit
11+
// 2 RuleExit
12+
// 1 RootExit
13+
// 2 RootExit
14+
// 1 OnceExit
15+
// 2 OnceExit
16+
17+
// 动态添加
18+
19+
// 1 Once
20+
// 2 Once
21+
// 1 Root
22+
// 2 Root
23+
// 1 Rule
24+
// 2 Rule
25+
// 在这里因为添加了所以重新执行了 Rule
26+
// 1 Rule
27+
// 2 Rule
28+
// 1 RuleExit
29+
// 2 RuleExit
30+
// 同样重新执行了 RuleExit
31+
// 1 RuleExit
32+
// 2 RuleExit
33+
// 1 RootExit
34+
// 2 RootExit
35+
// 重新执行 Root
36+
// 1 Root
37+
// 2 Root
38+
// 1 RootExit
39+
// 2 RootExit
40+
// end
41+
// 1 OnceExit
42+
// 2 OnceExit
43+
describe('plugins', () => {
44+
const weakMap = new WeakMap()
45+
it('enforce', async () => {
46+
const { css } = await postcss(
47+
[
48+
{
49+
postcssPlugin: '1',
50+
Once() {
51+
console.log('1 Once')
52+
},
53+
OnceExit() {
54+
console.log('1 OnceExit')
55+
},
56+
Root() {
57+
console.log('1 Root')
58+
},
59+
RootExit() {
60+
console.log('1 RootExit')
61+
},
62+
Rule() {
63+
console.log('1 Rule')
64+
},
65+
RuleExit() {
66+
console.log('1 RuleExit')
67+
},
68+
},
69+
{
70+
postcssPlugin: '2',
71+
Rule(node, { rule, decl }) {
72+
console.log('2 Rule')
73+
if (!weakMap.get(rule)) {
74+
node.append(rule({ selector: '.blue', nodes: [decl({ prop: 'color', value: 'blue' })] }))
75+
weakMap.set(rule, true)
76+
}
77+
},
78+
RuleExit() {
79+
console.log('2 RuleExit')
80+
},
81+
Root() {
82+
console.log('2 Root')
83+
},
84+
RootExit() {
85+
console.log('2 RootExit')
86+
},
87+
Once() {
88+
console.log('2 Once')
89+
},
90+
OnceExit(_root) {
91+
console.log('2 OnceExit')
92+
// root.append(rule({ selector: '.blue', nodes: [decl({ prop: 'color', value: 'blue' })] }))
93+
},
94+
},
95+
],
96+
).process('.red { color: red; }', {
97+
from: undefined,
98+
})
99+
expect(css).toMatchSnapshot()
100+
})
101+
})

0 commit comments

Comments
 (0)