Skip to content

Commit

Permalink
Configurable opts for babel-preset-env + babel-plugin-transform-runti…
Browse files Browse the repository at this point in the history
…me (#2991)

* Configurable opts for babel-preset-env + babel-plugin-transform-runtime

This adds `preset-env` and `transform-runtime` options to the
`next/babel` Babel preset, which are then passed through to those
presets and transforms. This allows configuration to keep next.js
from the default 'maximum' transform, and instead use built-in
implementations of globals, classes, async, and other commonly-supported
features.

Fixes #2989

* Use spread notation instead of Object.assign
  • Loading branch information
tmcw authored and timneutkens committed Oct 7, 2017
1 parent a761aa5 commit 559c252
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
26 changes: 26 additions & 0 deletions examples/with-configured-preset-env/.babelrc
@@ -0,0 +1,26 @@
{
"env": {
"production": {
"presets": [
"next/babel"
]
},
"development": {
"presets": [
["next/babel", {
"preset-env": {
"targets": {
"browsers": "last 1 Chrome version",
"node": true
}
},
"transform-runtime": {
"regenerator": false,
"useBuiltIns": true,
"polyfill": false
}
}]
]
}
}
}
12 changes: 12 additions & 0 deletions examples/with-configured-preset-env/package.json
@@ -0,0 +1,12 @@
{
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^15.4.2",
"react-dom": "^15.4.2"
}
}
21 changes: 21 additions & 0 deletions examples/with-configured-preset-env/pages/index.js
@@ -0,0 +1,21 @@
import {Component} from 'react'

function * generatorMethod () {
yield 1
}

async function fooBar () {
for (let val of generatorMethod()) {
return val
}
}

export default class Index extends Component {
async otherMethod () {
await fooBar()
}
render () {
const foo = new Map([['cats', 'dogs']])
return <h1>Garbage {foo.get('cats')}</h1>
}
}
10 changes: 6 additions & 4 deletions server/build/babel/preset.js
Expand Up @@ -11,10 +11,11 @@ const envPlugins = {

const plugins = envPlugins[process.env.NODE_ENV] || envPlugins['development']

module.exports = {
module.exports = (context, opts = {}) => ({
presets: [
[require.resolve('babel-preset-env'), {
modules: false
modules: false,
...opts['preset-env']
}],
require.resolve('babel-preset-react')
],
Expand All @@ -23,7 +24,8 @@ module.exports = {
require.resolve('./plugins/handle-import'),
require.resolve('babel-plugin-transform-object-rest-spread'),
require.resolve('babel-plugin-transform-class-properties'),
require.resolve('babel-plugin-transform-runtime'),
[require.resolve('babel-plugin-transform-runtime'),
opts['transform-runtime'] || {}],
require.resolve('styled-jsx/babel'),
...plugins,
[
Expand All @@ -43,4 +45,4 @@ module.exports = {
}
]
]
}
})

0 comments on commit 559c252

Please sign in to comment.