diff --git a/cli.js b/cli.js index a1198cf..8281d9d 100755 --- a/cli.js +++ b/cli.js @@ -16,6 +16,7 @@ const cli = meow( -d, --default-locale default locale --flat json [default: true] | yaml [default: false] --delimiter json | yaml [default: .] + --module-name module source name from where components are imported Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -46,6 +47,9 @@ const cli = meow( delimiter: { type: 'string', default: '.' + }, + 'module-name': { + type: 'string' } } } diff --git a/index.js b/index.js index 19b5f58..6bb787f 100644 --- a/index.js +++ b/index.js @@ -80,15 +80,23 @@ module.exports = async (locales, pattern, buildDir, opts) => { const ext = isJson(opts.format) ? 'json' : 'yml' - const { defaultLocale } = opts + const { defaultLocale, moduleName } = opts const delimiter = opts.delimiter ? opts.delimiter : '.' const oldLocaleMaps = loadLocaleFiles(locales, buildDir, ext, delimiter) - const newLocaleMaps = await extractReactIntl(locales, pattern, { - defaultLocale - }) + const extractorOptions = { defaultLocale } + + if (moduleName) { + extractorOptions.moduleSourceName = moduleName + } + + const newLocaleMaps = await extractReactIntl( + locales, + pattern, + extractorOptions + ) return Promise.all( locales.map(locale => { diff --git a/package.json b/package.json index 77c49e2..5360105 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "messages" ], "dependencies": { - "extract-react-intl": "^0.8.1", + "extract-react-intl": "^0.9.0", "flat": "^4.1.0", "js-yaml": "^3.12.1", "load-json-file": "^5.1.0", diff --git a/readme.md b/readme.md index 7edf550..67937af 100644 --- a/readme.md +++ b/readme.md @@ -93,6 +93,7 @@ $ extract-messages --help --flat json [default: true] | yaml [default: false] --default-locale default locale [default: en] --delimiter json | yaml [default: .] + --module-name module source name from where components are imported [default: react-intl] Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -163,6 +164,13 @@ If format is `yaml`, set to `false`. Be careful if `false`. See [this issue](https://github.com/akameco/extract-react-intl-messages/issues/3). +##### moduleName + +Type: `string`
+Default: `react-intl` + +Set from where _defineMessages_, `` and `` are imported. + ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): diff --git a/test/fixtures/custom/a/messages.js b/test/fixtures/custom/a/messages.js new file mode 100644 index 0000000..fd212ce --- /dev/null +++ b/test/fixtures/custom/a/messages.js @@ -0,0 +1,12 @@ +import { defineMessages } from '../i18n' + +export default defineMessages({ + hello: { + id: 'a.custom.hello', + defaultMessage: 'hello' + }, + world: { + id: 'a.custom.world', + defaultMessage: 'world' + } +}) diff --git a/test/fixtures/custom/b/messages.js b/test/fixtures/custom/b/messages.js new file mode 100644 index 0000000..a802b6d --- /dev/null +++ b/test/fixtures/custom/b/messages.js @@ -0,0 +1,8 @@ +import { defineMessages } from '../i18n' + +export default defineMessages({ + hello: { + id: 'b.custom.message', + defaultMessage: 'Message' + } +}) diff --git a/test/fixtures/custom/i18n.js b/test/fixtures/custom/i18n.js new file mode 100644 index 0000000..121ca9c --- /dev/null +++ b/test/fixtures/custom/i18n.js @@ -0,0 +1 @@ +export { defineMessages } from 'react-intl' diff --git a/test/json/snapshots/test.js.md b/test/json/snapshots/test.js.md index a12a44b..06238c0 100644 --- a/test/json/snapshots/test.js.md +++ b/test/json/snapshots/test.js.md @@ -132,4 +132,39 @@ Generated by [AVA](https://ava.li). 'c.hello', 'y.hello', 'z.hello', - ] + +## export using custom module + +> Snapshot 1 + + { + 'a.custom.hello': 'hello', + 'a.custom.world': 'world', + 'b.custom.message': 'Message', + } + +> Snapshot 2 + + { + 'a.custom.hello': '', + 'a.custom.world': '', + 'b.custom.message': '', + } + +## export using custom module + +> Snapshot 1 + + { + 'a.custom.hello': 'hello', + 'a.custom.world': 'world', + 'b.custom.message': 'Message', + } + +> Snapshot 2 + + { + 'a.custom.hello': '', + 'a.custom.world': '', + 'b.custom.message': '', + } diff --git a/test/json/snapshots/test.js.snap b/test/json/snapshots/test.js.snap index bd65ebc..9d364ac 100644 Binary files a/test/json/snapshots/test.js.snap and b/test/json/snapshots/test.js.snap differ diff --git a/test/json/test.js b/test/json/test.js index 6b3ce9e..76cdbb3 100644 --- a/test/json/test.js +++ b/test/json/test.js @@ -64,3 +64,13 @@ test('delimiter - nest', async t => { t.snapshot(en) t.snapshot(ja) }) + +test('export using custom module', async t => { + const tmp = tempy.directory() + const opts = { moduleName: '../i18n' } + await m(['en', 'ja'], 'test/fixtures/custom/**/*.js', tmp, opts) + const en = JSON.parse(fs.readFileSync(path.resolve(tmp, 'en.json'), 'utf8')) + const ja = JSON.parse(fs.readFileSync(path.resolve(tmp, 'ja.json'), 'utf8')) + t.snapshot(en) + t.snapshot(ja) +}) diff --git a/yarn.lock b/yarn.lock index f2d899d..2b4b3d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2021,10 +2021,10 @@ extglob@^2.0.4: snapdragon "^0.8.1" to-regex "^3.0.1" -extract-react-intl@^0.8.1: - version "0.8.1" - resolved "https://registry.yarnpkg.com/extract-react-intl/-/extract-react-intl-0.8.1.tgz#09a585e4c3bd816be993e24f8d77945356b8a8e6" - integrity sha512-NsU2pqsxUtYKG4EPaYHIwW1EJTSSd9gmqEjBwbJeq5PNkPJmOui5wp7KzH56MldvopEJp7bKx0UX98L1hkXJIg== +extract-react-intl@^0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/extract-react-intl/-/extract-react-intl-0.9.0.tgz#7eb9de48d222d483eefd4975afc965a1a43e667a" + integrity sha512-O/AqANf7QIXlZPvXeuBr8aXbK/sFMZ0gn26YcRXUSsBaSSFHEvXVrLlQO6nGTAOOVghLoVRS52im5ylPbpdD6g== dependencies: "@babel/core" "^7.0.0" babel-plugin-react-intl "^3.0.1"