Commit ab57978d authored by Jason Miller's avatar Jason Miller Committed by Joe Haddad

Modern JS for SSR Bundles (#7961)

* Compile for the current version of Node.js by default when producing server builds.

* Update packages/next/build/babel/preset.ts
Co-Authored-By: 's avatarJoe Haddad <timer150@gmail.com>

* Include server variation in Babel cacheKey
parent 6faf0d18
...@@ -60,6 +60,7 @@ module.exports = ( ...@@ -60,6 +60,7 @@ module.exports = (
options: NextBabelPresetOptions = {} options: NextBabelPresetOptions = {}
): BabelPreset => { ): BabelPreset => {
const supportsESM = api.caller(supportsStaticESM) const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer)
const presetEnvConfig = { const presetEnvConfig = {
// 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
...@@ -67,6 +68,19 @@ module.exports = ( ...@@ -67,6 +68,19 @@ module.exports = (
exclude: ['transform-typeof-symbol'], exclude: ['transform-typeof-symbol'],
...options['preset-env'], ...options['preset-env'],
} }
// When transpiling for the server, target the current Node version if not explicitly specified:
if (
isServer &&
(!presetEnvConfig.targets || !('node' in presetEnvConfig.targets))
) {
presetEnvConfig.targets = {
// Targets the current process' version of Node. This requires apps be
// built and deployed on the same version of Node.
node: 'current',
}
}
return { return {
presets: [ presets: [
[require('@babel/preset-env').default, presetEnvConfig], [require('@babel/preset-env').default, presetEnvConfig],
......
...@@ -34,6 +34,7 @@ module.exports = babelLoader.custom(babel => { ...@@ -34,6 +34,7 @@ module.exports = babelLoader.custom(babel => {
cacheDirectory: join(opts.distDir, 'cache', 'next-babel-loader'), cacheDirectory: join(opts.distDir, 'cache', 'next-babel-loader'),
cacheIdentifier: cacheIdentifier:
cacheKey + cacheKey +
(opts.isServer ? '-server' : '') +
JSON.stringify( JSON.stringify(
babel.loadPartialConfig({ babel.loadPartialConfig({
filename, filename,
...@@ -80,6 +81,8 @@ module.exports = babelLoader.custom(babel => { ...@@ -80,6 +81,8 @@ module.exports = babelLoader.custom(babel => {
options.presets = [...options.presets, presetItem] options.presets = [...options.presets, presetItem]
} }
options.caller.isServer = isServer
if (!isServer && isPageFile) { if (!isServer && isPageFile) {
const pageConfigPlugin = babel.createConfigItem( const pageConfigPlugin = babel.createConfigItem(
[require('../../babel/plugins/next-page-config')], [require('../../babel/plugins/next-page-config')],
......
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