@workday/canvas-kit-styling-transform 13.2.1 → 13.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/dist/commonjs/index.d.ts +14 -0
  2. package/dist/commonjs/index.d.ts.map +1 -0
  3. package/dist/commonjs/index.js +28 -0
  4. package/dist/commonjs/lib/createObjectTransform.d.ts +10 -0
  5. package/dist/commonjs/lib/createObjectTransform.d.ts.map +1 -0
  6. package/dist/commonjs/lib/createObjectTransform.js +14 -0
  7. package/dist/commonjs/lib/createPropertyTransform.d.ts +7 -0
  8. package/dist/commonjs/lib/createPropertyTransform.d.ts.map +1 -0
  9. package/dist/commonjs/lib/createPropertyTransform.js +11 -0
  10. package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts +16 -0
  11. package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
  12. package/dist/commonjs/lib/createTypeScriptWatchProgram.js +45 -0
  13. package/dist/commonjs/lib/styleTransform.d.ts +23 -0
  14. package/dist/commonjs/lib/styleTransform.d.ts.map +1 -0
  15. package/dist/commonjs/lib/styleTransform.js +131 -0
  16. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts +35 -0
  17. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts.map +1 -0
  18. package/dist/commonjs/lib/utils/createStyleObjectNode.js +84 -0
  19. package/dist/commonjs/lib/utils/getCssVariables.d.ts +4 -0
  20. package/dist/commonjs/lib/utils/getCssVariables.d.ts.map +1 -0
  21. package/dist/commonjs/lib/utils/getCssVariables.js +31 -0
  22. package/dist/commonjs/lib/utils/getErrorMessage.d.ts +16 -0
  23. package/dist/commonjs/lib/utils/getErrorMessage.d.ts.map +1 -0
  24. package/dist/commonjs/lib/utils/getErrorMessage.js +45 -0
  25. package/dist/commonjs/lib/utils/getFallbackVariable.d.ts +9 -0
  26. package/dist/commonjs/lib/utils/getFallbackVariable.d.ts.map +1 -0
  27. package/dist/commonjs/lib/utils/getFallbackVariable.js +30 -0
  28. package/dist/commonjs/lib/utils/getHash.d.ts +4 -0
  29. package/dist/commonjs/lib/utils/getHash.d.ts.map +1 -0
  30. package/dist/commonjs/lib/utils/getHash.js +16 -0
  31. package/dist/commonjs/lib/utils/getVarName.d.ts +17 -0
  32. package/dist/commonjs/lib/utils/getVarName.d.ts.map +1 -0
  33. package/dist/commonjs/lib/utils/getVarName.js +34 -0
  34. package/dist/commonjs/lib/utils/handleCalc.d.ts +2 -0
  35. package/dist/commonjs/lib/utils/handleCalc.d.ts.map +1 -0
  36. package/dist/commonjs/lib/utils/handleCalc.js +33 -0
  37. package/dist/commonjs/lib/utils/handleCreateStencil.d.ts +7 -0
  38. package/dist/commonjs/lib/utils/handleCreateStencil.d.ts.map +1 -0
  39. package/dist/commonjs/lib/utils/handleCreateStencil.js +309 -0
  40. package/dist/commonjs/lib/utils/handleCreateStyles.d.ts +3 -0
  41. package/dist/commonjs/lib/utils/handleCreateStyles.d.ts.map +1 -0
  42. package/dist/commonjs/lib/utils/handleCreateStyles.js +86 -0
  43. package/dist/commonjs/lib/utils/handleCreateVars.d.ts +3 -0
  44. package/dist/commonjs/lib/utils/handleCreateVars.d.ts.map +1 -0
  45. package/dist/commonjs/lib/utils/handleCreateVars.js +65 -0
  46. package/dist/commonjs/lib/utils/handleCssVar.d.ts +2 -0
  47. package/dist/commonjs/lib/utils/handleCssVar.d.ts.map +1 -0
  48. package/dist/commonjs/lib/utils/handleCssVar.js +19 -0
  49. package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts +3 -0
  50. package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts.map +1 -0
  51. package/dist/commonjs/lib/utils/handleInjectGlobal.js +40 -0
  52. package/dist/commonjs/lib/utils/handleKeyframes.d.ts +3 -0
  53. package/dist/commonjs/lib/utils/handleKeyframes.d.ts.map +1 -0
  54. package/dist/commonjs/lib/utils/handleKeyframes.js +48 -0
  55. package/dist/commonjs/lib/utils/handleParentModifier.d.ts +2 -0
  56. package/dist/commonjs/lib/utils/handleParentModifier.d.ts.map +1 -0
  57. package/dist/commonjs/lib/utils/handleParentModifier.js +26 -0
  58. package/dist/commonjs/lib/utils/handlePx2Rem.d.ts +2 -0
  59. package/dist/commonjs/lib/utils/handlePx2Rem.d.ts.map +1 -0
  60. package/dist/commonjs/lib/utils/handlePx2Rem.js +19 -0
  61. package/dist/commonjs/lib/utils/injectStyles.d.ts +3 -0
  62. package/dist/commonjs/lib/utils/injectStyles.d.ts.map +1 -0
  63. package/dist/commonjs/lib/utils/injectStyles.js +11 -0
  64. package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts +7 -0
  65. package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts.map +1 -0
  66. package/dist/commonjs/lib/utils/isImportedFromStyling.js +24 -0
  67. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts +11 -0
  68. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
  69. package/dist/commonjs/lib/utils/parseNodeToStaticValue.js +217 -0
  70. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts +15 -0
  71. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
  72. package/dist/commonjs/lib/utils/parseObjectToStaticValue.js +168 -0
  73. package/dist/commonjs/lib/utils/stylisFns.d.ts +3 -0
  74. package/dist/commonjs/lib/utils/stylisFns.d.ts.map +1 -0
  75. package/dist/commonjs/lib/utils/stylisFns.js +45 -0
  76. package/dist/commonjs/lib/utils/types.d.ts +210 -0
  77. package/dist/commonjs/lib/utils/types.d.ts.map +1 -0
  78. package/dist/commonjs/lib/utils/types.js +2 -0
  79. package/dist/commonjs/lib/webpack-loader.d.ts +7 -0
  80. package/dist/commonjs/lib/webpack-loader.d.ts.map +1 -0
  81. package/dist/commonjs/lib/webpack-loader.js +21 -0
  82. package/dist/commonjs/lib/webpackPlugin.d.ts +31 -0
  83. package/dist/commonjs/lib/webpackPlugin.d.ts.map +1 -0
  84. package/dist/commonjs/lib/webpackPlugin.js +38 -0
  85. package/dist/commonjs/spec/createProgramFromSource.d.ts +8 -0
  86. package/dist/commonjs/spec/createProgramFromSource.d.ts.map +1 -0
  87. package/dist/commonjs/spec/createProgramFromSource.js +111 -0
  88. package/dist/commonjs/spec/findNodes.d.ts +3 -0
  89. package/dist/commonjs/spec/findNodes.d.ts.map +1 -0
  90. package/dist/commonjs/spec/findNodes.js +33 -0
  91. package/dist/commonjs/testing.d.ts +4 -0
  92. package/dist/commonjs/testing.d.ts.map +1 -0
  93. package/dist/commonjs/testing.js +11 -0
  94. package/dist/es6/index.d.ts +14 -0
  95. package/dist/es6/index.d.ts.map +1 -0
  96. package/dist/es6/index.js +14 -0
  97. package/dist/es6/lib/createObjectTransform.d.ts +10 -0
  98. package/dist/es6/lib/createObjectTransform.d.ts.map +1 -0
  99. package/dist/es6/lib/createObjectTransform.js +10 -0
  100. package/dist/es6/lib/createPropertyTransform.d.ts +7 -0
  101. package/dist/es6/lib/createPropertyTransform.d.ts.map +1 -0
  102. package/dist/es6/lib/createPropertyTransform.js +7 -0
  103. package/dist/es6/lib/createTypeScriptWatchProgram.d.ts +16 -0
  104. package/dist/es6/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
  105. package/dist/es6/lib/createTypeScriptWatchProgram.js +36 -0
  106. package/dist/es6/lib/styleTransform.d.ts +23 -0
  107. package/dist/es6/lib/styleTransform.d.ts.map +1 -0
  108. package/dist/es6/lib/styleTransform.js +120 -0
  109. package/dist/es6/lib/utils/createStyleObjectNode.d.ts +35 -0
  110. package/dist/es6/lib/utils/createStyleObjectNode.d.ts.map +1 -0
  111. package/dist/es6/lib/utils/createStyleObjectNode.js +74 -0
  112. package/dist/es6/lib/utils/getCssVariables.d.ts +4 -0
  113. package/dist/es6/lib/utils/getCssVariables.d.ts.map +1 -0
  114. package/dist/es6/lib/utils/getCssVariables.js +26 -0
  115. package/dist/es6/lib/utils/getErrorMessage.d.ts +16 -0
  116. package/dist/es6/lib/utils/getErrorMessage.d.ts.map +1 -0
  117. package/dist/es6/lib/utils/getErrorMessage.js +41 -0
  118. package/dist/es6/lib/utils/getFallbackVariable.d.ts +9 -0
  119. package/dist/es6/lib/utils/getFallbackVariable.d.ts.map +1 -0
  120. package/dist/es6/lib/utils/getFallbackVariable.js +26 -0
  121. package/dist/es6/lib/utils/getHash.d.ts +4 -0
  122. package/dist/es6/lib/utils/getHash.d.ts.map +1 -0
  123. package/dist/es6/lib/utils/getHash.js +9 -0
  124. package/dist/es6/lib/utils/getVarName.d.ts +17 -0
  125. package/dist/es6/lib/utils/getVarName.d.ts.map +1 -0
  126. package/dist/es6/lib/utils/getVarName.js +27 -0
  127. package/dist/es6/lib/utils/handleCalc.d.ts +2 -0
  128. package/dist/es6/lib/utils/handleCalc.d.ts.map +1 -0
  129. package/dist/es6/lib/utils/handleCalc.js +27 -0
  130. package/dist/es6/lib/utils/handleCreateStencil.d.ts +7 -0
  131. package/dist/es6/lib/utils/handleCreateStencil.d.ts.map +1 -0
  132. package/dist/es6/lib/utils/handleCreateStencil.js +301 -0
  133. package/dist/es6/lib/utils/handleCreateStyles.d.ts +3 -0
  134. package/dist/es6/lib/utils/handleCreateStyles.d.ts.map +1 -0
  135. package/dist/es6/lib/utils/handleCreateStyles.js +79 -0
  136. package/dist/es6/lib/utils/handleCreateVars.d.ts +3 -0
  137. package/dist/es6/lib/utils/handleCreateVars.d.ts.map +1 -0
  138. package/dist/es6/lib/utils/handleCreateVars.js +58 -0
  139. package/dist/es6/lib/utils/handleCssVar.d.ts +2 -0
  140. package/dist/es6/lib/utils/handleCssVar.d.ts.map +1 -0
  141. package/dist/es6/lib/utils/handleCssVar.js +13 -0
  142. package/dist/es6/lib/utils/handleInjectGlobal.d.ts +3 -0
  143. package/dist/es6/lib/utils/handleInjectGlobal.d.ts.map +1 -0
  144. package/dist/es6/lib/utils/handleInjectGlobal.js +33 -0
  145. package/dist/es6/lib/utils/handleKeyframes.d.ts +3 -0
  146. package/dist/es6/lib/utils/handleKeyframes.d.ts.map +1 -0
  147. package/dist/es6/lib/utils/handleKeyframes.js +41 -0
  148. package/dist/es6/lib/utils/handleParentModifier.d.ts +2 -0
  149. package/dist/es6/lib/utils/handleParentModifier.d.ts.map +1 -0
  150. package/dist/es6/lib/utils/handleParentModifier.js +20 -0
  151. package/dist/es6/lib/utils/handlePx2Rem.d.ts +2 -0
  152. package/dist/es6/lib/utils/handlePx2Rem.d.ts.map +1 -0
  153. package/dist/es6/lib/utils/handlePx2Rem.js +13 -0
  154. package/dist/es6/lib/utils/injectStyles.d.ts +3 -0
  155. package/dist/es6/lib/utils/injectStyles.d.ts.map +1 -0
  156. package/dist/es6/lib/utils/injectStyles.js +7 -0
  157. package/dist/es6/lib/utils/isImportedFromStyling.d.ts +7 -0
  158. package/dist/es6/lib/utils/isImportedFromStyling.d.ts.map +1 -0
  159. package/dist/es6/lib/utils/isImportedFromStyling.js +17 -0
  160. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts +11 -0
  161. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
  162. package/dist/es6/lib/utils/parseNodeToStaticValue.js +209 -0
  163. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts +15 -0
  164. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
  165. package/dist/es6/lib/utils/parseObjectToStaticValue.js +159 -0
  166. package/dist/es6/lib/utils/stylisFns.d.ts +3 -0
  167. package/dist/es6/lib/utils/stylisFns.d.ts.map +1 -0
  168. package/dist/es6/lib/utils/stylisFns.js +41 -0
  169. package/dist/es6/lib/utils/types.d.ts +210 -0
  170. package/dist/es6/lib/utils/types.d.ts.map +1 -0
  171. package/dist/es6/lib/utils/types.js +1 -0
  172. package/dist/es6/lib/webpack-loader.d.ts +7 -0
  173. package/dist/es6/lib/webpack-loader.d.ts.map +1 -0
  174. package/dist/es6/lib/webpack-loader.js +15 -0
  175. package/dist/es6/lib/webpackPlugin.d.ts +31 -0
  176. package/dist/es6/lib/webpackPlugin.d.ts.map +1 -0
  177. package/dist/es6/lib/webpackPlugin.js +31 -0
  178. package/dist/es6/spec/createProgramFromSource.d.ts +8 -0
  179. package/dist/es6/spec/createProgramFromSource.d.ts.map +1 -0
  180. package/dist/es6/spec/createProgramFromSource.js +81 -0
  181. package/dist/es6/spec/findNodes.d.ts +3 -0
  182. package/dist/es6/spec/findNodes.d.ts.map +1 -0
  183. package/dist/es6/spec/findNodes.js +26 -0
  184. package/dist/es6/testing.d.ts +4 -0
  185. package/dist/es6/testing.d.ts.map +1 -0
  186. package/dist/es6/testing.js +3 -0
  187. package/index.ts +19 -0
  188. package/lib/createObjectTransform.ts +12 -0
  189. package/lib/createPropertyTransform.ts +9 -0
  190. package/lib/createTypeScriptWatchProgram.ts +52 -0
  191. package/lib/styleTransform.ts +169 -0
  192. package/lib/utils/createStyleObjectNode.ts +105 -0
  193. package/lib/utils/getCssVariables.ts +36 -0
  194. package/lib/utils/getErrorMessage.ts +57 -0
  195. package/lib/utils/getFallbackVariable.ts +34 -0
  196. package/lib/utils/getHash.ts +13 -0
  197. package/lib/utils/getVarName.ts +32 -0
  198. package/lib/utils/handleCalc.ts +33 -0
  199. package/lib/utils/handleCreateStencil.ts +442 -0
  200. package/lib/utils/handleCreateStyles.ts +97 -0
  201. package/lib/utils/handleCreateVars.ts +90 -0
  202. package/lib/utils/handleCssVar.ts +19 -0
  203. package/lib/utils/handleInjectGlobal.ts +51 -0
  204. package/lib/utils/handleKeyframes.ts +60 -0
  205. package/lib/utils/handleParentModifier.ts +29 -0
  206. package/lib/utils/handlePx2Rem.ts +22 -0
  207. package/lib/utils/injectStyles.ts +14 -0
  208. package/lib/utils/isImportedFromStyling.ts +21 -0
  209. package/lib/utils/parseNodeToStaticValue.ts +281 -0
  210. package/lib/utils/parseObjectToStaticValue.ts +201 -0
  211. package/lib/utils/stylisFns.ts +56 -0
  212. package/lib/utils/types.ts +223 -0
  213. package/lib/webpack-loader.ts +28 -0
  214. package/lib/webpackPlugin.ts +69 -0
  215. package/package.json +19 -14
  216. package/index.js +0 -22
@@ -0,0 +1,301 @@
1
+ import ts from 'typescript';
2
+ import { slugify } from '@workday/canvas-kit-styling';
3
+ import { getVarName } from './getVarName';
4
+ import { maybeWrapCSSVariables, parseObjectToStaticValue } from './parseObjectToStaticValue';
5
+ import { createStyleObjectNode, serializeStyles } from './createStyleObjectNode';
6
+ import { getValueFromAliasedSymbol, parseNodeToStaticValue } from './parseNodeToStaticValue';
7
+ import { isImportedFromStyling } from './isImportedFromStyling';
8
+ import { getHash } from './getHash';
9
+ /**
10
+ * Handle all arguments of the CallExpression `createStencil()`
11
+ */
12
+ export const handleCreateStencil = (node, context) => {
13
+ const { checker, names, extractedNames } = context;
14
+ /**
15
+ * This will match whenever a `createStencil()` call expression is encountered. It will loop
16
+ * over all the config to extract variables and styles.
17
+ *
18
+ */
19
+ if (ts.isCallExpression(node) &&
20
+ ts.isIdentifier(node.expression) &&
21
+ node.expression.text === 'createStencil' &&
22
+ isImportedFromStyling(node.expression, checker)) {
23
+ const config = node.arguments[0];
24
+ // We need to keep track of stencil variables and values to automatically merge into the base
25
+ // styles
26
+ const stencilVariables = {};
27
+ // Stencil name is the variable name
28
+ const stencilName = getVarName(node.expression);
29
+ const stencilHash = getHash(node, context);
30
+ if (ts.isObjectLiteralExpression(config)) {
31
+ config.properties.forEach(property => {
32
+ if (ts.isPropertyAssignment(property) &&
33
+ property.name &&
34
+ ts.isIdentifier(property.name) &&
35
+ property.name.text === 'extends' &&
36
+ ts.isIdentifier(property.initializer)) {
37
+ const className = getClassName(property.initializer.text, context);
38
+ const extendsStencilName = property.initializer.text;
39
+ if (!Object.values(context.styles).some(fileStyles => {
40
+ return fileStyles.some(rule => rule.includes(`.${className}`));
41
+ })) {
42
+ // Process the extended stencil since those styles haven't been processed yet
43
+ getValueFromAliasedSymbol(property.initializer, '', context);
44
+ }
45
+ // attach all variables from extends stencil
46
+ Object.keys(names).forEach(key => {
47
+ if (key.startsWith(`${extendsStencilName}.`)) {
48
+ // We want to copy a new entry into variables that is the extended stencil with the same variable name as the base variable name
49
+ names[key.replace(extendsStencilName, stencilName)] = names[key];
50
+ }
51
+ });
52
+ }
53
+ });
54
+ // get variables first
55
+ const varsConfig = config.properties.find(property => {
56
+ return (property.name &&
57
+ ts.isIdentifier(property.name) &&
58
+ property.name.text === 'vars' &&
59
+ ts.isPropertyAssignment(property));
60
+ });
61
+ function extractNames(node) {
62
+ if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
63
+ if (ts.isObjectLiteralExpression(node.initializer)) {
64
+ return node.initializer.properties.map(extractNames);
65
+ }
66
+ const varName = getVarName(node.name);
67
+ const varValue = `--${getClassName(varName, context)}`;
68
+ names[varName] = `--${node.name.text}-${slugify(stencilName).replace('-stencil', '')}-${stencilHash}`;
69
+ names[node.name.text] = names[varName];
70
+ extractedNames[names[varName]] = varValue;
71
+ // Evaluate the variable defaults
72
+ const value = parseNodeToStaticValue(node.initializer, context).toString();
73
+ if (value) {
74
+ // Only add the stencil variable if there's a value. An empty string means no default.
75
+ stencilVariables[names[varName]] = String(maybeWrapCSSVariables(value, names));
76
+ }
77
+ }
78
+ }
79
+ if (varsConfig && ts.isObjectLiteralExpression(varsConfig.initializer)) {
80
+ varsConfig.initializer.properties.forEach(variable => {
81
+ extractNames(variable);
82
+ });
83
+ }
84
+ const configProperties = config.properties.map((property, index, properties) => {
85
+ if (property.name && ts.isIdentifier(property.name)) {
86
+ // base config object
87
+ if (property.name.text === 'base') {
88
+ const styleObj = parseStyleBlock(property, context, stencilName);
89
+ if (styleObj) {
90
+ const initializer = createStyleReplacementNode(property, {
91
+ ...stencilVariables,
92
+ boxSizing: 'border-box',
93
+ ...styleObj,
94
+ }, getClassName(getVarName(property.name), context), context);
95
+ return updateStyleProperty(property, initializer);
96
+ }
97
+ }
98
+ // modifiers config object
99
+ if (property.name.text === 'modifiers' &&
100
+ ts.isPropertyAssignment(property) &&
101
+ ts.isObjectLiteralExpression(property.initializer)) {
102
+ // modifier key
103
+ return ts.factory.updatePropertyAssignment(property, property.name, ts.factory.updateObjectLiteralExpression(property.initializer, property.initializer.properties.map(modifierProperty => {
104
+ if (ts.isPropertyAssignment(modifierProperty) &&
105
+ modifierProperty.name &&
106
+ ts.isIdentifier(modifierProperty.name) &&
107
+ ts.isObjectLiteralExpression(modifierProperty.initializer)) {
108
+ // modifier value
109
+ return ts.factory.updatePropertyAssignment(modifierProperty, modifierProperty.name, ts.factory.updateObjectLiteralExpression(modifierProperty.initializer, modifierProperty.initializer.properties.map(modifier => {
110
+ const styleObj = parseStyleBlock(modifier, context, getClassName(stencilName, context));
111
+ if (styleObj && modifier.name) {
112
+ const stencilClassName = getClassName(stencilName, context);
113
+ const fullModifierName = getClassName(getVarName(modifier.name), context);
114
+ const className = `${stencilClassName}.${fullModifierName.replace(`${stencilClassName}--`, '')}`;
115
+ const initializer = createStyleReplacementNode(modifier, styleObj, className, context);
116
+ return updateStyleProperty(modifier, initializer);
117
+ }
118
+ return modifier;
119
+ })));
120
+ }
121
+ return property;
122
+ })));
123
+ }
124
+ // compound config array
125
+ /**
126
+ * Compound config array. It looks like:
127
+ *
128
+ * ```ts
129
+ * compound: [
130
+ * {
131
+ * modifiers: { size: 'large', iconPosition: 'start' },
132
+ * styles: { padding: 20 }
133
+ * }
134
+ * ]
135
+ * ```
136
+ */
137
+ if (property.name.text === 'compound' &&
138
+ ts.isPropertyAssignment(property) &&
139
+ ts.isArrayLiteralExpression(property.initializer)) {
140
+ return ts.factory.updatePropertyAssignment(property, property.name, ts.factory.updateArrayLiteralExpression(property.initializer, property.initializer.elements.map(element => {
141
+ if (ts.isObjectLiteralExpression(element)) {
142
+ const selectors = [];
143
+ selectors.push(`.${getClassName(stencilName, context)}`);
144
+ return ts.factory.updateObjectLiteralExpression(element, element.properties.map((compoundProperty, index, compoundProperties) => {
145
+ /**
146
+ * If the property is `modifiers`, we want to extract selectors from it. For
147
+ * example,
148
+ *
149
+ * ```ts
150
+ * const button = createStencil({
151
+ * // other config
152
+ * compound: {
153
+ * modifiers: { size: 'large', inverse: true },
154
+ * styles: {}
155
+ * }
156
+ * })
157
+ * ```
158
+ *
159
+ * After this, `selectors` should contain ['.button--size-large',
160
+ * '.button--inverse']
161
+ */
162
+ if (compoundProperty.name &&
163
+ ts.isIdentifier(compoundProperty.name) &&
164
+ compoundProperty.name.text === 'modifiers' &&
165
+ ts.isPropertyAssignment(compoundProperty) &&
166
+ ts.isObjectLiteralExpression(compoundProperty.initializer)) {
167
+ compoundProperty.initializer.properties.forEach(modifier => {
168
+ if (ts.isPropertyAssignment(modifier)) {
169
+ let className = '';
170
+ if (ts.isIdentifier(modifier.name)) {
171
+ className = slugify(modifier.name.text);
172
+ }
173
+ // The initializer should either be the `true` keyword or a string
174
+ // literal. We don't add `-true` to the name.
175
+ if (ts.isStringLiteral(modifier.initializer)) {
176
+ className += `-${modifier.initializer.text}`;
177
+ }
178
+ selectors.push(className);
179
+ }
180
+ });
181
+ return compoundProperty;
182
+ }
183
+ // styles key
184
+ if (compoundProperty.name &&
185
+ ts.isIdentifier(compoundProperty.name) &&
186
+ compoundProperty.name.text === 'styles') {
187
+ const styleObj = parseStyleBlock(compoundProperty, context, stencilName);
188
+ if (styleObj) {
189
+ // We need to inject compound style selectors into a file. We'll compound the
190
+ // selectors with multiple class names. This will increase specificity of compound
191
+ // selectors. This will be harder to override and we don't increase specificity in
192
+ // the runtime implementation, but runtime creates an extra CSS class name that
193
+ // isn't known to anyone. It seems unreasonable to expect CSS users to remember to
194
+ // add compound modifier class names. We'll make it so it is easier to author
195
+ // components in CSS and let them sort the specificity issues.
196
+ const serialized = serializeStyles(compoundProperty, styleObj, `${selectors.join('.')}{%s}`, context);
197
+ const initializer = createStyleObjectNode(serialized.styles, serialized.name);
198
+ return updateStyleProperty(compoundProperty, initializer);
199
+ }
200
+ }
201
+ return compoundProperty;
202
+ }));
203
+ }
204
+ return element;
205
+ })));
206
+ }
207
+ }
208
+ return property;
209
+ });
210
+ return ts.factory.updateCallExpression(node, node.expression, undefined, [
211
+ ts.factory.updateObjectLiteralExpression(config, configProperties),
212
+ ts.factory.createStringLiteral(`${slugify(stencilName).replace('-stencil', '')}-${stencilHash}`),
213
+ ]);
214
+ }
215
+ }
216
+ return;
217
+ };
218
+ /**
219
+ * A style block is a `base`, `modifier`, or `compoundModifier` style block. It could be an ObjectLiteralExpression,
220
+ * an ArrowFunction, MethodDeclaration, etc.
221
+ */
222
+ function parseStyleBlock(property, context, stencilName) {
223
+ let styleObj;
224
+ if (ts.isPropertyAssignment(property)) {
225
+ if (ts.isObjectLiteralExpression(property.initializer)) {
226
+ styleObj = parseObjectToStaticValue(property.initializer, {
227
+ ...context,
228
+ nameScope: `${stencilName}.vars.`,
229
+ });
230
+ }
231
+ if (isFunctionLikeDeclaration(property.initializer)) {
232
+ const returnNode = getReturnStatement(property.initializer);
233
+ if (returnNode) {
234
+ styleObj = parseObjectToStaticValue(returnNode, {
235
+ ...context,
236
+ nameScope: `${stencilName}.vars.`,
237
+ });
238
+ }
239
+ }
240
+ }
241
+ if (isFunctionLikeDeclaration(property)) {
242
+ const returnNode = getReturnStatement(property);
243
+ if (returnNode) {
244
+ styleObj = parseObjectToStaticValue(returnNode, {
245
+ ...context,
246
+ nameScope: `${stencilName}.vars.`,
247
+ });
248
+ }
249
+ }
250
+ return styleObj;
251
+ }
252
+ function getReturnStatement(node) {
253
+ if (node.body && ts.isParenthesizedExpression(node.body)) {
254
+ return node.body.expression;
255
+ }
256
+ if (node.body && ts.isBlock(node.body)) {
257
+ let returnNode;
258
+ // look for the return statement. It must be a top-level statement in the block
259
+ node.body.statements.forEach(statement => {
260
+ // () => { return {} }
261
+ if (ts.isReturnStatement(statement)) {
262
+ returnNode = statement.expression;
263
+ }
264
+ });
265
+ return returnNode;
266
+ }
267
+ return undefined;
268
+ }
269
+ function isFunctionLikeDeclaration(node) {
270
+ return node.hasOwnProperty('body');
271
+ }
272
+ function createStyleReplacementNode(node, styleObj, className, context) {
273
+ const { names, extractedNames } = context;
274
+ const serialized = serializeStyles(node, styleObj, `.${className}{%s}`, context);
275
+ const varName = getVarName(node);
276
+ const value = `css-${serialized.name}`;
277
+ names[varName] = value;
278
+ extractedNames[value] = getClassName(varName, context);
279
+ return createStyleObjectNode(serialized.styles, serialized.name);
280
+ }
281
+ export function getClassName(name, { prefix }) {
282
+ return (`${prefix}-` +
283
+ slugify(name)
284
+ .replace('-vars', '')
285
+ .replace('-stencil', '')
286
+ .replace('-base', '')
287
+ .replace('-modifiers', '-')
288
+ .replace('-true', '')
289
+ .replace('-compound', ''));
290
+ }
291
+ /**
292
+ * If the property is a `PropertyAssignment`, use the TypeScript factory updater to maximize
293
+ * sourcemap use. Otherwise, return a new property assignment. For example, a Stencil could be using
294
+ * a `MethodDeclaration` which means we return a different node type: `base({color}) {}`
295
+ */
296
+ function updateStyleProperty(property, initializer) {
297
+ if (ts.isPropertyAssignment(property)) {
298
+ return ts.factory.updatePropertyAssignment(property, property.name, initializer);
299
+ }
300
+ return ts.factory.createPropertyAssignment(property.name, initializer);
301
+ }
@@ -0,0 +1,3 @@
1
+ import { NodeTransformer } from './types';
2
+ export declare const handleCreateStyles: NodeTransformer;
3
+ //# sourceMappingURL=handleCreateStyles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleCreateStyles.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateStyles.ts"],"names":[],"mappings":"AAIA,OAAO,EAAoB,eAAe,EAAqB,MAAM,SAAS,CAAC;AAK/E,eAAO,MAAM,kBAAkB,EAAE,eA4EhC,CAAC"}
@@ -0,0 +1,79 @@
1
+ import ts from 'typescript';
2
+ import { parseObjectToStaticValue, parseStyleObjFromType } from './parseObjectToStaticValue';
3
+ import { createStyleObjectNode, serializeStyles } from './createStyleObjectNode';
4
+ import { isImportedFromStyling } from './isImportedFromStyling';
5
+ import { getVarName } from './getVarName';
6
+ import { slugify } from '@workday/canvas-kit-styling';
7
+ export const handleCreateStyles = (node, context) => {
8
+ const { checker, prefix } = context;
9
+ /**
10
+ * Check if the node is a call expression that looks like:
11
+ *
12
+ * ```ts
13
+ * createStyles({
14
+ * // properties
15
+ * })
16
+ * ```
17
+ *
18
+ * It will also make sure the `createStyles` function was imported from
19
+ * `@workday/canvas-kit-styling` to ensure we don't rewrite the AST of code we don't own.
20
+ *
21
+ * This transformation will pre-serialize the style objects and turn them into strings for
22
+ * faster runtime processing in Emotion. The following is an example of the transformation.
23
+ *
24
+ * ```ts
25
+ * // before transformation
26
+ * const myStyles = createStyles({
27
+ * fontSize: '1rem'
28
+ * })
29
+ *
30
+ * // after transformation
31
+ * const myStyles = createStyles({
32
+ * name: 'abc123',
33
+ * styles: 'font-size: 1rem;'
34
+ * })
35
+ * ```
36
+ *
37
+ * The after transformation already serialized the styles and goes through a shortcut process
38
+ * in `@emotion/css` where only the Emotion cache is checked and styles are inserted if the
39
+ * cache key wasn't found.
40
+ */
41
+ if (ts.isCallExpression(node) &&
42
+ ts.isIdentifier(node.expression) &&
43
+ node.expression.text === 'createStyles' &&
44
+ isImportedFromStyling(node.expression, checker) &&
45
+ node.arguments.length > 0) {
46
+ const cssClassName = `${prefix}-${slugify(getVarName(node.expression))
47
+ .replace('-styles', '')
48
+ .replace('-modifiers', '-')
49
+ .replace('-true', '')}`;
50
+ const newArguments = [...node.arguments].map(arg => {
51
+ // An `ObjectLiteralExpression` is an object like `{foo:'bar'}`:
52
+ // https://ts-ast-viewer.com/#code/MYewdgzgLgBFCmBbADjAvDA3gKBjAZiCAFwwDkARgIYBOZ2AvkA
53
+ if (ts.isObjectLiteralExpression(arg)) {
54
+ const styleObj = parseObjectToStaticValue(arg, context);
55
+ return createStyleReplacementNode(node, styleObj, cssClassName, context);
56
+ }
57
+ // An Identifier is a variable. It could come from anywhere - imports, earlier
58
+ // assignments, etc. The easiest thing to do is to ask the TypeScript type checker what
59
+ // the type representation is and go from there.
60
+ if (ts.isIdentifier(arg)) {
61
+ const type = checker.getTypeAtLocation(arg);
62
+ // `createStyles` accepts strings as class names. If the class name is
63
+ if (type.isStringLiteral() || type.getFlags() & ts.TypeFlags.String) {
64
+ return arg;
65
+ }
66
+ // The type must be a object
67
+ const styleObj = parseStyleObjFromType(type, context);
68
+ return createStyleReplacementNode(node, styleObj, cssClassName, context);
69
+ }
70
+ return arg;
71
+ });
72
+ return ts.factory.updateCallExpression(node, node.expression, [], newArguments);
73
+ }
74
+ return;
75
+ };
76
+ function createStyleReplacementNode(node, styleObj, className, context) {
77
+ const serialized = serializeStyles(node, styleObj, `.${className}{%s}`, context);
78
+ return createStyleObjectNode(serialized.styles, serialized.name);
79
+ }
@@ -0,0 +1,3 @@
1
+ import { NodeTransformer } from './types';
2
+ export declare const handleCreateVars: NodeTransformer;
3
+ //# sourceMappingURL=handleCreateVars.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleCreateVars.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateVars.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,eAAe,EAAqB,MAAM,SAAS,CAAC;AAG5D,eAAO,MAAM,gBAAgB,EAAE,eA+D9B,CAAC"}
@@ -0,0 +1,58 @@
1
+ import ts from 'typescript';
2
+ import { slugify } from '@workday/canvas-kit-styling';
3
+ import { getVarName } from './getVarName';
4
+ import { getHash } from './getHash';
5
+ export const handleCreateVars = (node, context) => {
6
+ /**
7
+ * This will create a variable
8
+ */
9
+ if (ts.isCallExpression(node) &&
10
+ ts.isIdentifier(node.expression) &&
11
+ node.expression.text === 'createVars') {
12
+ const { id, vars } = addNames(node, context);
13
+ return ts.factory.updateCallExpression(node, node.expression, [], [
14
+ ts.factory.createObjectLiteralExpression([
15
+ ts.factory.createPropertyAssignment(ts.factory.createIdentifier('id'), ts.factory.createStringLiteral(id)),
16
+ ts.factory.createPropertyAssignment(ts.factory.createIdentifier('args'), ts.factory.createArrayLiteralExpression(vars.map(val => ts.factory.createStringLiteral(val)), false)),
17
+ ], false),
18
+ ]);
19
+ }
20
+ /**
21
+ * Check to see if this node is an ObjectLiteralExpression with properties with `createVars`
22
+ * values.
23
+ *
24
+ * ```ts
25
+ * const foo = {
26
+ * bar: createVars('color')
27
+ * }
28
+ * ```
29
+ *
30
+ * This happens when names are imported from other files. We don't need to transform this, but
31
+ * we'll need to add names for property parsing.
32
+ */
33
+ if (ts.isObjectLiteralExpression(node)) {
34
+ node.properties.forEach(property => {
35
+ if (ts.isPropertyAssignment(property) &&
36
+ ts.isCallExpression(property.initializer) &&
37
+ ts.isIdentifier(property.initializer.expression) &&
38
+ property.initializer.expression.text === 'createVars') {
39
+ addNames(property.initializer, context);
40
+ }
41
+ });
42
+ }
43
+ return;
44
+ };
45
+ function addNames(node, context) {
46
+ const { prefix, names, extractedNames } = context;
47
+ const hash = getHash(node, context);
48
+ const varNamePrefix = getVarName(node);
49
+ const vars = node.arguments
50
+ .map(arg => ts.isStringLiteral(arg) && arg.text)
51
+ .filter(Boolean);
52
+ vars.forEach(v => {
53
+ const varName = `${varNamePrefix}.${v}`;
54
+ names[varName] = `--${v}-${hash}`;
55
+ extractedNames[names[varName]] = `--${prefix}-${slugify(varName).replace('-vars', '')}`;
56
+ });
57
+ return { id: hash, vars };
58
+ }
@@ -0,0 +1,2 @@
1
+ export declare const handleCssVar: import("./types").PropertyTransform;
2
+ //# sourceMappingURL=handleCssVar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleCssVar.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCssVar.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,YAAY,qCAYvB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import ts from 'typescript';
2
+ import { cssVar } from '@workday/canvas-kit-styling';
3
+ import { createPropertyTransform } from '../createPropertyTransform';
4
+ import { parseNodeToStaticValue } from './parseNodeToStaticValue';
5
+ export const handleCssVar = createPropertyTransform((node, context) => {
6
+ if (ts.isCallExpression(node) &&
7
+ ts.isIdentifier(node.expression) &&
8
+ node.expression.text === 'cssVar') {
9
+ const args = node.arguments.map(arg => parseNodeToStaticValue(arg, context));
10
+ return cssVar(args[0], args[1] || context.names[args[0]]);
11
+ }
12
+ return;
13
+ });
@@ -0,0 +1,3 @@
1
+ import { NodeTransformer } from './types';
2
+ export declare const handleInjectGlobal: NodeTransformer;
3
+ //# sourceMappingURL=handleInjectGlobal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleInjectGlobal.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleInjectGlobal.ts"],"names":[],"mappings":"AAKA,OAAO,EAAoB,eAAe,EAAqB,MAAM,SAAS,CAAC;AAG/E,eAAO,MAAM,kBAAkB,EAAE,eAgChC,CAAC"}
@@ -0,0 +1,33 @@
1
+ import ts from 'typescript';
2
+ import { isImportedFromStyling } from './isImportedFromStyling';
3
+ import { parseObjectToStaticValue } from './parseObjectToStaticValue';
4
+ import { createStyleObjectNode, serializeStyles } from './createStyleObjectNode';
5
+ import { parseNodeToStaticValue } from './parseNodeToStaticValue';
6
+ export const handleInjectGlobal = (node, context) => {
7
+ const { checker } = context;
8
+ if (ts.isTaggedTemplateExpression(node) &&
9
+ ts.isIdentifier(node.tag) &&
10
+ node.tag.text === 'injectGlobal' &&
11
+ isImportedFromStyling(node.tag, checker)) {
12
+ const styleObj = parseNodeToStaticValue(node.template, context).toString();
13
+ return ts.factory.createCallExpression(node.tag, undefined, [
14
+ createStyleReplacementNode(node, styleObj, context),
15
+ ]);
16
+ }
17
+ if (ts.isCallExpression(node) &&
18
+ ts.isIdentifier(node.expression) &&
19
+ node.expression.text === 'injectGlobal' &&
20
+ isImportedFromStyling(node.expression, checker)) {
21
+ if (ts.isObjectLiteralExpression(node.arguments[0])) {
22
+ const styleObj = parseObjectToStaticValue(node.arguments[0], context);
23
+ return ts.factory.updateCallExpression(node, node.expression, undefined, [
24
+ createStyleReplacementNode(node, styleObj, context),
25
+ ]);
26
+ }
27
+ }
28
+ return;
29
+ };
30
+ function createStyleReplacementNode(node, styleObj, context) {
31
+ const serialized = serializeStyles(node, styleObj, '%s', context);
32
+ return createStyleObjectNode(serialized.styles, serialized.name);
33
+ }
@@ -0,0 +1,3 @@
1
+ import { NodeTransformer } from './types';
2
+ export declare const handleKeyframes: NodeTransformer;
3
+ //# sourceMappingURL=handleKeyframes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleKeyframes.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleKeyframes.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,eAAe,EAAqB,MAAM,SAAS,CAAC;AAO/E,eAAO,MAAM,eAAe,EAAE,eAoC7B,CAAC"}
@@ -0,0 +1,41 @@
1
+ import ts from 'typescript';
2
+ import { parseNodeToStaticValue } from './parseNodeToStaticValue';
3
+ import { getVarName } from './getVarName';
4
+ import { createStyleObjectNode, serializeStyles } from './createStyleObjectNode';
5
+ import { isImportedFromStyling } from './isImportedFromStyling';
6
+ import { parseObjectToStaticValue } from './parseObjectToStaticValue';
7
+ export const handleKeyframes = (node, context) => {
8
+ const { checker } = context;
9
+ // keyframes`css`
10
+ if (ts.isTaggedTemplateExpression(node) &&
11
+ ts.isIdentifier(node.tag) &&
12
+ node.tag.text === 'keyframes' &&
13
+ isImportedFromStyling(node.tag, checker)) {
14
+ // parseNodeToStaticValue can parse templates. Pass it through there to get a single static string
15
+ const styleObj = parseNodeToStaticValue(node.template, context).toString();
16
+ const identifierName = getVarName(node);
17
+ return ts.factory.createCallExpression(node.tag, undefined, [
18
+ createStyleReplacementNode(node, styleObj, identifierName, context),
19
+ ]);
20
+ }
21
+ // keyframes({})
22
+ if (ts.isCallExpression(node) &&
23
+ ts.isIdentifier(node.expression) &&
24
+ node.expression.text === 'keyframes' &&
25
+ isImportedFromStyling(node.expression, checker)) {
26
+ if (ts.isObjectLiteralExpression(node.arguments[0])) {
27
+ const styleObj = parseObjectToStaticValue(node.arguments[0], context);
28
+ const identifierName = getVarName(node);
29
+ return ts.factory.updateCallExpression(node, node.expression, undefined, [
30
+ createStyleReplacementNode(node, styleObj, identifierName, context),
31
+ ]);
32
+ }
33
+ }
34
+ return;
35
+ };
36
+ function createStyleReplacementNode(node, styleObj, identifierName, context) {
37
+ const serialized = serializeStyles(node, styleObj, `@keyframes animation-%n{%s}`, context);
38
+ const animationName = `animation-${serialized.name}`;
39
+ context.names[identifierName] = animationName;
40
+ return createStyleObjectNode(serialized.styles, serialized.name);
41
+ }
@@ -0,0 +1,2 @@
1
+ export declare const handleParentModifier: import("./types").PropertyTransform;
2
+ //# sourceMappingURL=handleParentModifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleParentModifier.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleParentModifier.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,oBAAoB,qCAsB/B,CAAC"}
@@ -0,0 +1,20 @@
1
+ import ts from 'typescript';
2
+ import { parentModifier } from '@workday/canvas-kit-styling';
3
+ import { createPropertyTransform } from '../createPropertyTransform';
4
+ import { parseNodeToStaticValue } from './parseNodeToStaticValue';
5
+ export const handleParentModifier = createPropertyTransform((node, context) => {
6
+ const { names, extractedNames } = context;
7
+ if (ts.isComputedPropertyName(node) &&
8
+ ts.isCallExpression(node.expression) &&
9
+ ts.isIdentifier(node.expression.expression) &&
10
+ node.expression.expression.text === 'parentModifier') {
11
+ const args = node.expression.arguments.map(arg => parseNodeToStaticValue(arg, context));
12
+ const hash = args[0].toString().replace('css-', 'm');
13
+ // add a mapping from `css-{hash}` to `{hash}` for extraction string replacement
14
+ names[args[0]] = hash;
15
+ // map the `{hash}` to the extracted CSS class name
16
+ extractedNames[hash] = extractedNames[args[0]];
17
+ return parentModifier(args[0].toString());
18
+ }
19
+ return;
20
+ });
@@ -0,0 +1,2 @@
1
+ export declare const handlePx2Rem: import("./types").PropertyTransform;
2
+ //# sourceMappingURL=handlePx2Rem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlePx2Rem.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handlePx2Rem.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,YAAY,qCAevB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import ts from 'typescript';
2
+ import { px2rem } from '@workday/canvas-kit-styling';
3
+ import { createPropertyTransform } from '../createPropertyTransform';
4
+ import { parseNodeToStaticValue } from './parseNodeToStaticValue';
5
+ export const handlePx2Rem = createPropertyTransform((node, context) => {
6
+ if (ts.isCallExpression(node) &&
7
+ ts.isIdentifier(node.expression) &&
8
+ node.expression.text === 'px2rem') {
9
+ const args = node.arguments.map(arg => parseNodeToStaticValue(arg, context));
10
+ return px2rem(parseFloat(args[0]), args[1] ? parseFloat(args[1]) : undefined);
11
+ }
12
+ return;
13
+ });
@@ -0,0 +1,3 @@
1
+ import { TransformerContext } from './types';
2
+ export declare function injectStyles({ styles, cache }: TransformerContext, fileName: string, hash: string, styleOutput: string): void;
3
+ //# sourceMappingURL=injectStyles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injectStyles.d.ts","sourceRoot":"","sources":["../../../../lib/utils/injectStyles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,SAAS,CAAC;AAE3C,wBAAgB,YAAY,CAC1B,EAAC,MAAM,EAAE,KAAK,EAAC,EAAE,kBAAkB,EACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,QAOpB"}
@@ -0,0 +1,7 @@
1
+ export function injectStyles({ styles, cache }, fileName, hash, styleOutput) {
2
+ if (!cache[hash]) {
3
+ styles[fileName] = styles[fileName] || [];
4
+ styles[fileName].push(styleOutput);
5
+ cache[hash] = styleOutput;
6
+ }
7
+ }