Skip to content

Commit

Permalink
fix: babel-plugin-react-intl error
Browse files Browse the repository at this point in the history
  • Loading branch information
akameco committed Nov 30, 2019
1 parent fb088f2 commit d3d9c43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/test/fixtures/**
.github
dist
package.json
13 changes: 5 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
/* eslint-disable no-console, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
'use strict'
const meow = require('meow')
const fn = require('./dist').default
Expand Down Expand Up @@ -48,17 +47,15 @@ const cli = meow(
default: false
},
// babel-plugin-react-intl boolean options
enforceDescriptions: {
extractSourceLocation: {
type: 'boolean',
default: false
},
enforceDefaultMessage: {
type: 'boolean',
default: true
removeDefaultMessage: {
type: 'boolean'
},
extractSourceLocation: {
type: 'boolean',
default: false
extractFromFormatMessageCall: {
type: 'boolean'
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"main": "dist/index.js",
"scripts": {
"add-contributor": "all-contributors add",
"fmt": "prettier --write '**/*.{json,js,md}'",
"fmt": "prettier --write '**/*.{ts,json,js,md}'",
"example": "./cli.js -l=en,ja -o example/i18n -d en --extractFromFormatMessageCall=true 'example/**/*.{js,tsx}'",
"prepublish": "npm run build",
"build": "tsc",
Expand Down Expand Up @@ -90,12 +90,12 @@
"typescript": "^3.6.2"
},
"lint-staged": {
"*.{js}": [
"*.{ts}": [
"prettier --write",
"eslint --fix",
"git add"
],
"*.{json,md}": [
"*.{js,json,md}": [
"prettier --write",
"git add"
]
Expand Down
24 changes: 23 additions & 1 deletion src/extract-react-intl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ const getBabelrc = (cwd: string) => {
const getBabelrcDir = (cwd: string) =>
path.dirname(readBabelrcUp.sync({ cwd }).path)

const babelPluginReactIntlOptions = [
'moduleSourceName',
'extractSourceLocation',
'messagesDir',
'overrideIdFn',
'removeDefaultMessage',
'extractFromFormatMessageCall',
'additionalComponentNames'
]

type Options = {
defaultLocale?: string
cwd?: string
Expand Down Expand Up @@ -91,7 +101,19 @@ export default async (
const presets = babelrc.presets || []
const plugins = babelrc.plugins || []

presets.unshift({ plugins: [[babelPluginReactIntl, pluginOptions]] })
presets.unshift({
plugins: [
[
babelPluginReactIntl,
Object.entries(pluginOptions).reduce((acc, [key, value]) => {
if (babelPluginReactIntlOptions.includes(key)) {
return { ...acc, [key]: value }
}
return acc
}, {})
]
]
})

const extractFromFile = async (file: string) => {
const babelOpts = {
Expand Down

0 comments on commit d3d9c43

Please sign in to comment.