@@ -3,7 +3,9 @@ import { generate, parse, traverse } from '@/babel'
3
3
// crash code
4
4
5
5
export function inspectProcessTailwindFeaturesReturnContext ( content : string ) {
6
- const ast = parse ( content )
6
+ const ast = parse ( content , {
7
+ sourceType : 'unambiguous' ,
8
+ } )
7
9
let hasPatched = false
8
10
traverse ( ast , {
9
11
FunctionDeclaration ( p ) {
@@ -39,20 +41,17 @@ export function inspectPostcssPlugin(content: string) {
39
41
traverse ( ast , {
40
42
Program ( p ) {
41
43
const n = p . node
42
- // find module.exports = function tailwindcss (configOrPath)
44
+ // find function _default (configOrPath = {}) {
43
45
const idx = n . body . findIndex ( ( x ) => {
44
46
return (
45
- t . isExpressionStatement ( x )
46
- && t . isAssignmentExpression ( x . expression )
47
- && t . isMemberExpression ( x . expression . left )
48
- && t . isFunctionExpression ( x . expression . right )
49
- && x . expression . right . id ?. name === 'tailwindcss'
47
+ t . isFunctionDeclaration ( x )
48
+ && x . id ?. name === '_default'
50
49
)
51
50
} )
52
51
53
52
if ( idx > - 1 ) {
54
53
const prevStatement = n . body [ idx - 1 ]
55
- const lastStatement = n . body [ n . body . length - 1 ]
54
+ const lastStatement = n . body [ idx - 2 ]
56
55
const hasPatchedCondition0
57
56
= prevStatement
58
57
&& t . isVariableDeclaration ( prevStatement )
@@ -70,108 +69,87 @@ export function inspectPostcssPlugin(content: string) {
70
69
// const contextRef = {
71
70
// value: []
72
71
// };
73
- const statement = t . variableDeclaration ( 'const ' , [
72
+ const statement = t . variableDeclaration ( 'var ' , [
74
73
t . variableDeclarator ( t . identifier ( variableName ) , t . objectExpression ( [ t . objectProperty ( t . identifier ( valueKey ) , t . arrayExpression ( ) ) ] ) ) ,
75
74
] )
76
- n . body . splice ( idx , 0 , statement )
77
- // module.exports.contextRef = contextRef;
78
- n . body . push (
75
+ n . body . splice ( idx , 0 , statement ,
76
+ // exports.contextRef = contextRef;
79
77
t . expressionStatement (
80
78
t . assignmentExpression (
81
79
'=' ,
82
- t . memberExpression ( t . memberExpression ( t . identifier ( 'module' ) , t . identifier ( ' exports') ) , t . identifier ( exportKey ) ) ,
80
+ t . memberExpression ( t . identifier ( 'exports' ) , t . identifier ( exportKey ) ) ,
83
81
t . identifier ( variableName ) ,
84
82
) ,
85
- ) ,
86
- )
83
+ ) )
87
84
}
88
85
}
89
86
} ,
90
- FunctionExpression ( p ) {
87
+ FunctionDeclaration ( p ) {
91
88
if ( hasPatched ) {
92
89
return
93
90
}
94
91
const n = p . node
95
- if ( n . id ?. name === 'tailwindcss ' && n . body . body . length === 1 && t . isReturnStatement ( n . body . body [ 0 ] ) ) {
92
+ if ( n . id ?. name === '_default ' && n . body . body . length === 1 && t . isReturnStatement ( n . body . body [ 0 ] ) ) {
96
93
const returnStatement = n . body . body [ 0 ]
97
- if ( t . isObjectExpression ( returnStatement . argument ) && returnStatement . argument . properties . length === 2 ) {
98
- const properties = returnStatement . argument . properties
99
- if ( t . isObjectProperty ( properties [ 0 ] ) && t . isObjectProperty ( properties [ 1 ] ) ) {
100
- const keyMatched = t . isIdentifier ( properties [ 0 ] . key ) && properties [ 0 ] . key . name === 'postcssPlugin'
101
- const pluginsMatched = t . isIdentifier ( properties [ 1 ] . key ) && properties [ 1 ] . key . name === 'plugins'
102
- if (
103
- pluginsMatched
104
- && keyMatched
105
- && t . isCallExpression ( properties [ 1 ] . value )
106
- && t . isMemberExpression ( properties [ 1 ] . value . callee )
107
- && t . isArrayExpression ( properties [ 1 ] . value . callee . object )
108
- ) {
109
- const pluginsCode = properties [ 1 ] . value . callee . object . elements
110
- if ( pluginsCode [ 1 ] && t . isFunctionExpression ( pluginsCode [ 1 ] ) ) {
111
- const targetBlockStatement = pluginsCode [ 1 ] . body
94
+ if ( t . isCallExpression ( returnStatement . argument ) && t . isMemberExpression ( returnStatement . argument . callee ) && t . isArrayExpression ( returnStatement . argument . callee . object ) ) {
95
+ const targetFn = returnStatement . argument . callee . object . elements [ 1 ]
96
+ if ( t . isFunctionExpression ( targetFn ) ) {
97
+ // 函数体
98
+ const targetBlockStatement = targetFn . body
112
99
113
- const lastStatement = targetBlockStatement . body [ targetBlockStatement . body . length - 1 ]
114
- if ( t . isExpressionStatement ( lastStatement ) ) {
115
- // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root, result));
116
- const newExpressionStatement = t . expressionStatement (
117
- t . callExpression (
118
- t . memberExpression (
119
- t . memberExpression ( t . identifier ( variableName ) , t . identifier ( 'value' ) ) ,
100
+ const lastStatement = targetBlockStatement . body [ targetBlockStatement . body . length - 1 ]
101
+ if ( t . isExpressionStatement ( lastStatement ) ) {
102
+ // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root, result));
103
+ const newExpressionStatement = t . expressionStatement (
104
+ t . callExpression (
105
+ t . memberExpression (
106
+ t . memberExpression ( t . identifier ( variableName ) , t . identifier ( 'value' ) ) ,
120
107
121
- t . identifier ( 'push' ) ,
122
- ) ,
108
+ t . identifier ( 'push' ) ,
109
+ ) ,
123
110
124
- [ lastStatement . expression ] ,
125
- ) ,
126
- )
127
- targetBlockStatement . body [ targetBlockStatement . body . length - 1 ] = newExpressionStatement
128
- }
111
+ [ lastStatement . expression ] ,
112
+ ) ,
113
+ )
114
+ targetBlockStatement . body [ targetBlockStatement . body . length - 1 ] = newExpressionStatement
115
+ }
129
116
130
- const ifIdx = targetBlockStatement . body . findIndex ( x => t . isIfStatement ( x ) )
131
- if ( ifIdx > - 1 ) {
132
- const ifRoot = < t . IfStatement > targetBlockStatement . body [ ifIdx ]
133
- if ( t . isBlockStatement ( ifRoot . consequent ) && ifRoot . consequent . body [ 1 ] && t . isForOfStatement ( ifRoot . consequent . body [ 1 ] ) ) {
134
- const forOf : t . ForOfStatement = ifRoot . consequent . body [ 1 ]
135
- if ( t . isBlockStatement ( forOf . body ) && forOf . body . body . length === 1 && t . isIfStatement ( forOf . body . body [ 0 ] ) ) {
136
- const if2 : t . IfStatement = forOf . body . body [ 0 ]
137
- if ( t . isBlockStatement ( if2 . consequent ) && if2 . consequent . body . length === 1 && t . isExpressionStatement ( if2 . consequent . body [ 0 ] ) ) {
138
- const target = if2 . consequent . body [ 0 ]
139
- // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root1, result));
140
- const newExpressionStatement = t . expressionStatement (
141
- t . callExpression ( t . memberExpression ( t . memberExpression ( t . identifier ( variableName ) , t . identifier ( 'value' ) ) , t . identifier ( 'push' ) ) , [ target . expression ] ) ,
142
- )
143
- if2 . consequent . body [ 0 ] = newExpressionStatement
144
- }
145
- }
117
+ const ifIdx = targetBlockStatement . body . findIndex ( x => t . isIfStatement ( x ) )
118
+ if ( ifIdx > - 1 ) {
119
+ const ifRoot = < t . IfStatement > targetBlockStatement . body [ ifIdx ]
120
+ if ( t . isBlockStatement ( ifRoot . consequent ) && ifRoot . consequent . body [ 1 ] && t . isForOfStatement ( ifRoot . consequent . body [ 1 ] ) ) {
121
+ const forOf : t . ForOfStatement = ifRoot . consequent . body [ 1 ]
122
+ if ( t . isBlockStatement ( forOf . body ) && forOf . body . body . length === 1 && t . isIfStatement ( forOf . body . body [ 0 ] ) ) {
123
+ const if2 : t . IfStatement = forOf . body . body [ 0 ]
124
+ if ( t . isBlockStatement ( if2 . consequent ) && if2 . consequent . body . length === 1 && t . isExpressionStatement ( if2 . consequent . body [ 0 ] ) ) {
125
+ const target = if2 . consequent . body [ 0 ]
126
+ // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root1, result));
127
+ const newExpressionStatement = t . expressionStatement (
128
+ t . callExpression ( t . memberExpression ( t . memberExpression ( t . identifier ( variableName ) , t . identifier ( 'value' ) ) , t . identifier ( 'push' ) ) , [ target . expression ] ) ,
129
+ )
130
+ if2 . consequent . body [ 0 ] = newExpressionStatement
146
131
}
147
132
}
148
- // clear contextRef.value
149
- targetBlockStatement . body . unshift (
150
- // contentRef.value = []
151
- // t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
152
-
153
- // contentRef.value.length = 0
154
- t . expressionStatement (
155
- t . assignmentExpression (
156
- '=' ,
157
- t . memberExpression ( t . memberExpression ( t . identifier ( variableName ) , t . identifier ( valueKey ) ) , t . identifier ( 'length' ) ) ,
158
- t . numericLiteral ( 0 ) ,
159
- ) ,
160
- ) ,
161
- )
162
133
}
163
134
}
135
+ // clear contextRef.value
136
+ targetBlockStatement . body . unshift (
137
+ // contentRef.value = []
138
+ // t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
139
+
140
+ // contentRef.value.length = 0
141
+ t . expressionStatement (
142
+ t . assignmentExpression (
143
+ '=' ,
144
+ t . memberExpression ( t . memberExpression ( t . identifier ( variableName ) , t . identifier ( valueKey ) ) , t . identifier ( 'length' ) ) ,
145
+ t . numericLiteral ( 0 ) ,
146
+ ) ,
147
+ ) ,
148
+ )
164
149
}
165
150
}
166
151
}
167
- // start = true
168
152
} ,
169
- // BlockStatement(p) {
170
- // const n = p.node
171
- // if (start && p.parent.type === 'FunctionExpression' && !p.parent.id) {
172
- // n.body.unshift(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression())))
173
- // }
174
- // }
175
153
} )
176
154
return {
177
155
code : hasPatched ? content : generate ( ast ) . code ,
0 commit comments