Skip to content

Commit

Permalink
[v2] Don't error if .babelrc.js doesn't exist (#4068)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews authored Feb 16, 2018
1 parent bb258cb commit e28a1c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/babel-plugin-remove-graphql-queries/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-remove-graphql-queries",
"version": "2.0.0-alpha.f20ac0ed",
"version": "2.0.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
60 changes: 33 additions & 27 deletions packages/gatsby/src/utils/babel-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* @flow */

import fs from "fs"
import path from "path"
import json5 from "json5"
import _ from "lodash"
const fs = require("fs")
const path = require("path")
const json5 = require("json5")
const _ = require("lodash")
const report = require(`gatsby-cli/lib/reporter`)
import apiRunnerNode from "./api-runner-node"

const apiRunnerNode = require("./api-runner-node")
const testRequireError = require(`./test-require-error`).default

// TODO update this to store Babelrc config in Redux store.

Expand All @@ -15,11 +17,14 @@ import apiRunnerNode from "./api-runner-node"
* not parseable.
*/
function findBabelrc(directory) {
try {
const babelrc = fs.readFileSync(path.join(directory, `.babelrc`), `utf-8`)
return json5.parse(babelrc)
} catch (error) {
if (error.code !== `ENOENT`) throw error
const babelrcPath = path.join(directory, `.babelrc`)
if (fs.existsSync(babelrcPath)) {
try {
const babelrc = fs.readFileSync(babelrcPath, `utf-8`)
return json5.parse(babelrc)
} catch (error) {
throw error
}
}
return null
}
Expand All @@ -28,39 +33,40 @@ function findBabelrc(directory) {
* Locates a .babelrc.js in the Gatsby site root directory.
* requires it and unwraps any esm default export
*/
const preferDefault = m => (m && m.default) || m
function findBabelrcJs(directory, stage) {
let babelrc = null
const babelrcPath = path.join(directory, `.babelrc.js`)
try {
// $FlowFixMe
let babelrc = require(path.join(directory, `.babelrc.js`))
babelrc =
babelrc && babelrc.__esModule ? babelrc.default || undefined : babelrc

// TODO support this
if (typeof babelrc === `function`) {
report.error(
`.babelrc.js files that export a function are not supported in Gatsby`
)
return null
babelrc = preferDefault(require(babelrcPath))
} catch (error) {
if (!testRequireError(babelrcPath, error)) {
throw error
}
}

return babelrc
} catch (error) {
if (error.code !== `ENOENT`) throw error
// TODO support this
if (typeof babelrc === `function`) {
report.error(
`.babelrc.js files that export a function are not supported in Gatsby`
)
return null
}
return null

return babelrc
}

/**
* Reads the user's package.json and returns the "babel" section. It will
* return undefined when the "babel" section does not exist.
*/
function findBabelPackage(directory) {
const packageJson = require(path.join(directory, `package.json`))
try {
// $FlowFixMe - https://github.com/facebook/flow/issues/1975
const packageJson = require(path.join(directory, `package.json`))
return packageJson.babel
} catch (error) {
if (error.code === `MODULE_NOT_FOUND`) {
if (testRequireError(packageJson, error)) {
return null
} else {
throw error
Expand Down

0 comments on commit e28a1c8

Please sign in to comment.