提交 ac2a7187 编写于 作者: A Arunoda Susiripala 提交者: Guillermo Rauch

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

* Add support for using .babelrc in the app root.

* Update the README about the .babelrc usage.

* Fix a typo.

* Remove additional example usage.
上级 ad0f0a4d
...@@ -455,29 +455,19 @@ module.exports = { ...@@ -455,29 +455,19 @@ module.exports = {
### Customizing babel config ### Customizing babel config
In order to extend our usage of `babel`, you can define a function that extends its config via `next.config.js`. 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:
The following example config shows you how to use `babel-preset-stage-0` with your app.
```js ```js
// This file is not going through babel transformation. {
// So, we write it in vanilla JS "presets": [
// (But you could use ES2015 features supported by your Node.js version) "next/babel",
"stage-0"
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
}
} }
``` ```
**Note:** Next.js uses Babel 6 out of the box, which doesn't support default `module.exports`. Every time you export a component, make sure you use the ES6 `export` syntax instead. 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.
## Production deployment ## Production deployment
......
{
"presets": [
"next/babel",
"stage-0"
],
}
...@@ -26,6 +26,9 @@ This example features: ...@@ -26,6 +26,9 @@ This example features:
* An app using proposed [do expressions](https://babeljs.io/docs/plugins/transform-do-expressions/). * 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 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 ## How to run it
......
// 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) {
// 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
}
}
import { resolve, join, dirname } from 'path' import { resolve, join, dirname } from 'path'
import { existsSync } from 'fs'
import { readFile, writeFile } from 'mz/fs' import { readFile, writeFile } from 'mz/fs'
import { transform } from 'babel-core' import { transform } from 'babel-core'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import mkdirp from 'mkdirp-then' import mkdirp from 'mkdirp-then'
import getConfig from '../../config'
export default babel export default babel
async function babel (dir, { dev = false } = {}) { async function babel (dir, { dev = false } = {}) {
dir = resolve(dir) dir = resolve(dir)
const config = getConfig('../')
let src let src
try { try {
...@@ -23,14 +22,14 @@ async function babel (dir, { dev = false } = {}) { ...@@ -23,14 +22,14 @@ async function babel (dir, { dev = false } = {}) {
} }
let babelOptions = { let babelOptions = {
babelrc: false, babelrc: true,
sourceMaps: dev ? 'inline' : false, sourceMaps: dev ? 'inline' : false,
presets: [require.resolve('./preset')] presets: []
} }
if (config.babel) { const hasBabelRc = existsSync(join(dir, '.babelrc'))
console.log('> Using "babel" config function defined in next.config.js.') if (!hasBabelRc) {
babelOptions = await config.babel(babelOptions, { dev }) babelOptions.presets.push(require.resolve('./preset'))
} }
const { code } = transform(src, babelOptions) const { code } = transform(src, babelOptions)
......
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { createHash } from 'crypto' import { createHash } from 'crypto'
import { existsSync } from 'fs'
import webpack from 'webpack' import webpack from 'webpack'
import glob from 'glob-promise' import glob from 'glob-promise'
import WriteFilePlugin from 'write-file-webpack-plugin' import WriteFilePlugin from 'write-file-webpack-plugin'
...@@ -84,18 +85,18 @@ export default async function createCompiler (dir, { dev = false, quiet = false ...@@ -84,18 +85,18 @@ export default async function createCompiler (dir, { dev = false, quiet = false
) )
} }
let mainBabelOptions = { const mainBabelOptions = {
babelrc: false, babelrc: true,
cacheDirectory: true, cacheDirectory: true,
sourceMaps: dev ? 'both' : false, sourceMaps: dev ? 'both' : false,
presets: [ presets: []
require.resolve('./babel/preset')
]
} }
if (config.babel) { const hasBabelRc = existsSync(join(dir, '.babelrc'))
console.log('> Using "babel" config function defined in next.config.js.') if (hasBabelRc) {
mainBabelOptions = await config.babel(mainBabelOptions, { dev }) console.log('> Using .babelrc defined in your app root')
} else {
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
} }
const loaders = (dev ? [{ const loaders = (dev ? [{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册