Skip to content

Commit

Permalink
FIX 6864 - improve the loading of babel config (#6878)
Browse files Browse the repository at this point in the history
FIX 6864 - improve the loading of babel config
  • Loading branch information
shilman committed May 27, 2019
2 parents 6caf544 + de47483 commit 638c48e
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/core/src/server/utils/load-custom-babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@ function removeReactHmre(presets) {
// Tries to load a .babelrc and returns the parsed object if successful
function loadFromPath(babelConfigPath) {
let config;
const error = {};

if (fs.existsSync(babelConfigPath)) {
const content = fs.readFileSync(babelConfigPath, 'utf-8');

try {
// eslint-disable-next-line global-require, import/no-dynamic-require
config = require(babelConfigPath);
logger.info('=> Loading custom babel config as JS');
} catch (e) {
error.js = e;
}
try {
config = /^module.exports/.test(content)
? require(babelConfigPath) // eslint-disable-line
: JSON5.parse(content);
config.babelrc = false;
config = JSON5.parse(content);
logger.info('=> Loading custom babel config');
} catch (e) {
logger.error(`=> Error parsing babel config file: ${e.message}`);
throw e;
error.json = e;
}

if (!config) {
logger.error(`=> Error parsing babel config file: ${babelConfigPath}
We tried both loading as JS & JSON, neither worked.
Maybe there's a syntax error in the file?`);
logger.error(`=> From JS loading we got: ${error.js.message}`);
logger.error(`=> From JSON loading we got: ${error.js.message}`);

throw error.js;
}

config.babelrc = false;
}

if (!config) return null;
Expand Down

0 comments on commit 638c48e

Please sign in to comment.