Skip to content
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 28 commits into from Aug 16, 2018
Merged
Show file tree
Hide file tree
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 Jul 27, 2018
446a2eb
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews Aug 9, 2018
e814fd0
WIP
KyleAMathews Aug 9, 2018
c37459d
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews Aug 9, 2018
8846126
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews Aug 10, 2018
ec7bc2b
Write out babel plugin info + stage for custom babel-loader
KyleAMathews Aug 10, 2018
69afca8
cleanups
KyleAMathews Aug 10, 2018
ba147ee
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews Aug 14, 2018
20b4855
Add missing await
KyleAMathews Aug 14, 2018
d79b0ce
Don't set babelrc to false since we check for `partialConfig.hasFiles…
KyleAMathews Aug 14, 2018
b5b69e8
Set env variable instead of writing out file
KyleAMathews Aug 14, 2018
748dc9c
Remove old file for creating config
KyleAMathews Aug 14, 2018
14922dd
Add back stage specific plugins
KyleAMathews Aug 14, 2018
196dae8
Add required plugins/presets to babel config even if there's a file-d…
KyleAMathews Aug 14, 2018
3333174
Update sample .babelrc people can copy into their site
KyleAMathews Aug 14, 2018
e1e1db5
Trim away more unused code
KyleAMathews Aug 14, 2018
bdca781
update snapshot
KyleAMathews Aug 14, 2018
7693394
Add fallback plugins for when a .babelrc isn't defined + merge in gat…
KyleAMathews Aug 15, 2018
6b27a5c
Comments
KyleAMathews Aug 15, 2018
66de295
Add back plugin
KyleAMathews Aug 15, 2018
99dedbf
Update snapshots
KyleAMathews Aug 15, 2018
9883d64
update tests
KyleAMathews Aug 16, 2018
cef9f22
Add some docs
KyleAMathews Aug 16, 2018
ac1ea8f
test helper libs
KyleAMathews Aug 16, 2018
ee0523c
Merge remote-tracking branch 'origin/master' into babel-api
KyleAMathews Aug 16, 2018
5481a14
Fix lint errors
KyleAMathews Aug 16, 2018
e223d25
Remove json5 from gatsby's package.json as it's unused now
KyleAMathews Aug 16, 2018
f75d497
mock resolve
KyleAMathews Aug 16, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/docs/babel.md
Expand Up @@ -27,8 +27,6 @@ to root of your site and modifying it per your needs.

```json5
{
cacheDirectory: true,
babelrc: false,
presets: [
[
"@babel/preset-env",
Expand Down Expand Up @@ -59,6 +57,7 @@ to root of your site and modifying it per your needs.
},
],
"@babel/plugin-syntax-dynamic-import",
"babel-plugin-macros",
[
"@babel/plugin-transform-runtime",
{
Expand Down
46 changes: 0 additions & 46 deletions flow-typed/npm/json5_vx.x.x.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/gatsby/package.json
Expand Up @@ -84,7 +84,6 @@
"joi": "12.x.x",
"json-loader": "^0.5.7",
"json-stringify-safe": "^5.0.1",
"json5": "^0.5.0",
"kebab-hash": "^0.1.2",
"lodash": "^4.17.4",
"lodash-id": "^0.14.0",
Expand Down
Expand Up @@ -21,7 +21,7 @@ Array [
"id": "Plugin load-babel-config",
"name": "load-babel-config",
"nodeAPIs": Array [
"onCreateBabelConfig",
"onPreBootstrap",
],
"pluginOptions": Object {
"plugins": Array [],
Expand Down Expand Up @@ -125,7 +125,7 @@ Array [
"id": "Plugin load-babel-config",
"name": "load-babel-config",
"nodeAPIs": Array [
"onCreateBabelConfig",
"onPreBootstrap",
],
"pluginOptions": Object {
"plugins": Array [],
Expand Down
8 changes: 1 addition & 7 deletions packages/gatsby/src/commands/repl.js
@@ -1,13 +1,7 @@
const repl = require(`repl`)
const { graphql } = require(`graphql`)
const bootstrap = require(`../bootstrap`)
const {
store,
loadNodeContent,
getNodes,
getNode,
hasNodeChanged,
} = require(`../redux`)
const { store, loadNodeContent, getNodes, getNode } = require(`../redux`)

module.exports = async program => {
// run bootstrap
Expand Down
119 changes: 25 additions & 94 deletions packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js
@@ -1,97 +1,28 @@
/* @flow */

const fs = require(`fs`)
const path = require(`path`)
const json5 = require(`json5`)
Copy link
Contributor

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)

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 packages/gatsby/src/internal-plugins/load-babel-config/utils.js

This file was deleted.