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

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,223 @@
1
+ import ts from 'typescript';
2
+
3
+ export type TransformerContext = {
4
+ checker: ts.TypeChecker;
5
+ prefix: string;
6
+ getPrefix: (path: string) => string;
7
+ /**
8
+ * Name and value pairs for things that cannot be statically resolved via the type system. The
9
+ * name is resolved via `getPropertyAccessExpressionText` and the value can be anything. It should
10
+ * be whatever you want the runtime value to be, which is usually a hash. For example:
11
+ *
12
+ * ```ts
13
+ * const myVars = createVars('color');
14
+ *
15
+ * color: myVars.color
16
+ * ```
17
+ *
18
+ * The type of `myVars.color` is `string`, but both in runtime and transpile time, the
19
+ * `myVars.color` is resolved to a hard value. The `names` cache is a lookup to that value. The
20
+ * `names` cache does not track the AST of `color`, so `const {color} = myVars` will not work. The
21
+ * key comes from `getPropertyAccessExpressionText` which replaces `.` with `-`.
22
+ */
23
+ names: Record<string, string>;
24
+ /**
25
+ * Extracted names is a mapping from `names` to a value that will be output to CSS. If you add an
26
+ * entry to `names`, add an entry to `extractedNames` where the `key` of `extractedNames` is the
27
+ * `value` of `names`.
28
+ *
29
+ * For example:
30
+ * ```ts
31
+ * // names
32
+ * { "myVars.foo": "--css-12345" },
33
+ * // extractedNames
34
+ * { "--css-12345": "--css-my-vars-foo" }
35
+ * ```
36
+ *
37
+ * The value in `names` will be used for transformation of TypeScript files into JavaScript files.
38
+ * The `extractedNames` will be used for the output CSS files.
39
+ */
40
+ extractedNames: Record<string, string>;
41
+ /**
42
+ * Variable scoping allows for a variable lookup to match a predefined scope. For example, in
43
+ * stencils, the `base` config can take a function with the locally defined variable names.
44
+ *
45
+ * ```ts
46
+ * const myStencil = createStencil({
47
+ * vars: {color: 'red'},
48
+ * base({color}) {
49
+ * return {
50
+ * [color]: 'blue'
51
+ * }
52
+ * }
53
+ * })
54
+ * ```
55
+ *
56
+ * In this case, a name lookup would fail because there's no global name named `color`. There's a
57
+ * global name named `my-color` which is the fully-qualified name. The stencil parser will add
58
+ * `my-` as a name scoping when processing style attributes for name lookup purposes.
59
+ */
60
+ nameScope?: string;
61
+ /**
62
+ * All styles will be collected here. These styles will then be flushed out to CSS files according
63
+ * to the `getFileName` function.
64
+ */
65
+ styles: StylesOutput;
66
+ /**
67
+ * The cache is for idempotent style inserts. Styles are hashed based on file name and position in
68
+ * the file.
69
+ */
70
+ cache: Record<string, string>;
71
+ getFileName: (path: string) => string;
72
+ objectTransforms: ObjectTransform[];
73
+ propertyTransforms: PropertyTransform[];
74
+ fileName: string;
75
+ transform: NodeTransformer;
76
+ extractCSS: boolean;
77
+ seed: string;
78
+ };
79
+
80
+ export type NestedStyleObject = {[key: string]: number | string | NestedStyleObject};
81
+
82
+ /**
83
+ * Transformer function type. A transformer will be called by the TypeScript AST transformer visitor
84
+ * from the bottom of the tree to the top (inside-out/leaf first, root last). If a transformer knows
85
+ * how to handle the AST node, a node should be returned. Even if no transformation is desired,
86
+ * returning a node shortcuts processing. The visitor will call all NodeTransformers until a match
87
+ * is met.
88
+ */
89
+ export type NodeTransformer = (node: ts.Node, context: TransformerContext) => ts.Node | void;
90
+
91
+ /**
92
+ * Used to collect styles. The format will be:
93
+ *
94
+ * ```js
95
+ * {
96
+ * '/absolute/file/name': [
97
+ * '.styles-1 { ... }'
98
+ * ]
99
+ * }
100
+ * ```
101
+ */
102
+ export type StylesOutput = Record<string, string[]>;
103
+
104
+ export interface Config {
105
+ /**
106
+ * The prefix for all CSS class names and variables. This should be something short and
107
+ * distinguish your CSS class names from others on the page.
108
+ */
109
+ prefix?: string;
110
+ /**
111
+ * If you wish to automatically include CSS variable fallbacks, you can provide paths to CSS files
112
+ * that contain CSS variables. Fallbacks are useful if you expect your CSS to be loaded in an
113
+ * environment that might not include these CSS files. An example might be Storybook or other dev
114
+ * environments where you want your CSS to look good even in these environments. Fallbacks make
115
+ * bigger files because fallbacks are embedded.
116
+ *
117
+ * For example:
118
+ * ```ts
119
+ * const myComponent = createStyles({
120
+ * color: '--primary-color-100'
121
+ * })
122
+ * ```
123
+ *
124
+ * If you provide a fallback file that defines `--primary-color-100`, the fallback will be
125
+ * inlined:
126
+ * ```css
127
+ * // without
128
+ * .my-component{color:var(--primary-color-100);}
129
+ *
130
+ * // with fallback
131
+ * .my-component{color:var(--primary-color-100,#0000ff);}
132
+ * ```
133
+ *
134
+ * The benefit is your CSS will work even without the files that define all your global CSS
135
+ * variables.
136
+ */
137
+ fallbackFiles?: string[];
138
+ /**
139
+ * Optional additional transforms. A transform takes an AST node and optionally returns a new AST
140
+ * node. This is useful if your source files use custom utility functions. The style
141
+ * transformation process must receive static strings while reading your source files. The base
142
+ * transformers handle all style utilities provided by `@workday/canvas-kit-styling`, but you may
143
+ * use more. The transformer makes use of TypeScript's type checker to extract a static types out
144
+ * of variables, but will throw errors if it finds a generic type like `string` or `number`. If
145
+ * you get these errors and the error points to a function, maybe you need an additional
146
+ * transformer to understand the code. Some simple examples from `@workday/canvas-kit-styling` are
147
+ * `px2rem` and `calc.*`. Those have special handlers that are allowed to return new AST nodes
148
+ * that can be further processed. You can return `NumericLiteral`, `StringLiteral`, or
149
+ * `TemplateExpression` AST nodes. You can see `handlePx2Rem` and `handleCalc` transformers as
150
+ * examples as well as associated tests.
151
+ *
152
+ * Transforms are called using the visitor pattern. The order is innermost to outermost of the
153
+ * AST. This means if you have a special utility function used to return a style property, it will
154
+ * be called before the `createStyles` or `createStencils` is called.
155
+ *
156
+ * https://ts-ast-viewer.com/#code/MYewdgzgLgBAtgTwMIjgB3AUzLAvDYAJ0wEMpMBlKBAG0wgAoBvAKBhjRIBMuBLMAOYAuGAMxQACtz6CGAcgAeNOQEoWAXxVA
157
+ * ```ts
158
+ * const myComponent = createStyles({
159
+ * padding: getPadding('xl')
160
+ * })
161
+ * ```
162
+ *
163
+ * In this example, your transform will be called with `'xl'`, `getPadding`, the `padding`
164
+ * PropertyAssignment, `createStyles`, then variable declarations for `myComponent`. You must
165
+ * create your own filters for each AST node so you only transform the correct nodes. You can
166
+ * visit ts-ast-viewer.com to see the AST and types. You can also use the factory creator to
167
+ * return new AST.
168
+ */
169
+ additionalTransforms?: NodeTransformer[];
170
+
171
+ /**
172
+ * Should the CSS be statically extracted into CSS files? If `true`, CSS files will be created
173
+ * according to the `getFileName` configuration. If `getFileName` is not defined, a CSS file will
174
+ * be created with the same name as the source file with a `.css` extension.
175
+ */
176
+ extractCSS?: boolean;
177
+
178
+ /**
179
+ * This will be called every time a style or keyframe needs to be injected into extracted style
180
+ * files. By default, the file name will be the current file with the `.tsx` replaced with a
181
+ * `.css`. If you wish to move or combine styles into a file, provide a custom `getFileName`. You
182
+ * can return the same file path for multiple sources. If the same file is used (maybe always
183
+ * returning `index.css`), new styles will be appended to the end of the file.
184
+ */
185
+ getFileName?: (path: string) => string;
186
+
187
+ /**
188
+ * Object transforms take an AST node that represents a style object and turn it into a
189
+ * `NestedStyleObject` or return `void`. If an object transform returns an object, transformation
190
+ * parsing stops on that node. If `void` is returned, processing continues using
191
+ * `parseObjectToStaticValue`. These transforms are useful if you have custom utility functions
192
+ * that are not statically parsable by the static styling transformer.
193
+ */
194
+ objectTransforms?: ObjectTransform[];
195
+
196
+ /**
197
+ * Property transforms take an AST node that represents a style property value and turn it into a
198
+ * static string. The transform is static and will generate a CSS style string for injection
199
+ * directly into the browser (or CSS file). A string is the only valid return type. Returning
200
+ * `void` instructs the transform to move on to other transforms. Returning a string will
201
+ * short-circuit other property transforms or the default property transform.
202
+ */
203
+ propertyTransforms?: PropertyTransform[];
204
+
205
+ /**
206
+ * Optional function for getting a prefix given a file path. This is useful if you want to have a
207
+ * different prefix for each module in a monorepo.
208
+ */
209
+ getPrefix?: (path: string) => string;
210
+
211
+ /**
212
+ * Optional seed for the hash. This can be useful if you want each build to have a different hash.
213
+ * If you provide the same seed, the same hash will be generated for every style.
214
+ */
215
+ seed?: string;
216
+ }
217
+
218
+ export type ObjectTransform = (
219
+ node: ts.Node,
220
+ context: TransformerContext
221
+ ) => NestedStyleObject | void;
222
+
223
+ export type PropertyTransform = (node: ts.Node, context: TransformerContext) => string | void;
@@ -0,0 +1,28 @@
1
+ import ts from 'typescript';
2
+
3
+ interface LoaderContext {
4
+ getOptions: () => ReturnType<import('./webpackPlugin').StylingWebpackPlugin['getLoaderOptions']>;
5
+ resourcePath: string;
6
+ }
7
+
8
+ export default function typescriptLoaderWithTransformers(this: LoaderContext, source: string) {
9
+ const {builderProgram, transformers, postTransform} = this.getOptions();
10
+ const id = this.resourcePath;
11
+
12
+ const program = builderProgram.getProgram();
13
+
14
+ const sourceFile = program.getSourceFile(id);
15
+ if (!sourceFile) {
16
+ return source;
17
+ }
18
+
19
+ const printer = ts.createPrinter(program.getCompilerOptions());
20
+
21
+ const transformed = printer.printFile(
22
+ ts
23
+ .transform(sourceFile, transformers, program.getCompilerOptions())
24
+ .transformed.find(s => s.fileName === id) || sourceFile
25
+ );
26
+
27
+ return postTransform?.(transformed, id) || transformed;
28
+ }
@@ -0,0 +1,69 @@
1
+ import ts from 'typescript';
2
+
3
+ import {getCompilerOptions, startWatch} from './createTypeScriptWatchProgram';
4
+ import styleTransformer from './styleTransform';
5
+
6
+ type Transformer = (program: ts.Program) => ts.TransformerFactory<ts.SourceFile>;
7
+
8
+ const pluginName = 'StylingWebpackPlugin';
9
+
10
+ interface PluginConfig {
11
+ tsconfigPath: string;
12
+ /**
13
+ * Optional list of transformers. The default is running the styleTransformer. If you wish to run
14
+ * additional transformers, you can pass them in here.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * new StylingWebpackPlugin({
19
+ * tsconfigPath: './tsconfig.json',
20
+ * transformers: [styleTransformer, myTransformer],
21
+ * })
22
+ * ```
23
+ */
24
+ transformers?: (undefined | Transformer)[];
25
+
26
+ postTransform?: (code: string, id: string) => string | undefined;
27
+ }
28
+
29
+ export class StylingWebpackPlugin {
30
+ private compilerOptions: ts.CompilerOptions;
31
+ public builderProgram!: ts.BuilderProgram;
32
+ public transformers!: ts.TransformerFactory<ts.SourceFile>[];
33
+ public postTransform?: (code: string, id: string) => string | undefined;
34
+ public close: () => void;
35
+
36
+ constructor(config: PluginConfig) {
37
+ const tsconfigPath = config.tsconfigPath ?? './tsconfig.json';
38
+
39
+ this.compilerOptions = getCompilerOptions(tsconfigPath);
40
+
41
+ const [_builderProgram, close] = startWatch(
42
+ this.compilerOptions,
43
+ tsconfigPath,
44
+ (builderProgram: ts.BuilderProgram) => {
45
+ this.builderProgram = builderProgram;
46
+ this.transformers = (config.transformers ?? [styleTransformer])
47
+ .filter(t => t !== undefined)
48
+ .map(t => t!(this.builderProgram.getProgram()));
49
+ }
50
+ );
51
+
52
+ this.close = close;
53
+ this.postTransform = config.postTransform;
54
+ }
55
+
56
+ getLoaderOptions() {
57
+ return this;
58
+ }
59
+
60
+ apply(compiler: import('webpack').Compiler) {
61
+ // We must close down the watch program when the compiler shuts down. This doesn't happen
62
+ // automatically when using the watch program and the watch will shut down from a SIGINT. When a
63
+ // webpack production build is initiated, the shutdown signifies the build is complete and we
64
+ // need to end the watch program.
65
+ compiler.hooks.shutdown.tap(pluginName, () => {
66
+ this.close();
67
+ });
68
+ }
69
+ }
package/package.json CHANGED
@@ -1,27 +1,31 @@
1
1
  {
2
2
  "name": "@workday/canvas-kit-styling-transform",
3
- "version": "13.2.1",
3
+ "version": "13.2.3",
4
4
  "description": "The custom CSS in JS solution that takes JS styles and turns them into static CSS",
5
5
  "author": "Workday, Inc. (https://www.workday.com)",
6
6
  "license": "Apache-2.0",
7
- "main": "index.js",
8
- "module": "index.js",
9
7
  "sideEffects": false,
10
- "types": "index.d.ts",
8
+ "main": "dist/commonjs/index.js",
9
+ "module": "dist/es6/index.js",
10
+ "types": "dist/es6/index.d.ts",
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "https://github.com/workday/canvas-kit.git",
14
14
  "directory": "modules/styling/parser"
15
15
  },
16
16
  "files": [
17
- "*/package.json",
18
- "*/lib/*",
19
- "*/index.ts",
20
- "dist/",
17
+ "package.json",
18
+ "lib/*",
19
+ "index.js",
20
+ "dist/*",
21
21
  "index.ts",
22
22
  "testing.ts"
23
23
  ],
24
24
  "scripts": {
25
+ "clean": "rimraf dist && rimraf .build-info && mkdirp dist",
26
+ "build:cjs": "tsc -p tsconfig.cjs.json",
27
+ "build:es6": "tsc -p tsconfig.es6.json",
28
+ "build": "npm-run-all build:cjs build:es6",
25
29
  "test": "echo \"Error: no test specified\" && exit 1"
26
30
  },
27
31
  "keywords": [
@@ -34,14 +38,15 @@
34
38
  ],
35
39
  "dependencies": {
36
40
  "@emotion/serialize": "^1.0.2",
37
- "@workday/canvas-kit-styling": "^13.2.1",
38
- "@workday/canvas-tokens-web": "^2.1.1",
39
- "stylis": "4.0.13",
40
- "ts-node": "^10.9.1",
41
- "typescript": "5.0"
41
+ "@workday/canvas-kit-styling": "^13.2.3",
42
+ "stylis": "4.0.13"
42
43
  },
43
44
  "devDependencies": {
45
+ "@types/common-tags": "^1.8.0",
44
46
  "common-tags": "^1.8.0"
45
47
  },
46
- "gitHead": "d601d517dbaced7a41f2f49d8312e4d7979dcea4"
48
+ "peerDependencies": {
49
+ "typescript": ">=5.0"
50
+ },
51
+ "gitHead": "4bf7deabfbac10c4b4ff7b706c54292c4bf79e35"
47
52
  }
package/index.js DELETED
@@ -1,22 +0,0 @@
1
- // allow transforms to use TypeScript without any additional configuration
2
- require('ts-node').register({
3
- /* options */
4
- });
5
-
6
- const myExport = require('./lib/styleTransform');
7
-
8
- myExport.parseNodeToStaticValue =
9
- require('./lib/utils/parseNodeToStaticValue').parseNodeToStaticValue;
10
- myExport.parseObjectToStaticValue =
11
- require('./lib/utils/parseObjectToStaticValue').parseObjectToStaticValue;
12
- myExport.createObjectTransform = require('./lib/createObjectTransform').createObjectTransform;
13
- myExport.createPropertyTransform = require('./lib/createPropertyTransform').createPropertyTransform;
14
- myExport.withDefaultContext = require('./lib/styleTransform').withDefaultContext;
15
- myExport.getClassName = require('./lib/utils/handleCreateStencil').getClassName;
16
- myExport.styleTransformer = myExport;
17
-
18
- module.exports = myExport;
19
-
20
- myExport.createConfig = function createConfig(config) {
21
- return config;
22
- };