Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
9b956bf
init
akameco Apr 22, 2017
c1cbadb
Tweak
akameco Apr 22, 2017
de33080
Update .travis.yml
akameco Apr 22, 2017
9268bf6
0.1.0
akameco Apr 22, 2017
2dea3c2
Node version >= 6
akameco Apr 22, 2017
72a7742
0.1.1
akameco Apr 22, 2017
9cd37d7
require self
akameco Apr 25, 2017
9bb9dc0
Throws error when pattern not found
akameco Apr 25, 2017
dc0401a
0.2.0
akameco Apr 25, 2017
22288a6
Fix
akameco Apr 26, 2017
3c99ab2
Error .babelrc not found
akameco Apr 26, 2017
4b05169
0.2.1
akameco Apr 26, 2017
711195e
Use default value
akameco Apr 26, 2017
122eccd
0.3.0
akameco Apr 26, 2017
63dbf32
Add prettier
akameco May 16, 2017
c5443d0
Update
akameco May 17, 2017
9ea65ff
yarn
akameco May 17, 2017
9434e72
v0.3.1
akameco May 17, 2017
ba4d93e
Update yarn.lock
akameco Sep 10, 2017
d3ecd2f
Add .prettierrc
akameco Sep 10, 2017
9c6c262
Update deps
akameco Oct 1, 2017
3788f06
Consider env values when parsing babrlrc
nodaguti Dec 11, 2017
ad1bfca
Fix tests
nodaguti Dec 12, 2017
2af639a
Merge pull request #1 from nodaguti/consider-env
akameco Dec 12, 2017
058f8ab
chore: use eslint & update format & add flow
akameco Dec 12, 2017
d360d48
chore(all-contributor): add all-contributors
akameco Dec 12, 2017
51083f5
chore(all-contributors): add nodaguti as contributor
akameco Dec 12, 2017
47695aa
chore(npm): update prettier setting
akameco Dec 12, 2017
63d711d
ci(travis): update for travis
akameco Dec 12, 2017
4739afb
test(jest): use jest from ava
akameco Dec 12, 2017
215e40e
chore(lint): fix target
akameco Dec 12, 2017
7946de3
v0.4.0
akameco Dec 12, 2017
bd45481
chore(prettier): ignore covarage
akameco Dec 28, 2017
ca8b550
chore(npm): use babel-jest (#4)
akameco Dec 28, 2017
f17cc31
docs(readme): add badge
akameco Dec 28, 2017
16ac1f4
chore(package): fix lint-staged field
akameco Dec 28, 2017
b886938
chore(package): update dependencis
akameco Dec 29, 2017
d4331ad
chore(github): add TEMPLATE.md
akameco Dec 29, 2017
1128a6e
chore: add CODE_OF_CONDUCT (#5)
akameco Dec 29, 2017
9006a0e
fix: relative paths resolution inside babelrc (#3)
fix-fix Dec 29, 2017
4607d57
chore(all-contributors): add fix-fix as contributor
akameco Dec 29, 2017
0443bed
v0.5.0
akameco Dec 29, 2017
b4c1c3c
docs(PULL_REQUEST_TEMPLATE): update
akameco Dec 29, 2017
10945fb
chore(github): add TEMPLATE
akameco Dec 29, 2017
66dd2d0
fix(bug): path resolution of babel presets and plugins (#6)
fix-fix Jan 25, 2018
5ad4988
chore(npm): update packages
akameco Jan 25, 2018
2c5cebc
v0.5.1
akameco Jan 25, 2018
0dea213
Add an informative intro text.
enrique-ramirez Jan 31, 2018
59e840e
Adding myself to contributors table
enrique-ramirez Jan 31, 2018
3996a5e
Adding links and correcting my grammar.
enrique-ramirez Jan 31, 2018
c92c1be
This comma will probably break things.
enrique-ramirez Jan 31, 2018
df6a152
docs(readme): add contributor
akameco Jan 31, 2018
dbbd68b
Merge branch 'enrique-ramirez-improve-readme'
akameco Jan 31, 2018
9ef9ad9
chore(package): update dependencies
greenkeeper[bot] Feb 23, 2018
ed187bc
docs(readme): add Greenkeeper badge
greenkeeper[bot] Feb 23, 2018
4056ef3
docs(readme): update
akameco Feb 23, 2018
730676c
chore(npm): update
akameco Feb 23, 2018
40f8288
babel-plugin-react-intl as preset not plugin
bradbarrow Mar 1, 2018
5009b95
Add bradbarrow to contributors
bradbarrow Mar 1, 2018
9b76fbb
Merge pull request #10 from redbubble/plugin-priority
akameco Mar 1, 2018
9cce065
v0.5.2
akameco Mar 1, 2018
50e025b
chore(npm): update
akameco Mar 9, 2018
0e7fa1e
chore(npm): update dependencies
akameco Mar 17, 2018
cefe088
Merge pull request #13 from akameco/chore/update-0318
akameco Mar 18, 2018
0e98350
chore(npm): update
akameco May 28, 2018
15dc710
Merge pull request #14 from akameco/chore/npm-update
akameco May 28, 2018
c127525
chore: make babel-core a peer dependency
remcohaszing Jun 3, 2018
a01d625
docs(readme): document to install peer dependencies
remcohaszing Jun 4, 2018
33b5cc3
Merge pull request #15 from remcohaszing/support-babel-7
akameco Jun 4, 2018
89f75ff
chore(npm): update
akameco Jun 4, 2018
fe7fafd
v0.6.0
akameco Jun 4, 2018
440d6f3
chore(package): update dependencis
akameco Oct 12, 2018
e0b0874
feat: Drop node v6
akameco Oct 12, 2018
2d2d00c
refactor: use async/await
akameco Oct 12, 2018
d03ec1b
Merge pull request #16 from akameco/drop-node-6
akameco Oct 12, 2018
03dc2b5
v0.7.0
akameco Oct 12, 2018
e79f8b7
chore: update deps
akameco Jan 20, 2019
13165ea
fix(lint): fix eslint error
akameco Jan 20, 2019
449f6ae
Allow to specify custom module name passed to babel-plugin-react-intl
Jan 22, 2019
b73b8a5
refactor(deps): update babel7
akameco Jan 27, 2019
24f7972
Merge pull request #19 from akameco/refactor/update-deps
akameco Jan 27, 2019
0b4aaa0
v0.8.0
akameco Jan 27, 2019
5ce314a
chore(deps): udpate
akameco Jan 27, 2019
45e812b
v0.8.1
akameco Jan 27, 2019
1a5624e
Merge pull request #20 from Filson14/master
akameco Jan 28, 2019
186e59b
style(fmt): format code
akameco Jan 28, 2019
144c7d4
chore(deps): update
akameco Jan 28, 2019
db69862
docs(contributor): Add @Filson14 as a contributor
akameco Jan 28, 2019
1443b0b
v0.9.0
akameco Jan 28, 2019
64f6800
docs: tweak
akameco Jan 28, 2019
0b14928
Add 'extract-react-intl/' from commit '64f68003e86a0518f292a97c0d1199…
akameco Aug 8, 2019
d87cc44
refactor(extract-react-intl): migration
akameco Aug 8, 2019
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
30 changes: 30 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@
"contributions": [
"code"
]
},
{
"login": "nodaguti",
"name": "nodaguti",
"avatar_url": "https://avatars0.githubusercontent.com/u/27622?v=4",
"profile": "http://about.me/nodaguti",
"contributions": [
"code",
"test"
]
},
{
"login": "fix-fix",
"name": "fix-fix",
"avatar_url": "https://avatars1.githubusercontent.com/u/11943024?v=4",
"profile": "https://github.com/fix-fix",
"contributions": [
"code"
]
},
{
"login": "bradbarrow",
"name": "bradbarrow",
"avatar_url": "https://avatars3.githubusercontent.com/u/1264276?v=4",
"profile": "http://bradbarrow.com",
"contributions": [
"bug",
"code",
"test"
]
}
],
"repoType": "github"
Expand Down
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env", {
"targets": {
"node": "current"
}
}
],
"@babel/preset-flow"
]
}
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/test/fixtures/**
**/test/resolution/**
**/test/pluginOrdering/**
flow-typed
coverage
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* text=auto
*.js text eol=lf
*.lock binary
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package.json
**/test/fixtures/**
.github
package.json
97 changes: 97 additions & 0 deletions extract-react-intl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict'
const path = require('path')
const glob = require('glob')
const pify = require('pify')
const merge = require('lodash.merge')
const mergeWith = require('lodash.mergewith')
const { resolvePlugin, resolvePreset, transformFile } = require('@babel/core')
const readBabelrcUp = require('read-babelrc-up')

const localeMap = arr =>
arr.reduce((obj, x) => {
obj[x] = {}
return obj
}, {})

const concatArray = (obj, src) => {
if (Array.isArray(obj)) {
return obj.concat(src)
}
return undefined
}

const createResolveList = fn => (list, cwd) =>
list.map(x => (typeof x === 'string' ? fn(x, cwd) : x))

const resolvePresets = createResolveList(resolvePreset)
const resolvePlugins = createResolveList(resolvePlugin)

const getBabelrc = cwd => {
try {
const babelrc = readBabelrcUp.sync({ cwd }).babel
if (!babelrc.env) {
return babelrc
}

const env = process.env.BABEL_ENV || process.env.NODE_ENV || 'development'

return mergeWith(babelrc, babelrc.env[env], concatArray)
} catch (error) {
return { presets: [], plugins: [] }
}
}

const getBabelrcDir = cwd => path.dirname(readBabelrcUp.sync({ cwd }).path)

// eslint-disable-next-line max-lines-per-function
module.exports = async (locales, pattern, opts) => {
if (!Array.isArray(locales)) {
throw new TypeError(`Expected a Array, got ${typeof locales}`)
}

if (typeof pattern !== 'string') {
throw new TypeError(`Expected a string, got ${typeof pattern}`)
}

opts = {
cwd: process.cwd(),
defaultLocale: 'en',
...opts
}

const babelrc = getBabelrc(opts.cwd) || {}
const babelrcDir = getBabelrcDir(opts.cwd)

const { moduleSourceName } = opts
const pluginOptions = moduleSourceName ? { moduleSourceName } : {}

const { presets = [], plugins = [] } = babelrc

presets.unshift({
// eslint-disable-next-line global-require
plugins: [[require('babel-plugin-react-intl').default, pluginOptions]]
})

const extractFromFile = async file => {
const babelOpts = {
presets: resolvePresets(presets, babelrcDir),
plugins: resolvePlugins(plugins, babelrcDir)
}
const { metadata: result } = await pify(transformFile)(file, babelOpts)
const localeObj = localeMap(locales)
for (const { id, defaultMessage } of result['react-intl'].messages) {
for (const locale of locales) {
localeObj[locale][id] =
opts.defaultLocale === locale ? defaultMessage : ''
}
}
return localeObj
}

const files = await pify(glob)(pattern)
if (files.length === 0) {
throw new Error(`File not found (${pattern})`)
}
const arr = await Promise.all(files.map(extractFromFile))
return arr.reduce((h, obj) => merge(h, obj), localeMap(locales))
}
43 changes: 43 additions & 0 deletions extract-react-intl/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# extract-react-intl

## API

### extractReactIntl(locales, pattern, [options])

Return a `Promise` wrapped extracted messages.

#### locales

Type: `Array<string>`

Example: `['en', 'ja']`

#### pattern

Type: `string`

File path with glob.

#### options

Additional options.

#### defaultLocale

Type: `string`<br> Default: `en`

Set default locale for your app.

#### moduleSourceName

Type: `string`<br> Example: `./path/to/module` <br> Default: `react-intl`

The ES6 module source name of the React Intl package. Defines from where _defineMessages_, `<FormattedMessage />` and `<FormattedHTMLMessage />` are imported.

##### cwd

Type: `string`<br> Default: `.`

**You most likely don't need this.**

Change run path.
32 changes: 32 additions & 0 deletions extract-react-intl/test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`babel plugin execution order 1`] = `
Object {
"en": Object {
"extract-react-intl.test.pluginOrdering.test": "auto",
},
}
`;

exports[`babelrc path resolution 1`] = `
Object {
"en": Object {
"test": "test",
},
}
`;

exports[`extract from file 1`] = `
Object {
"en": Object {
"components.App.hello": "hello",
"components.App.world": "world",
"components/Greeting/welcome": "Welcome {name}, you have received {unreadCount, plural, =0 {no new messages} one {{formattedUnreadCount} new message} other {{formattedUnreadCount} new messages}} since {formattedLastLoginTime}.",
},
"ja": Object {
"components.App.hello": "",
"components.App.world": "",
"components/Greeting/welcome": "",
},
}
`;
32 changes: 32 additions & 0 deletions extract-react-intl/test/fixtures/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": [
"last 2 versions",
"safari >= 7"
]
}
}
],
"@babel/preset-react",
"@babel/preset-flow"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
],
"env": {
"react-intl": {
"plugins": [
[
"react-intl-auto",
{
"removePrefix": "extract-react-intl.test.fixtures"
}
]
]
}
}
}
22 changes: 22 additions & 0 deletions extract-react-intl/test/fixtures/components/App/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @flow
import React, { Component } from 'react'
import { FormattedMessage } from 'react-intl'
import Greeting from '../Greeting'
import messages from './messages'

export default class App extends Component {
render() {
const user = {
name: 'Eric',
unreadCount: 4,
lastLoginTime: Date.now() - 1000 * 60 * 60 * 24
}

return (
<div>
<FormattedMessage {...messages.hello} />
<Greeting user={user} />
</div>
)
}
}
7 changes: 7 additions & 0 deletions extract-react-intl/test/fixtures/components/App/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow
import { defineMessages } from 'react-intl'

export default defineMessages({
hello: 'hello',
world: 'world'
})
48 changes: 48 additions & 0 deletions extract-react-intl/test/fixtures/components/Greeting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// @flow
import React, { Component } from 'react'
import {
FormattedMessage,
FormattedNumber,
FormattedRelative
} from 'react-intl'
import messages from './messages'

type Props = {
user: {
name: string,
unreadCount: number,
lastLoginTime: number
}
}

export default class Greeting extends Component {
props: Props

render() {
const { user } = this.props

return (
<p>
<FormattedMessage
{...messages.welcome}
values={{
name: <b>{user.name}</b>,
unreadCount: user.unreadCount,
formattedUnreadCount: (
<b>
<FormattedNumber value={user.unreadCount} />
</b>
),
formattedLastLoginTime: (
<FormattedRelative value={user.lastLoginTime} />
)
}}
/>
</p>
)
}
}

function defaultMessage() {
return 'hello'
}
15 changes: 15 additions & 0 deletions extract-react-intl/test/fixtures/components/Greeting/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow
import { defineMessages } from 'react-intl'

export default defineMessages({
welcome: {
id: 'components/Greeting/welcome',
defaultMessage: `
Welcome {name}, you have received {unreadCount, plural,
=0 {no new messages}
one {{formattedUnreadCount} new message}
other {{formattedUnreadCount} new messages}
} since {formattedLastLoginTime}.
`
}
})
Loading