Skip to content

Commit

Permalink
Use find-babel-config for the babel config lookup
Browse files Browse the repository at this point in the history
find-babel-config searches for the configuration in the closest .babelrc or
package.json file
  • Loading branch information
Tommy Leunen committed Aug 14, 2016
1 parent ccc591c commit 55fa78c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
*.log
.idea
npm-shrinkwrap.json
npm-shrinkwrap.json
.DS_Store
36 changes: 15 additions & 21 deletions dist/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ exports.default = function (configType, baseConfig, configDir) {

// Search for a .babelrc in the config directory, then the module root
// directory. If found, use that to extend webpack configurations.
var babelConfig = loadBabelConfig(_path2.default.resolve(configDir, '.babelrc'));

var _findBabelConfig$sync = _findBabelConfig2.default.sync(configDir, 0);

var babelConfig = _findBabelConfig$sync.config;

var inConfigDir = true;

if (!babelConfig) {
babelConfig = loadBabelConfig('.babelrc');
var _findBabelConfig$sync2 = _findBabelConfig2.default.sync('./', 0);

var babelConfigRoot = _findBabelConfig$sync2.config;

babelConfig = babelConfigRoot;
inConfigDir = false;
}

if (babelConfig) {
babelConfig = removeIncompatiblePresets(babelConfig);
// If the custom config uses babel's `extends` clause, then replace it with
// an absolute path. `extends` will not work unless we do this.
if (babelConfig.extends) {
Expand Down Expand Up @@ -92,9 +101,9 @@ var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _cjson = require('cjson');
var _findBabelConfig = require('find-babel-config');

var _cjson2 = _interopRequireDefault(_cjson);
var _findBabelConfig2 = _interopRequireDefault(_findBabelConfig);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

Expand All @@ -108,23 +117,8 @@ function removeReactHmre(presets) {
}
}

// Tries to load a .babelrc and returns the parsed object if successful
function loadBabelConfig(babelConfigPath) {
var config = void 0;
if (_fs2.default.existsSync(babelConfigPath)) {
var content = _fs2.default.readFileSync(babelConfigPath, 'utf-8');
try {
config = _cjson2.default.parse(content);
config.babelrc = false;
logger.info('=> Loading custom .babelrc');
} catch (e) {
logger.error('=> Error parsing .babelrc file: ' + e.message);
throw e;
}
}

if (!config) return null;

// Remove incomaptible babel presets
function removeIncompatiblePresets(config) {
// Remove react-hmre preset.
// It causes issues with react-storybook.
// We don't really need it.
Expand Down
Binary file removed docs/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"express": "^4.13.3",
"file-loader": "^0.9.0",
"json-loader": "^0.5.4",
"find-babel-config": "^1.0.0",
"json-stringify-safe": "^5.0.1",
"lodash.pick": "^4.2.0",
"postcss-loader": "0.9.1",
Expand Down
27 changes: 7 additions & 20 deletions src/server/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import cjson from 'cjson';
import findBabelConfig from 'find-babel-config';

// avoid ESLint errors
const logger = console;
Expand All @@ -12,23 +12,8 @@ function removeReactHmre(presets) {
}
}

// Tries to load a .babelrc and returns the parsed object if successful
function loadBabelConfig(babelConfigPath) {
let config;
if (fs.existsSync(babelConfigPath)) {
const content = fs.readFileSync(babelConfigPath, 'utf-8');
try {
config = cjson.parse(content);
config.babelrc = false;
logger.info('=> Loading custom .babelrc');
} catch (e) {
logger.error(`=> Error parsing .babelrc file: ${e.message}`);
throw e;
}
}

if (!config) return null;

// Remove incomaptible babel presets
function removeIncompatiblePresets(config) {
// Remove react-hmre preset.
// It causes issues with react-storybook.
// We don't really need it.
Expand All @@ -53,15 +38,17 @@ export default function (configType, baseConfig, configDir) {

// Search for a .babelrc in the config directory, then the module root
// directory. If found, use that to extend webpack configurations.
let babelConfig = loadBabelConfig(path.resolve(configDir, '.babelrc'));
let { config: babelConfig } = findBabelConfig.sync(configDir, 0);
let inConfigDir = true;

if (!babelConfig) {
babelConfig = loadBabelConfig('.babelrc');
const { config: babelConfigRoot } = findBabelConfig.sync('./', 0);
babelConfig = babelConfigRoot;
inConfigDir = false;
}

if (babelConfig) {
babelConfig = removeIncompatiblePresets(babelConfig);
// If the custom config uses babel's `extends` clause, then replace it with
// an absolute path. `extends` will not work unless we do this.
if (babelConfig.extends) {
Expand Down

0 comments on commit 55fa78c

Please sign in to comment.