-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcss.ts
183 lines (166 loc) · 5.33 KB
/
css.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { Expression } from '@babel/types';
import { validateParams } from '@wyw-in-js/processor-utils';
import type {
CallParam,
TemplateParam,
Params,
TailProcessorParams,
ValueCache,
} from '@wyw-in-js/processor-utils';
import type { Replacements, Rules } from '@wyw-in-js/shared';
import { ValueType } from '@wyw-in-js/shared';
import type { CSSInterpolation } from '@emotion/serialize';
import deepMerge from 'lodash/merge';
import BaseProcessor from './base-processor';
import type { IOptions } from './styled';
import { cache, css } from '../utils/emotion';
import type { Primitive, TemplateCallback } from './keyframes';
/**
* @description Scope css class generation similar to css from emotion.
*
* @example
* ```ts
* import { css } from '@pigment-css/react';
*
* const class1 = css(({theme}) => ({
* color: (theme.vars || theme).palette.primary.main,
* }))
* ```
*
* <html className={class1} />
*/
export class CssProcessor extends BaseProcessor {
callParam: CallParam | TemplateParam;
constructor(params: Params, ...args: TailProcessorParams) {
if (params.length < 2) {
throw BaseProcessor.SKIP;
}
super([params[0]], ...args);
validateParams(
params,
['callee', ['call', 'template']],
`Invalid use of ${this.tagSource.imported} tag.`,
);
const [, callParams] = params;
if (callParams[0] === 'call') {
const [, ...callArgs] = callParams;
this.dependencies.push(...callArgs);
} else if (callParams[0] === 'template') {
callParams[1].forEach((element) => {
if ('kind' in element && element.kind !== ValueType.CONST) {
this.dependencies.push(element);
}
});
}
this.callParam = callParams;
}
build(values: ValueCache) {
if (this.artifacts.length > 0) {
throw new Error(`MUI: "${this.tagSource.imported}" is already built`);
}
const [callType] = this.callParam;
if (callType === 'template') {
this.handleTemplate(this.callParam, values);
} else {
this.handleCall(this.callParam, values);
}
}
private handleTemplate([, callArgs]: TemplateParam, values: ValueCache) {
const templateStrs: string[] = [];
// @ts-ignore @TODO - Fix this. No idea how to initialize a Tagged String array.
templateStrs.raw = [];
const templateExpressions: Primitive[] = [];
const { themeArgs } = this.options as IOptions;
callArgs.forEach((item) => {
if ('kind' in item) {
switch (item.kind) {
case ValueType.FUNCTION: {
const value = values.get(item.ex.name) as TemplateCallback;
templateExpressions.push(value(themeArgs));
break;
}
case ValueType.CONST:
templateExpressions.push(item.value);
break;
case ValueType.LAZY: {
const evaluatedValue = values.get(item.ex.name);
if (typeof evaluatedValue === 'function') {
templateExpressions.push(evaluatedValue(themeArgs));
} else {
templateExpressions.push(evaluatedValue as Primitive);
}
break;
}
default:
break;
}
} else if (item.type === 'TemplateElement') {
templateStrs.push(item.value.cooked as string);
// @ts-ignore
templateStrs.raw.push(item.value.raw);
}
});
this.generateArtifacts(templateStrs, ...templateExpressions);
}
generateArtifacts(styleObjOrTaggged: CSSInterpolation | string[], ...args: Primitive[]) {
const cssClassName = css(styleObjOrTaggged, ...args);
const cssText = cache.registered[cssClassName] as string;
const rules: Rules = {
[this.asSelector]: {
className: this.className,
cssText,
displayName: this.displayName,
start: this.location?.start ?? null,
},
};
const sourceMapReplacements: Replacements = [
{
length: cssText.length,
original: {
start: {
column: this.location?.start.column ?? 0,
line: this.location?.start.line ?? 0,
},
end: {
column: this.location?.end.column ?? 0,
line: this.location?.end.line ?? 0,
},
},
},
];
this.artifacts.push(['css', [rules, sourceMapReplacements]]);
}
private handleCall([, ...callArgs]: CallParam, values: ValueCache) {
const mergedStyleObj: CSSInterpolation = {};
callArgs.forEach((callArg) => {
let styleObj: CSSInterpolation;
if (callArg.kind === ValueType.LAZY) {
styleObj = values.get(callArg.ex.name) as CSSInterpolation;
} else if (callArg.kind === ValueType.FUNCTION) {
const { themeArgs } = this.options as IOptions;
const value = values.get(callArg.ex.name) as (
args: Record<string, unknown> | undefined,
) => CSSInterpolation;
styleObj = value(themeArgs);
}
if (styleObj) {
deepMerge(mergedStyleObj, styleObj);
}
});
if (Object.keys(mergedStyleObj).length > 0) {
this.generateArtifacts(mergedStyleObj);
}
}
doEvaltimeReplacement() {
this.replacer(this.value, false);
}
doRuntimeReplacement() {
this.doEvaltimeReplacement();
}
get asSelector() {
return `.${this.className}`;
}
get value(): Expression {
return this.astService.stringLiteral(this.className);
}
}