Skip to content

Commit 89bd757

Browse files
committed
chore(deps): upgrade
1 parent 82cd13f commit 89bd757

6 files changed

Lines changed: 296 additions & 215 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@
216216
"prettier": "^3.6.2",
217217
"promisify-loader-runner": "^1.0.1",
218218
"rimraf": "^6.0.1",
219-
"sass": "^1.90.0",
220-
"sass-embedded": "^1.90.0",
219+
"sass": "^1.91.0",
220+
"sass-embedded": "^1.91.0",
221221
"sass-true": "^9.0.0",
222222
"semver": "7.7.2",
223223
"set-value": "^4.1.0",
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`plugins > enforce 1`] = `".red { color: red; .blue { color: blue } }"`;
3+
exports[`plugins > enforce append rule case 1`] = `".red { color: red; .blue { color: blue } }"`;
4+
5+
exports[`plugins > enforce default 1`] = `".red { color: red; }"`;
6+
7+
exports[`plugins > enforce remove rule case 1`] = `""`;

packages/postcss/test/plugins/index.test.ts

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import postcss from 'postcss'
2-
2+
import { createPlugins } from './utils'
33
// 不动态添加
44
// 1 Once
55
// 2 Once
@@ -41,31 +41,23 @@ import postcss from 'postcss'
4141
// 1 OnceExit
4242
// 2 OnceExit
4343
describe('plugins', () => {
44-
const weakMap = new WeakMap()
45-
it('enforce', async () => {
44+
it('enforce default', async () => {
4645
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-
},
46+
createPlugins([
47+
'1',
48+
'2',
49+
]),
50+
).process('.red { color: red; }', {
51+
from: undefined,
52+
})
53+
expect(css).toMatchSnapshot()
54+
})
55+
56+
it('enforce append rule case', async () => {
57+
const weakMap = new WeakMap()
58+
const { css } = await postcss(
59+
createPlugins([
60+
'1',
6961
{
7062
postcssPlugin: '2',
7163
Rule(node, { rule, decl }) {
@@ -75,24 +67,33 @@ describe('plugins', () => {
7567
weakMap.set(rule, true)
7668
}
7769
},
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')
70+
},
71+
]),
72+
).process('.red { color: red; }', {
73+
from: undefined,
74+
})
75+
expect(css).toMatchSnapshot()
76+
})
77+
78+
it('enforce remove rule case', async () => {
79+
const { css } = await postcss(
80+
createPlugins([
81+
{
82+
postcssPlugin: '1',
83+
Rule(node) {
84+
console.log('1 Rule')
85+
node.remove()
8986
},
90-
OnceExit(_root) {
91-
console.log('2 OnceExit')
92-
// root.append(rule({ selector: '.blue', nodes: [decl({ prop: 'color', value: 'blue' })] }))
87+
},
88+
{
89+
postcssPlugin: '2',
90+
Rule(node) {
91+
// 不会命中
92+
console.log('2 Rule -----------')
93+
node.remove()
9394
},
9495
},
95-
],
96+
]),
9697
).process('.red { color: red; }', {
9798
from: undefined,
9899
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable ts/no-unused-vars */
2+
import type { AcceptedPlugin, Plugin } from 'postcss'
3+
import { defu } from '@weapp-tailwindcss/shared'
4+
5+
function isPostcssPlugin(postcssPlugin: string | Plugin): postcssPlugin is Plugin {
6+
return typeof postcssPlugin !== 'string'
7+
}
8+
9+
function createMixin(name: string): Partial<Plugin> {
10+
return {
11+
Once() {
12+
console.log(`${name} Once`)
13+
},
14+
OnceExit() {
15+
console.log(`${name} OnceExit`)
16+
},
17+
Root() {
18+
console.log(`${name} Root`)
19+
},
20+
RootExit() {
21+
console.log(`${name} RootExit`)
22+
},
23+
Rule() {
24+
console.log(`${name} Rule`)
25+
},
26+
RuleExit() {
27+
console.log(`${name} RuleExit`)
28+
},
29+
AtRule(atRule, helper) {
30+
console.log(`${name} AtRule`)
31+
},
32+
AtRuleExit(atRule, helper) {
33+
console.log(`${name} AtRuleExit`)
34+
},
35+
Comment(comment, helper) {
36+
console.log(`${name} Comment`)
37+
},
38+
CommentExit(comment, helper) {
39+
console.log(`${name} CommentExit`)
40+
},
41+
Declaration(decl, helper) {
42+
console.log(`${name} Declaration`)
43+
},
44+
DeclarationExit(decl, helper) {
45+
console.log(`${name} DeclarationExit`)
46+
},
47+
Document(document, helper) {
48+
console.log(`${name} Document`)
49+
},
50+
DocumentExit(document, helper) {
51+
console.log(`${name} DocumentExit`)
52+
},
53+
}
54+
}
55+
56+
export function createPlugin(postcssPlugin: string | Plugin): Plugin {
57+
const isPlugin = isPostcssPlugin(postcssPlugin)
58+
let plugin: Plugin
59+
if (isPlugin) {
60+
plugin = defu<Plugin, Plugin[]>(postcssPlugin, {
61+
postcssPlugin: postcssPlugin.postcssPlugin,
62+
...createMixin(postcssPlugin.postcssPlugin),
63+
})
64+
}
65+
else {
66+
plugin = defu<Plugin, Plugin[]>({
67+
postcssPlugin,
68+
...createMixin(postcssPlugin),
69+
})
70+
}
71+
return plugin as Plugin
72+
}
73+
74+
export function createPlugins(postcssPlugins: (string | Plugin)[]): AcceptedPlugin[] {
75+
return postcssPlugins.map(createPlugin)
76+
}

0 commit comments

Comments
 (0)