You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-2Lines changed: 30 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,8 @@ $ yarn add flavors.macro
20
20
```
21
21
22
22
## Usage
23
+
24
+
### Flavors
23
25
A placholder for a flavor, called a flavor-key, needs to be configured. This key can be used in the import statements in the app. Against this key, flavors can be added.
24
26
25
27
Add the following into the `.babel-plugin-macrosrc.json` at the root of the project.
@@ -42,7 +44,7 @@ You can then use the flavor-key in any class by adding the macro-key as the last
42
44
```js
43
45
importflavorsfrom'flavors.macro'
44
46
importHellofrom'./hello.layout-theme.js'
45
-
importByefrom'./hello.layout-theme'
47
+
importByefrom'./bye.layout-theme'
46
48
// ... other imports
47
49
48
50
// Add this right after all the imports are declared
@@ -52,13 +54,39 @@ When the application is built, the above class will evaluate to -
52
54
53
55
```js
54
56
importHellofrom'./hello.green.js'
55
-
importByefrom'./hello.green'
57
+
importByefrom'./bye.green'
56
58
// ... other imports
57
59
```
58
60
59
61
NOTE: After adding/editing the configuration file(`.babel-plugin-macrosrc.json` or if any of the other equivalents being used), the npm server needs to be manually restarted.
60
62
61
63
Multiple flavor-keys can be added to the `flavorsMap`. If there are no flavor-keys, then a default key `defaultFlavor` is assumed, which will be replaced by an empty string.
62
64
65
+
### getFlavor
66
+
If the difference between flavors is really small/subtle or if there is already existing code, in which the flavors need to be created for a small portion of it, creating separate files for all of the different flavors can be cumbersome.
67
+
68
+
The `getFlavor()` macro call-expression can be used to fetch the flavor for the corresponding flavor-key.
69
+
70
+
So for the configuration from the above examples -
71
+
```js
72
+
import { getFlavor } from'flavors.macro'
73
+
74
+
// ... Rest of the code
75
+
var currFlavor =getFlavor("layout-theme")
76
+
switch (currFlavor) {
77
+
case"green":
78
+
console.log("Using green flavor")
79
+
break;
80
+
case"red":
81
+
console.log("Using red flavor")
82
+
break;
83
+
case"":
84
+
default:
85
+
console.log("No such flavor found")
86
+
}
87
+
```
88
+
`getFlavor` will return an empty string if the flavor-key is incorrect.
0 commit comments