Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ const Component = () => (

- `MESSAGE_DIR: string`: The directory of output json files.

## Config

Use [cosmiconfig](https://www.npmjs.com/package/cosmiconfig) to set the `babel-plugin-macros` configuration: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md#config-experimental

### `verbose`: To disable the log messages

default: `true`

```js
// babel-plugin-macros.config.js
module.exports = {
'react-intl': {
verbose: false,
},
};
```

## Alternative

- https://github.com/yahoo/babel-plugin-react-intl
Expand Down
7 changes: 7 additions & 0 deletions babel-plugin-macros.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow

module.exports = {
'react-intl': {
verbose: true,
},
};
22 changes: 14 additions & 8 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'babel-flow-types';
// import printAST from 'ast-pretty-print';
import {
defaultConfig,
importReactIntl,
writeFileSync,
getMessages,
Expand All @@ -25,6 +26,7 @@ function reactIntlMacro({
},
},
babel: { types: t },
config: { verbose } = defaultConfig,
}: {
references: {
defineMessages: Array<BabelPath>,
Expand All @@ -33,6 +35,7 @@ function reactIntlMacro({
},
state: BabelPluginPass,
babel: Babel,
config: { verbose: boolean },
}): void {
const {
defineMessages = [],
Expand Down Expand Up @@ -62,21 +65,24 @@ function reactIntlMacro({
...FormattedMessage.map(getJSXMessage),
...FormattedHTMLMessage.map(getJSXMessage),
];

const sourceRelativedDir = path.relative(process.cwd(), filename);
const inputFilename = filename;
const outputFilename = path
.join(process.cwd(), MESSAGE_DIR, sourceRelativedDir)
.replace(/(js|jsx)$/g, 'json');

console.log(
`${path.relative(process.cwd(), inputFilename)} -> ${path.relative(
process.cwd(),
outputFilename,
)}`,
);
if (verbose) {
const inputFilename = filename;
console.log(
`${path.relative(process.cwd(), inputFilename)} -> ${path.relative(
process.cwd(),
outputFilename,
)}`,
);
}

writeFileSync(outputFilename, messages, state);
}
}

export default createMacro(reactIntlMacro);
export default createMacro(reactIntlMacro, { configName: 'react-intl' });
3 changes: 3 additions & 0 deletions src/utils/defaultConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow

export default { verbose: true };
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
export * from './ast-helper';

export { default as defaultConfig } from './defaultConfig';
export { default as getJSXMessage } from './getJSXMessage';
export { default as getMessages } from './getMessages';
export { default as importReactIntl } from './importReactIntl';
Expand Down