Skip to content

Commit

Permalink
Allow to provide custom module name/path from where imports will… (#61)
Browse files Browse the repository at this point in the history
Allow to provide custom module name/path from where imports will be resolved
  • Loading branch information
akameco authored Sep 5, 2019
2 parents 693024b + a831b87 commit 4c71529
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@
"code",
"test"
]
},
{
"login": "Filson14",
"name": "Filip \"Filson\" Pasternak",
"avatar_url": "https://avatars1.githubusercontent.com/u/4540538?v=4",
"profile": "https://github.com/Filson14",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Coverage Status](https://coveralls.io/repos/github/akameco/babel-plugin-react-intl-auto/badge.svg?branch=master)](https://coveralls.io/github/akameco/babel-plugin-react-intl-auto?branch=master)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors-)
[![babel-plugin-react-intl-auto Dev Token](https://badge.devtoken.rocks/babel-plugin-react-intl-auto)](https://devtoken.rocks/package/babel-plugin-react-intl-auto)

> i18n for the component age. Auto management react-intl ID.
Expand Down Expand Up @@ -267,6 +267,13 @@ Default: `false`

if `filebase` is `true`, generate id with filename.

#### moduleSourceName

Type: `string` <br>
Default: `react-intl`

if set, enables to use custom module as a source for _defineMessages_ etc.

#### includeExportName

Type: `boolean | 'all'` <br>
Expand Down Expand Up @@ -449,6 +456,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
</tr>
<tr>
<td align="center"><a href="https://github.com/dominik-zeglen"><img src="https://avatars3.githubusercontent.com/u/6833443?v=4" width="100px;" alt="Dominik Żegleń"/><br /><sub><b>Dominik Żegleń</b></sub></a><br /><a href="https://github.com/akameco/babel-plugin-react-intl-auto/commits?author=dominik-zeglen" title="Code">💻</a> <a href="https://github.com/akameco/babel-plugin-react-intl-auto/commits?author=dominik-zeglen" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Filson14"><img src="https://avatars1.githubusercontent.com/u/4540538?v=4" width="100px;" alt="Filip "Filson" Pasternak"/><br /><sub><b>Filip "Filson" Pasternak</b></sub></a><br /><a href="https://github.com/akameco/babel-plugin-react-intl-auto/commits?author=Filson14" title="Code">💻</a></td>
</tr>
</table>

Expand Down
37 changes: 37 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,43 @@ export default defineMessages({
`;
exports[`moduleSourceNameTest default: default 1`] = `
import { defineMessages } from 'react-intl'
export default defineMessages({
hello: 'hello',
})
↓ ↓ ↓ ↓ ↓ ↓
import { defineMessages } from 'react-intl';
export default defineMessages({
hello: 'hello'
});
`;
exports[`moduleSourceNameTest moduleSourceName: moduleSourceName 1`] = `
import { defineMessages } from 'gatsby-plugin-intl'
export default defineMessages({
hello: 'hello',
})
↓ ↓ ↓ ↓ ↓ ↓
import { defineMessages } from 'gatsby-plugin-intl';
export default defineMessages({
hello: {
"id": "src.__fixtures__.hello",
"defaultMessage": 'hello'
}
});
`;
exports[`removePrefix = "src" default: default 1`] = `
import { defineMessages } from 'react-intl'
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ export default defineMessages({
},
]

const moduleSourceNameTest = {
title: 'moduleSourceName',
code: `
import { defineMessages } from 'gatsby-plugin-intl'
export default defineMessages({
hello: 'hello',
})
`,
}

cases([
{ title: 'default', tests },
{
Expand Down Expand Up @@ -297,6 +308,13 @@ cases([
includeExportName: true,
},
},
{
title: 'moduleSourceNameTest',
tests: [defaultTest, moduleSourceNameTest],
pluginOptions: {
moduleSourceName: 'gatsby-plugin-intl',
},
},
])

function cases(
Expand All @@ -312,6 +330,7 @@ function cases(
babelOptions: { filename },
tests: [],
}
// eslint-disable-next-line no-unused-vars
for (const testCase of testCases) {
testCase.tests = testCase.tests.map(t => ({ ...t, title: t.title }))
pluginTester({ ...defaultOpts, ...testCase })
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { State } from './types'
const isImportLocalName = (
name: ?string,
allowedNames: $ReadOnlyArray<string>,
{ file }: State
{ file, opts: { moduleSourceName = 'react-intl' } }: State
) => {
const isSearchedImportSpecifier = specifier =>
specifier.isImportSpecifier() &&
Expand All @@ -21,7 +21,7 @@ const isImportLocalName = (
ImportDeclaration: {
exit(path) {
isImported =
path.node.source.value.includes('react-intl') &&
path.node.source.value.includes(moduleSourceName) &&
path.get('specifiers').some(isSearchedImportSpecifier)

if (isImported) {
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export type State = {
includeExportName?: boolean | 'all',
extractComments?: boolean,
useKey?: boolean,
moduleSourceName?: string,
},
}

0 comments on commit 4c71529

Please sign in to comment.