提交 4b9b2fb3 编写于 作者: J JJ Kasper 提交者: 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
上级 53a336ce
......@@ -31,7 +31,8 @@ const defaultConfig = {
ampBindInitData: false,
exportTrailingSlash: true,
profiling: false,
flyingShuttle: false
flyingShuttle: false,
asyncToPromises: false
}
}
......
......@@ -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 production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules: 'auto',
exclude: [
'transform-typeof-symbol',
'transform-regenerator',
'transform-async-to-generator'
],
exclude: ['transform-typeof-symbol'],
...options['preset-env']
}
return {
......@@ -76,9 +72,6 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset =
}]
],
plugins: [
['babel-plugin-transform-async-to-promises', {
inlineHelpers: true
}],
require('babel-plugin-react-require'),
require('@babel/plugin-syntax-dynamic-import'),
// Transform dynamic import to require
......@@ -91,7 +84,7 @@ module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset =
[require('@babel/plugin-transform-runtime'), {
corejs: 2,
helpers: true,
regenerator: false,
regenerator: true,
useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs',
...options['transform-runtime']
}],
......
......@@ -22,7 +22,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
const defaultLoaders = {
babel: {
loader: 'next-babel-loader',
options: { isServer, cwd: dir }
options: { isServer, cwd: dir, asyncToPromises: config.experimental.asyncToPromises }
},
// Backwards compat
hotSelfAccept: {
......
......@@ -15,7 +15,8 @@ module.exports = babelLoader.custom(babel => {
return {
customOptions (opts) {
const custom = {
isServer: opts.isServer
isServer: opts.isServer,
asyncToPromises: opts.asyncToPromises
}
const filename = join(opts.cwd, 'noop.js')
const loader = Object.assign({
......@@ -31,9 +32,10 @@ module.exports = babelLoader.custom(babel => {
}, opts)
delete loader.isServer
delete loader.asyncToPromises
return { loader, custom }
},
config (cfg, { source, customOptions: { isServer } }) {
config (cfg, { source, customOptions: { isServer, asyncToPromises } }) {
const filename = this.resourcePath
const options = Object.assign({}, cfg.options)
if (cfg.hasFilesystemConfig()) {
......@@ -56,6 +58,33 @@ module.exports = babelLoader.custom(babel => {
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
// That break webpack, since webpack doesn't support combining commonjs and esmodules
if (source.indexOf('module.exports') !== -1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册