@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.
- package/dist/commonjs/index.d.ts +14 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +28 -0
- package/dist/commonjs/lib/createObjectTransform.d.ts +10 -0
- package/dist/commonjs/lib/createObjectTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/createObjectTransform.js +14 -0
- package/dist/commonjs/lib/createPropertyTransform.d.ts +7 -0
- package/dist/commonjs/lib/createPropertyTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/createPropertyTransform.js +11 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts +16 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.js +45 -0
- package/dist/commonjs/lib/styleTransform.d.ts +23 -0
- package/dist/commonjs/lib/styleTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/styleTransform.js +131 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts +35 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.js +84 -0
- package/dist/commonjs/lib/utils/getCssVariables.d.ts +4 -0
- package/dist/commonjs/lib/utils/getCssVariables.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getCssVariables.js +31 -0
- package/dist/commonjs/lib/utils/getErrorMessage.d.ts +16 -0
- package/dist/commonjs/lib/utils/getErrorMessage.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getErrorMessage.js +45 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.d.ts +9 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.js +30 -0
- package/dist/commonjs/lib/utils/getHash.d.ts +4 -0
- package/dist/commonjs/lib/utils/getHash.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getHash.js +16 -0
- package/dist/commonjs/lib/utils/getVarName.d.ts +17 -0
- package/dist/commonjs/lib/utils/getVarName.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getVarName.js +34 -0
- package/dist/commonjs/lib/utils/handleCalc.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleCalc.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCalc.js +33 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.d.ts +7 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.js +309 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.js +86 -0
- package/dist/commonjs/lib/utils/handleCreateVars.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleCreateVars.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateVars.js +65 -0
- package/dist/commonjs/lib/utils/handleCssVar.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleCssVar.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCssVar.js +19 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.js +40 -0
- package/dist/commonjs/lib/utils/handleKeyframes.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleKeyframes.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleKeyframes.js +48 -0
- package/dist/commonjs/lib/utils/handleParentModifier.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleParentModifier.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleParentModifier.js +26 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.d.ts +2 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.js +19 -0
- package/dist/commonjs/lib/utils/injectStyles.d.ts +3 -0
- package/dist/commonjs/lib/utils/injectStyles.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/injectStyles.js +11 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts +7 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.js +24 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts +11 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.js +217 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts +15 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.js +168 -0
- package/dist/commonjs/lib/utils/stylisFns.d.ts +3 -0
- package/dist/commonjs/lib/utils/stylisFns.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/stylisFns.js +45 -0
- package/dist/commonjs/lib/utils/types.d.ts +210 -0
- package/dist/commonjs/lib/utils/types.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/types.js +2 -0
- package/dist/commonjs/lib/webpack-loader.d.ts +7 -0
- package/dist/commonjs/lib/webpack-loader.d.ts.map +1 -0
- package/dist/commonjs/lib/webpack-loader.js +21 -0
- package/dist/commonjs/lib/webpackPlugin.d.ts +31 -0
- package/dist/commonjs/lib/webpackPlugin.d.ts.map +1 -0
- package/dist/commonjs/lib/webpackPlugin.js +38 -0
- package/dist/commonjs/spec/createProgramFromSource.d.ts +8 -0
- package/dist/commonjs/spec/createProgramFromSource.d.ts.map +1 -0
- package/dist/commonjs/spec/createProgramFromSource.js +111 -0
- package/dist/commonjs/spec/findNodes.d.ts +3 -0
- package/dist/commonjs/spec/findNodes.d.ts.map +1 -0
- package/dist/commonjs/spec/findNodes.js +33 -0
- package/dist/commonjs/testing.d.ts +4 -0
- package/dist/commonjs/testing.d.ts.map +1 -0
- package/dist/commonjs/testing.js +11 -0
- package/dist/es6/index.d.ts +14 -0
- package/dist/es6/index.d.ts.map +1 -0
- package/dist/es6/index.js +14 -0
- package/dist/es6/lib/createObjectTransform.d.ts +10 -0
- package/dist/es6/lib/createObjectTransform.d.ts.map +1 -0
- package/dist/es6/lib/createObjectTransform.js +10 -0
- package/dist/es6/lib/createPropertyTransform.d.ts +7 -0
- package/dist/es6/lib/createPropertyTransform.d.ts.map +1 -0
- package/dist/es6/lib/createPropertyTransform.js +7 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.d.ts +16 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.js +36 -0
- package/dist/es6/lib/styleTransform.d.ts +23 -0
- package/dist/es6/lib/styleTransform.d.ts.map +1 -0
- package/dist/es6/lib/styleTransform.js +120 -0
- package/dist/es6/lib/utils/createStyleObjectNode.d.ts +35 -0
- package/dist/es6/lib/utils/createStyleObjectNode.d.ts.map +1 -0
- package/dist/es6/lib/utils/createStyleObjectNode.js +74 -0
- package/dist/es6/lib/utils/getCssVariables.d.ts +4 -0
- package/dist/es6/lib/utils/getCssVariables.d.ts.map +1 -0
- package/dist/es6/lib/utils/getCssVariables.js +26 -0
- package/dist/es6/lib/utils/getErrorMessage.d.ts +16 -0
- package/dist/es6/lib/utils/getErrorMessage.d.ts.map +1 -0
- package/dist/es6/lib/utils/getErrorMessage.js +41 -0
- package/dist/es6/lib/utils/getFallbackVariable.d.ts +9 -0
- package/dist/es6/lib/utils/getFallbackVariable.d.ts.map +1 -0
- package/dist/es6/lib/utils/getFallbackVariable.js +26 -0
- package/dist/es6/lib/utils/getHash.d.ts +4 -0
- package/dist/es6/lib/utils/getHash.d.ts.map +1 -0
- package/dist/es6/lib/utils/getHash.js +9 -0
- package/dist/es6/lib/utils/getVarName.d.ts +17 -0
- package/dist/es6/lib/utils/getVarName.d.ts.map +1 -0
- package/dist/es6/lib/utils/getVarName.js +27 -0
- package/dist/es6/lib/utils/handleCalc.d.ts +2 -0
- package/dist/es6/lib/utils/handleCalc.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCalc.js +27 -0
- package/dist/es6/lib/utils/handleCreateStencil.d.ts +7 -0
- package/dist/es6/lib/utils/handleCreateStencil.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateStencil.js +301 -0
- package/dist/es6/lib/utils/handleCreateStyles.d.ts +3 -0
- package/dist/es6/lib/utils/handleCreateStyles.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateStyles.js +79 -0
- package/dist/es6/lib/utils/handleCreateVars.d.ts +3 -0
- package/dist/es6/lib/utils/handleCreateVars.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateVars.js +58 -0
- package/dist/es6/lib/utils/handleCssVar.d.ts +2 -0
- package/dist/es6/lib/utils/handleCssVar.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCssVar.js +13 -0
- package/dist/es6/lib/utils/handleInjectGlobal.d.ts +3 -0
- package/dist/es6/lib/utils/handleInjectGlobal.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleInjectGlobal.js +33 -0
- package/dist/es6/lib/utils/handleKeyframes.d.ts +3 -0
- package/dist/es6/lib/utils/handleKeyframes.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleKeyframes.js +41 -0
- package/dist/es6/lib/utils/handleParentModifier.d.ts +2 -0
- package/dist/es6/lib/utils/handleParentModifier.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleParentModifier.js +20 -0
- package/dist/es6/lib/utils/handlePx2Rem.d.ts +2 -0
- package/dist/es6/lib/utils/handlePx2Rem.d.ts.map +1 -0
- package/dist/es6/lib/utils/handlePx2Rem.js +13 -0
- package/dist/es6/lib/utils/injectStyles.d.ts +3 -0
- package/dist/es6/lib/utils/injectStyles.d.ts.map +1 -0
- package/dist/es6/lib/utils/injectStyles.js +7 -0
- package/dist/es6/lib/utils/isImportedFromStyling.d.ts +7 -0
- package/dist/es6/lib/utils/isImportedFromStyling.d.ts.map +1 -0
- package/dist/es6/lib/utils/isImportedFromStyling.js +17 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts +11 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.js +209 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts +15 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.js +159 -0
- package/dist/es6/lib/utils/stylisFns.d.ts +3 -0
- package/dist/es6/lib/utils/stylisFns.d.ts.map +1 -0
- package/dist/es6/lib/utils/stylisFns.js +41 -0
- package/dist/es6/lib/utils/types.d.ts +210 -0
- package/dist/es6/lib/utils/types.d.ts.map +1 -0
- package/dist/es6/lib/utils/types.js +1 -0
- package/dist/es6/lib/webpack-loader.d.ts +7 -0
- package/dist/es6/lib/webpack-loader.d.ts.map +1 -0
- package/dist/es6/lib/webpack-loader.js +15 -0
- package/dist/es6/lib/webpackPlugin.d.ts +31 -0
- package/dist/es6/lib/webpackPlugin.d.ts.map +1 -0
- package/dist/es6/lib/webpackPlugin.js +31 -0
- package/dist/es6/spec/createProgramFromSource.d.ts +8 -0
- package/dist/es6/spec/createProgramFromSource.d.ts.map +1 -0
- package/dist/es6/spec/createProgramFromSource.js +81 -0
- package/dist/es6/spec/findNodes.d.ts +3 -0
- package/dist/es6/spec/findNodes.d.ts.map +1 -0
- package/dist/es6/spec/findNodes.js +26 -0
- package/dist/es6/testing.d.ts +4 -0
- package/dist/es6/testing.d.ts.map +1 -0
- package/dist/es6/testing.js +3 -0
- package/index.ts +19 -0
- package/lib/createObjectTransform.ts +12 -0
- package/lib/createPropertyTransform.ts +9 -0
- package/lib/createTypeScriptWatchProgram.ts +52 -0
- package/lib/styleTransform.ts +169 -0
- package/lib/utils/createStyleObjectNode.ts +105 -0
- package/lib/utils/getCssVariables.ts +36 -0
- package/lib/utils/getErrorMessage.ts +57 -0
- package/lib/utils/getFallbackVariable.ts +34 -0
- package/lib/utils/getHash.ts +13 -0
- package/lib/utils/getVarName.ts +32 -0
- package/lib/utils/handleCalc.ts +33 -0
- package/lib/utils/handleCreateStencil.ts +442 -0
- package/lib/utils/handleCreateStyles.ts +97 -0
- package/lib/utils/handleCreateVars.ts +90 -0
- package/lib/utils/handleCssVar.ts +19 -0
- package/lib/utils/handleInjectGlobal.ts +51 -0
- package/lib/utils/handleKeyframes.ts +60 -0
- package/lib/utils/handleParentModifier.ts +29 -0
- package/lib/utils/handlePx2Rem.ts +22 -0
- package/lib/utils/injectStyles.ts +14 -0
- package/lib/utils/isImportedFromStyling.ts +21 -0
- package/lib/utils/parseNodeToStaticValue.ts +281 -0
- package/lib/utils/parseObjectToStaticValue.ts +201 -0
- package/lib/utils/stylisFns.ts +56 -0
- package/lib/utils/types.ts +223 -0
- package/lib/webpack-loader.ts +28 -0
- package/lib/webpackPlugin.ts +69 -0
- package/package.json +19 -14
- 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.
|
|
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
|
-
"
|
|
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
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
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.
|
|
38
|
-
"
|
|
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
|
-
"
|
|
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
|
-
};
|