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 = (
options: NextBabelPresetOptions = {}
): BabelPreset => {
const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer)
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 production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
......@@ -67,6 +68,19 @@ module.exports = (
exclude: ['transform-typeof-symbol'],
...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 {
presets: [
[require('@babel/preset-env').default, presetEnvConfig],
......
......@@ -34,6 +34,7 @@ module.exports = babelLoader.custom(babel => {
cacheDirectory: join(opts.distDir, 'cache', 'next-babel-loader'),
cacheIdentifier:
cacheKey +
(opts.isServer ? '-server' : '') +
JSON.stringify(
babel.loadPartialConfig({
filename,
......@@ -80,6 +81,8 @@ module.exports = babelLoader.custom(babel => {
options.presets = [...options.presets, presetItem]
}
options.caller.isServer = isServer
if (!isServer && isPageFile) {
const pageConfigPlugin = babel.createConfigItem(
[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