Commit 4b9b2fb3 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Make async-to-promises babel plugin experimental (#7006)

* Make async-to-promises babel plugin experimental

* Move excludes for asyncToPromises behind flag too

* Move other configs behind flag

* Re-add original exclude item
parent 53a336ce
...@@ -31,7 +31,8 @@ const defaultConfig = { ...@@ -31,7 +31,8 @@ const defaultConfig = {
ampBindInitData: false, ampBindInitData: false,
exportTrailingSlash: true, exportTrailingSlash: true,
profiling: false, profiling: false,
flyingShuttle: false flyingShuttle: false,
asyncToPromises: false
} }
} }
......
...@@ -58,11 +58,7 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset = ...@@ -58,11 +58,7 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset =
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option // In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking // In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules: 'auto', modules: 'auto',
exclude: [ exclude: ['transform-typeof-symbol'],
'transform-typeof-symbol',
'transform-regenerator',
'transform-async-to-generator'
],
...options['preset-env'] ...options['preset-env']
} }
return { return {
...@@ -76,9 +72,6 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset = ...@@ -76,9 +72,6 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset =
}] }]
], ],
plugins: [ plugins: [
['babel-plugin-transform-async-to-promises', {
inlineHelpers: true
}],
require('babel-plugin-react-require'), require('babel-plugin-react-require'),
require('@babel/plugin-syntax-dynamic-import'), require('@babel/plugin-syntax-dynamic-import'),
// Transform dynamic import to require // Transform dynamic import to require
...@@ -91,7 +84,7 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset = ...@@ -91,7 +84,7 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset =
[require('@babel/plugin-transform-runtime'), { [require('@babel/plugin-transform-runtime'), {
corejs: 2, corejs: 2,
helpers: true, helpers: true,
regenerator: false, regenerator: true,
useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs', useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs',
...options['transform-runtime'] ...options['transform-runtime']
}], }],
......
...@@ -22,7 +22,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug = ...@@ -22,7 +22,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
const defaultLoaders = { const defaultLoaders = {
babel: { babel: {
loader: 'next-babel-loader', loader: 'next-babel-loader',
options: { isServer, cwd: dir } options: { isServer, cwd: dir, asyncToPromises: config.experimental.asyncToPromises }
}, },
// Backwards compat // Backwards compat
hotSelfAccept: { hotSelfAccept: {
......
...@@ -15,7 +15,8 @@ module.exports = babelLoader.custom(babel => { ...@@ -15,7 +15,8 @@ module.exports = babelLoader.custom(babel => {
return { return {
customOptions (opts) { customOptions (opts) {
const custom = { const custom = {
isServer: opts.isServer isServer: opts.isServer,
asyncToPromises: opts.asyncToPromises
} }
const filename = join(opts.cwd, 'noop.js') const filename = join(opts.cwd, 'noop.js')
const loader = Object.assign({ const loader = Object.assign({
...@@ -31,9 +32,10 @@ module.exports = babelLoader.custom(babel => { ...@@ -31,9 +32,10 @@ module.exports = babelLoader.custom(babel => {
}, opts) }, opts)
delete loader.isServer delete loader.isServer
delete loader.asyncToPromises
return { loader, custom } return { loader, custom }
}, },
config (cfg, { source, customOptions: { isServer } }) { config (cfg, { source, customOptions: { isServer, asyncToPromises } }) {
const filename = this.resourcePath const filename = this.resourcePath
const options = Object.assign({}, cfg.options) const options = Object.assign({}, cfg.options)
if (cfg.hasFilesystemConfig()) { if (cfg.hasFilesystemConfig()) {
...@@ -56,6 +58,33 @@ module.exports = babelLoader.custom(babel => { ...@@ -56,6 +58,33 @@ module.exports = babelLoader.custom(babel => {
options.plugins.push(nextDataPlugin) options.plugins.push(nextDataPlugin)
} }
if (asyncToPromises) {
const asyncToPromisesPlugin = babel.createConfigItem(['babel-plugin-transform-async-to-promises', {
inlineHelpers: true
}], { type: 'plugin' })
options.plugins = options.plugins || []
options.plugins.push(asyncToPromisesPlugin)
const regeneratorPlugin = options.plugins.find(plugin => {
return plugin[0] === require('@babel/plugin-transform-runtime')
})
if (regeneratorPlugin) {
regeneratorPlugin[1].regenerator = false
}
const babelPresetEnv = (options.presets || []).find((preset = []) => {
return preset[0] === require('@babel/preset-env').default
})
if (babelPresetEnv) {
babelPresetEnv[1].exclude = (options.presets[0][1].exclude || []).concat([
'transform-typeof-symbol',
'transform-regenerator',
'transform-async-to-generator'
])
.filter('transform-typeof-symbol')
}
}
// If the file has `module.exports` we have to transpile commonjs because Babel adds `import` statements // If the file has `module.exports` we have to transpile commonjs because Babel adds `import` statements
// That break webpack, since webpack doesn't support combining commonjs and esmodules // That break webpack, since webpack doesn't support combining commonjs and esmodules
if (source.indexOf('module.exports') !== -1) { if (source.indexOf('module.exports') !== -1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment