Skip to content

Commit

Permalink
fix: setBabelOptions (#7838)
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored and KyleAMathews committed Sep 3, 2018
1 parent 72b83ee commit 66101ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,9 @@ actions.replaceWebpackConfig = (config: Object, plugin?: ?Plugin = null) => {
* @param {Object} config An options object in the shape of a normal babelrc javascript object
* @example
* setBabelOptions({
* sourceMaps: `inline`,
* options: {
* sourceMaps: `inline`,
* }
* })
*/
actions.setBabelOptions = (options: Object, plugin?: ?Plugin = null) => {
Expand Down
15 changes: 14 additions & 1 deletion packages/gatsby/src/utils/babel-loader-helpers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const path = require(`path`)
const _ = require(`lodash`)

const prepareOptions = (babel, resolve = require.resolve) => {
const loadCachedConfig = () => {
let pluginBabelConfig = { test: { plugins: [], presets: [] } }
if (process.env.NODE_ENV !== `test`) {
pluginBabelConfig = require(path.join(
process.cwd(),
`./.cache/babelState.json`
))
}
return pluginBabelConfig
}

const getCustomOptions = () => {
const pluginBabelConfig = loadCachedConfig()
const stage = process.env.GATSBY_BUILD_STAGE || `test`
return pluginBabelConfig[stage].options
}

const prepareOptions = (babel, resolve = require.resolve) => {
let pluginBabelConfig = loadCachedConfig()

const stage = process.env.GATSBY_BUILD_STAGE || `test`

Expand Down Expand Up @@ -178,6 +189,8 @@ const mergeConfigItemOptions = ({ items, itemToMerge, type, babel }) => {
return items
}

exports.getCustomOptions = getCustomOptions

// Export helper functions for testing
exports.prepareOptions = prepareOptions
exports.mergeConfigItemOptions = mergeConfigItemOptions
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const babelLoader = require(`babel-loader`)

const {
prepareOptions,
getCustomOptions,
mergeConfigItemOptions,
} = require(`./babel-loader-helpers`)

Expand All @@ -28,6 +29,7 @@ module.exports = babelLoader.custom(babel => {
loader: {
cacheDirectory: true,
sourceType: `unambiguous`,
...getCustomOptions(),
...options,
},
}
Expand Down

0 comments on commit 66101ca

Please sign in to comment.