Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gnithin committed Feb 27, 2019
2 parents 702c257 + 0d801f5 commit 161de17
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGE-LOG

## 0.2.0
- Add all extensions support
- Update readme

## 0.1.0
- Add the basic working version where the macro replacement occurs
- Add some basic tests
Expand Down
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,57 @@

[![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square)](https://github.com/kentcdodds/babel-plugin-macros)

Macro for buiding different flavors of an app by manipulating import headers in React.
Macro for buiding different flavors of an app by manipulating import headers. Built and tested in apps made with `create-react-app`.

## Description
`flavors.macro` is a `babel-plugin-macros`, which allows us to build different flavors/themes of a react-application.
`flavors.macro` is a `babel-plugin-macros`, which will build different flavors of a react-application. This

## Installation

```
```bash
# NPM
npm install --save-dev flavors.macro
$ npm install --save-dev flavors.macro

# Yarn
yarn add flavors.macro
$ yarn add flavors.macro
```

## Usage
Add the following into your app's `package.json`
Add the following into the `.babel-plugin-macrosrc.json` at the root of the project.
```json
{
// ...
"babelMacros": {
"flavorsConfig": {
"flavorsMap": {
// Specify all the types of keys here
"layout-theme": "green",
}
}
}
}
```

Then you can use it in the file
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).

You can then use the flavor-key any class by adding the macro-key as the last element, in the name of the import string or just before the extension. In the below example, the flavor-key `layout-theme` is being used -
```js
import flavors from 'flavors.macro'
import Hello from './hello.layout-theme'
import Hello from './hello.layout-theme.js'
import Bye from './hello.layout-theme'
// ... other imports

// Add this right after all the imports are declared
flavors();
```

which will be built to -
which will evaluate to -

```js
import Hello from './hello.green'
import Hello from './hello.green.js'
import Bye from './hello.green'
// ... other imports
```

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.

## License
MIT. See license file
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flavors.macro",
"version": "0.1.0",
"description": "Macro for buiding different flavors of an app by manipulating import headers in React",
"version": "0.2.0",
"description": "Macro for building different flavors of an app by manipulating import headers in React",
"keywords": [
"babel-plugin-macros"
],
Expand All @@ -14,6 +14,10 @@
"build": "node_modules/@babel/cli/bin/babel.js src -d lib"
},
"author": "Nithin Gangadharan <nithin.linkin@gmail.com> (https://github.com/gnithin)",
"repository": {
"type": "git",
"url": "https://github.com/gnithin/flavors.macro"
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
2 changes: 1 addition & 1 deletion src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Processor {
continue
}

var isDefaultRegex = new RegExp(`^.+\\.${Utils.escapeRegExp(flavorKey)}(\\.js)?$`)
var isDefaultRegex = new RegExp(`^.+\\.${Utils.escapeRegExp(flavorKey)}(?:\\.[^.]+)?$`)
if (true === isDefaultRegex.test(importVal)) {
isMatched = true;
matchedKey = flavorKey
Expand Down
5 changes: 5 additions & 0 deletions tests/configs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ it('Empty config test', () => {
[EXPECTED_KEY]: "abc.DEFAULT.js",
[EXPECTED_MODIFIED_KEY]: false,
},
{
[IP_KEY]: "abc.defaultFlavor.css",
[EXPECTED_KEY]: "abc.css",
[EXPECTED_MODIFIED_KEY]: true,
},
{
[IP_KEY]: "abc",
[EXPECTED_KEY]: "abc",
Expand Down

0 comments on commit 161de17

Please sign in to comment.