Skip to content

Commit

Permalink
fix: relative paths resolution inside babelrc (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fix-fix authored and akameco committed Dec 29, 2017
1 parent 1128a6e commit 9006a0e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const glob = require('glob')
const pify = require('pify')
const merge = require('lodash.merge')
const mergeWith = require('lodash.mergewith')
const { transformFile } = require('babel-core')
const { resolvePlugin, resolvePreset, transformFile } = require('babel-core')
const readBabelrcUp = require('read-babelrc-up')

const localeMap = arr =>
Expand All @@ -20,10 +20,15 @@ const concatArray = (obj, 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
}
Expand All @@ -36,6 +41,8 @@ const getBabelrc = cwd => {
}
}

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

module.exports = (locales, pattern, opts) => {
if (!Array.isArray(locales)) {
Promise.reject(new TypeError(`Expected a Array, got ${typeof locales}`))
Expand All @@ -54,6 +61,7 @@ module.exports = (locales, pattern, opts) => {
)

const babelrc = getBabelrc(opts.cwd) || {}
const babelrcPath = getBabelrcPath(opts.cwd)

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

Expand All @@ -62,8 +70,8 @@ module.exports = (locales, pattern, opts) => {

const extractFromFile = file => {
return pify(transformFile)(file, {
presets,
plugins
presets: resolvePresets(presets, babelrcPath),
plugins: resolvePlugins(plugins, babelrcPath)
}).then(({ metadata: result }) => {
const localeObj = localeMap(locales)
for (const { id, defaultMessage } of result['react-intl'].messages) {
Expand Down

0 comments on commit 9006a0e

Please sign in to comment.