Skip to content

Commit 1cf4d14

Browse files
committed
Merge branch 'develop'
2 parents 8ac1461 + 5be14e5 commit 1cf4d14

File tree

10 files changed

+117
-55
lines changed

10 files changed

+117
-55
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
__snapshots__
33
.DS_Store
44
/lib
5+
.idea
6+
yarn.lock

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.4.0
4+
- Add support for directory-level flavors
5+
- Fix all the security warnings on libraries
6+
37
## 0.3.0
48
- Add `getFlavor` macro
59
- Fix the unmet dependency warnings on installing the library

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,31 @@ Here the the flavors-key is `layout-theme`. The current flavor being `green`.
4040

4141
If, for some reason, you cannot include the `.babel-plugin-macrosrc.json` file to your project, you can use any of the methods mentioned [here](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#config-experimental).
4242

43-
You can then use the flavor-key in any class by adding the macro-key as the last element, in the name of the import string or just before the extension. Also make sure to call the `flavor()` function. In the below example, the flavor-key `layout-theme`, configured above, is being used -
43+
You can then use the flavor-key in any class by adding the macro-key as the last element, in the name of the import string or just before the extension. You can use flavors in the directory-level as well. Just make sure to call the `flavor()` function, after all the import statements.
44+
45+
In the below example, the flavor-key `layout-theme`, configured above, is being used -
4446
```js
47+
// Import flavors on top
4548
import flavors from 'flavors.macro'
49+
4650
import Hello from './hello.layout-theme.js'
4751
import Bye from './bye.layout-theme'
52+
import Constants from './constants.layout-theme/theme'
53+
import Config from './layout-theme.config/config'
54+
import Theme from './layout-theme/theme'
4855
// ... other imports
4956

50-
// Add this right after all the imports are declared
57+
// Add this right after all the imports are declared (won't work otherwise)
5158
flavors();
5259
```
53-
When the application is built, the above class will evaluate to -
60+
When the application is built, the above statements will evaluate to -
5461

5562
```js
5663
import Hello from './hello.green.js'
5764
import Bye from './bye.green'
65+
import Constants from './constants.green/theme'
66+
import Config from './green.config/config'
67+
import Theme from './green/theme'
5868
// ... other imports
5969
```
6070

@@ -87,6 +97,11 @@ switch (currFlavor) {
8797
```
8898
`getFlavor` will return an empty string if the flavor-key is incorrect.
8999

100+
## Building and running locally
101+
- All the source-code is inside the `src` directory.
102+
- Ideally any change that's made needs to have a test. Running the tests - `npm test`.
103+
- Whenever testing out in the example-app, make sure to run `npm run-script build` in the project-root, since it'll be picking up from the local-env. NOTE: `lib` dir is git-ignored, so you'll not be able to see any-change tracked by git.
104+
- Deploy to npm using - `npm publish`
90105

91106
## License
92107
MIT. See license file

example-app/src/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import React, { Component } from 'react';
22
import Hello from './hello.layout-theme'
3+
import ContentLayout from './contents.layout-theme/content'
4+
import ContentStyled from './contents.style-theme/content'
35
import flavors from 'flavors.macro'
46
flavors();
57

68
class App extends Component {
79
render() {
810
return (
911
<div className="App">
12+
<ContentLayout />
13+
<ContentStyled />
1014
<Hello />
1115
</div>
1216
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { Component } from 'react';
2+
3+
class App extends Component {
4+
render() {
5+
return (
6+
<div>
7+
Content from inside the black flavor
8+
</div>
9+
);
10+
}
11+
}
12+
13+
export default App;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { Component } from 'react';
2+
3+
class App extends Component {
4+
render() {
5+
return (
6+
<div>
7+
Content from inside the green flavor
8+
</div>
9+
);
10+
}
11+
}
12+
13+
export default App;

package-lock.json

Lines changed: 30 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flavors.macro",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Macro for building different flavors of a react-app ",
55
"keywords": [
66
"babel-plugin-macros"

src/processor.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@ export default class Processor {
1616
importVal: importVal,
1717
}
1818

19+
if (Utils.isNull(importVal)) {
20+
return resp
21+
}
22+
1923
// Fetch the flavor-map
2024
var flavorMap = Processor.getFlavorMapForConfig(config)
2125

2226
// Import statement logic
2327
var isMatched = false;
2428
var replacementVal = null;
2529
var matchedKey = null;
30+
var matchedPathChar = null;
31+
2632
for (var flavorKey in flavorMap) {
2733
if (false === flavorMap.hasOwnProperty(flavorKey)) {
2834
continue
2935
}
3036

31-
var isDefaultRegex = new RegExp(`^.+\\.${Utils.escapeRegExp(flavorKey)}(?:\\.[^.]+)?$`)
32-
if (true === isDefaultRegex.test(importVal)) {
37+
var isDefaultRegex = new RegExp(`^.+([./])${Utils.escapeRegExp(flavorKey)}(?:[./].+)?$`)
38+
var regexResults = importVal.match(isDefaultRegex)
39+
if (false === Utils.isNull(regexResults) && regexResults.length >= 2) {
3340
isMatched = true;
3441
matchedKey = flavorKey
3542
replacementVal = flavorMap[matchedKey]
43+
matchedPathChar = regexResults[1]
3644
break;
3745
}
3846
}
@@ -50,9 +58,9 @@ export default class Processor {
5058
"" => abc.js
5159
*/
5260
if (replacementVal !== "") {
53-
replacementVal = `.${replacementVal}`
61+
replacementVal = `${matchedPathChar}${replacementVal}`
5462
}
55-
var defaultReplaceRegex = new RegExp(`\\.${Utils.escapeRegExp(matchedKey)}\\b`)
63+
var defaultReplaceRegex = new RegExp(`[./]${Utils.escapeRegExp(matchedKey)}\\b`)
5664
importVal = importVal.replace(defaultReplaceRegex, `${replacementVal}`);
5765

5866

tests/configs.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ it('Config test', () => {
116116
},
117117
[EXPECTED_MODIFIED_KEY]: false,
118118
},
119+
// Directory flavors
120+
{
121+
[IP_KEY]: "abc/styleFlavor/theme.js",
122+
[EXPECTED_KEY]: "abc/green/theme.js",
123+
[CONFIG_KEY]: flavorConfig,
124+
[EXPECTED_MODIFIED_KEY]: true,
125+
},
126+
{
127+
[IP_KEY]: "abc/constants.layoutFlavor/theme.js",
128+
[EXPECTED_KEY]: "abc/constants.red/theme.js",
129+
[CONFIG_KEY]: flavorConfig,
130+
[EXPECTED_MODIFIED_KEY]: true,
131+
},
132+
// The reversed-flavors should also work
133+
{
134+
[IP_KEY]: "abc/layoutFlavor.constants/theme.js",
135+
[EXPECTED_KEY]: "abc/red.constants/theme.js",
136+
[CONFIG_KEY]: flavorConfig,
137+
[EXPECTED_MODIFIED_KEY]: true,
138+
},
119139
]
120140

121141
ipList.forEach(entry => {

0 commit comments

Comments
 (0)