New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use new babel API's to manage babel options #6801
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
24155bd
WIP: flesh out how the babel-loader might work
jquense 446a2eb
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews e814fd0
WIP
KyleAMathews c37459d
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews 8846126
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews ec7bc2b
Write out babel plugin info + stage for custom babel-loader
KyleAMathews 69afca8
cleanups
KyleAMathews ba147ee
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews 20b4855
Add missing await
KyleAMathews d79b0ce
Don't set babelrc to false since we check for `partialConfig.hasFiles…
KyleAMathews b5b69e8
Set env variable instead of writing out file
KyleAMathews 748dc9c
Remove old file for creating config
KyleAMathews 14922dd
Add back stage specific plugins
KyleAMathews 196dae8
Add required plugins/presets to babel config even if there's a file-d…
KyleAMathews 3333174
Update sample .babelrc people can copy into their site
KyleAMathews e1e1db5
Trim away more unused code
KyleAMathews bdca781
update snapshot
KyleAMathews 7693394
Add fallback plugins for when a .babelrc isn't defined + merge in gat…
KyleAMathews 6b27a5c
Comments
KyleAMathews 66de295
Add back plugin
KyleAMathews 99dedbf
Update snapshots
KyleAMathews 9883d64
update tests
KyleAMathews cef9f22
Add some docs
KyleAMathews ac1ea8f
test helper libs
KyleAMathews ee0523c
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews 5481a14
Fix lint errors
KyleAMathews e223d25
Remove json5 from gatsby's package.json as it's unused now
KyleAMathews f75d497
mock resolve
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 25 additions & 94 deletions
119
packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,28 @@ | ||
/* @flow */ | ||
|
||
const fs = require(`fs`) | ||
const path = require(`path`) | ||
const json5 = require(`json5`) | ||
const report = require(`gatsby-cli/lib/reporter`) | ||
const { actionifyBabelrc, addDefaultPluginsPresets } = require(`./utils`) | ||
const existsSync = require(`fs-exists-cached`).sync | ||
|
||
const testRequireError = require(`../../utils/test-require-error`).default | ||
|
||
/** | ||
* Locates a .babelrc in the Gatsby site root directory. Parses it using | ||
* json5 (what Babel uses). It throws an error if the users's .babelrc is | ||
* not parseable. | ||
*/ | ||
function findBabelrc(directory) { | ||
const babelrcPath = path.join(directory, `.babelrc`) | ||
if (existsSync(babelrcPath)) { | ||
try { | ||
const babelrc = fs.readFileSync(babelrcPath, `utf-8`) | ||
return json5.parse(babelrc) | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* 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 { | ||
babelrc = preferDefault(require(babelrcPath)) | ||
} catch (error) { | ||
if (!testRequireError(babelrcPath, error)) { | ||
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 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 | ||
return packageJson.babel | ||
} catch (error) { | ||
if (testRequireError(packageJson, error)) { | ||
return null | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Creates a normalized Babel config to use with babel-loader. Loads a local | ||
* babelrc config if one exists or sets a backup default. | ||
*/ | ||
exports.onCreateBabelConfig = ({ stage, store, actions }) => { | ||
const program = store.getState().program | ||
const { directory } = program | ||
|
||
let babelrc = | ||
findBabelrc(directory) || | ||
findBabelrcJs(directory) || | ||
findBabelPackage(directory) | ||
|
||
// If user doesn't have a custom babelrc, add defaults. | ||
if (babelrc) { | ||
actionifyBabelrc(babelrc, actions) | ||
} else { | ||
addDefaultPluginsPresets(actions, { | ||
stage, | ||
browserslist: program.browserslist, | ||
}) | ||
} | ||
const fs = require(`fs-extra`) | ||
|
||
const apiRunnerNode = require(`../../utils/api-runner-node`) | ||
const { withBasePath } = require(`../../utils/path`) | ||
|
||
exports.onPreBootstrap = async ({ store }) => { | ||
const { directory, browserslist } = store.getState().program | ||
const directoryPath = withBasePath(directory) | ||
|
||
await apiRunnerNode(`onCreateBabelConfig`, { | ||
stage: `develop`, | ||
}) | ||
await apiRunnerNode(`onCreateBabelConfig`, { | ||
stage: `develop-html`, | ||
}) | ||
await apiRunnerNode(`onCreateBabelConfig`, { | ||
stage: `build-javascript`, | ||
}) | ||
await apiRunnerNode(`onCreateBabelConfig`, { | ||
stage: `build-html`, | ||
}) | ||
const babelrcState = store.getState().babelrc | ||
babelrcState.browserslist = browserslist | ||
const babelState = JSON.stringify(babelrcState.stages, null, 4) | ||
await fs.writeFile(directoryPath(`.cache/babelState.json`), babelState) | ||
} |
106 changes: 0 additions & 106 deletions
106
packages/gatsby/src/internal-plugins/load-babel-config/utils.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like we can remove
json5
from package.json (this and tests you remove are only places where this is used)