Skip to content

Commit 8ac1461

Browse files
committed
Merge branch 'develop'
2 parents 90e57fd + 7e4eb1b commit 8ac1461

File tree

13 files changed

+334
-147
lines changed

13 files changed

+334
-147
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGE-LOG
22

3+
## 0.3.0
4+
- Add `getFlavor` macro
5+
- Fix the unmet dependency warnings on installing the library
6+
37
## 0.2.1
48
- Update readme
59
- Update description key in package.json

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ $ yarn add flavors.macro
2020
```
2121

2222
## Usage
23+
24+
### Flavors
2325
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.
2426

2527
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
4244
```js
4345
import flavors from 'flavors.macro'
4446
import Hello from './hello.layout-theme.js'
45-
import Bye from './hello.layout-theme'
47+
import Bye from './bye.layout-theme'
4648
// ... other imports
4749

4850
// Add this right after all the imports are declared
@@ -52,13 +54,39 @@ When the application is built, the above class will evaluate to -
5254

5355
```js
5456
import Hello from './hello.green.js'
55-
import Bye from './hello.green'
57+
import Bye from './bye.green'
5658
// ... other imports
5759
```
5860

5961
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.
6062

6163
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.
6264

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.
89+
90+
6391
## License
6492
MIT. See license file

example-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
}
3232
}
3333
}
34-
}
34+
}

example-app/src/hello.green.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react'
2+
import { getFlavor } from 'flavors.macro'
23

34
export default class Hello extends React.Component {
45
render() {
56
document.body.style.background = "#0F0";
67
return (
78
<div style={{ color: "#F00" }}>
8-
Hello green!
9+
Hello {getFlavor("layout-theme")}!
910
</div>
1011
)
1112
}

0 commit comments

Comments
 (0)