Skip to content

Add support for using .babelrc in the app root. #493

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

Merged
merged 7 commits into from
Dec 26, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,28 +458,20 @@ module.exports = {

### Customizing babel config

In order to extend our usage of `babel`, you can define a function that extends its config via `next.config.js`.

The following example config shows you how to use `babel-preset-stage-0` with your app.
In order to extend our usage of `babel`, you can simply define a `.babelrc` file in the root of your app.
If found, we'll use it. Here's an example `.babelrc` file:

```js
// This file is not going through babel transformation.
// So, we write it in vanilla JS
// (But you could use ES2015 features supported by your Node.js version)

module.exports = {
// config is the set of options we pass to our babel-loaders's query option
babel: function (config, { dev }) {
// Add the stage-0 preset.
// Make sure to use 'require.resolve' otherwise we won't be able to find it.
config.presets.push(require.resolve('babel-preset-stage-0'))

// Important: return the modified config
return config
}
{
"presets": [
"next/babel",
"stage-0"
],
}
```

Although it's not required, you will need to add `next/babel` as a preset. That's the default preset we use for all Next.js apps.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, we removed support for babel in next.config.js right? Otherwise, we also have to make a mention of that here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We removed that. Now there's no such option.


## Production deployment

To deploy, instead of running `next`, you probably want to build ahead of time. Therefore, building and starting are separate commands:
Expand Down
6 changes: 6 additions & 0 deletions examples/with-custom-babel-config/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"next/babel",
"stage-0"
],
}
3 changes: 3 additions & 0 deletions examples/with-custom-babel-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ This example features:

* An app using proposed [do expressions](https://babeljs.io/docs/plugins/transform-do-expressions/).
* It uses babel-preset-stage-0, which allows us to use above JavaScript feature.
* It uses '.babelrc' file in the app directory to add above preset.

> Most of the time, when writing a custom `.babelrc` file, you need to add `next/babel` as a preset.

## How to run it

Expand Down
15 changes: 0 additions & 15 deletions examples/with-custom-babel-config/next.config.js

This file was deleted.

13 changes: 6 additions & 7 deletions server/build/babel/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { resolve, join, dirname } from 'path'
import { existsSync } from 'fs'
import { readFile, writeFile } from 'mz/fs'
import { transform } from 'babel-core'
import chokidar from 'chokidar'
import mkdirp from 'mkdirp-then'
import getConfig from '../../config'

export default babel

async function babel (dir, { dev = false } = {}) {
dir = resolve(dir)
const config = getConfig('../')

let src
try {
Expand All @@ -23,14 +22,14 @@ async function babel (dir, { dev = false } = {}) {
}

let babelOptions = {
babelrc: false,
babelrc: true,
sourceMaps: dev ? 'inline' : false,
presets: [require.resolve('./preset')]
presets: []
}

if (config.babel) {
console.log('> Using "babel" config function defined in next.config.js.')
babelOptions = await config.babel(babelOptions, { dev })
const hasBabelRc = existsSync(join(dir, '.babelrc'))
if (!hasBabelRc) {
babelOptions.presets.push(require.resolve('./preset'))
}

const { code } = transform(src, babelOptions)
Expand Down
17 changes: 9 additions & 8 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve, join } from 'path'
import { createHash } from 'crypto'
import { existsSync } from 'fs'
import webpack from 'webpack'
import glob from 'glob-promise'
import WriteFilePlugin from 'write-file-webpack-plugin'
Expand Down Expand Up @@ -84,18 +85,18 @@ export default async function createCompiler (dir, { dev = false, quiet = false
)
}

let mainBabelOptions = {
babelrc: false,
const mainBabelOptions = {
babelrc: true,
cacheDirectory: true,
sourceMaps: dev ? 'both' : false,
presets: [
require.resolve('./babel/preset')
]
presets: []
}

if (config.babel) {
console.log('> Using "babel" config function defined in next.config.js.')
mainBabelOptions = await config.babel(mainBabelOptions, { dev })
const hasBabelRc = existsSync(join(dir, '.babelrc'))
if (hasBabelRc) {
console.log('> Using .babelrc defined in your app root')
} else {
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
}

const loaders = (dev ? [{
Expand Down