Repository: vercel/next.js
Pull request: simplify the way we check for turbopack config to ensure we support an empty turbopack object

Previously we 'flattened' all the keys in the config to find unsupported leaf properties, but this meant that an empty object woudln't produce any keys.

instead just directly check for the `webpack` and `turbopack` properties and only use the scanning to look for experimental turbopack options and unsupported experimental configuration.

Fixes an issue where you cannot bypass the warning simply by adding `turbopack: {}`


Closes PACK-5618

Changed files:
filename: packages/next/src/lib/turbopack-warning.ts
patch: @@ -42,9 +42,6 @@ const unsupportedTurbopackNextConfigOptions = [
   'experimental.slowModuleDetection',
 ]
 
-// The following will need to be supported by `next build --turbopack`
-const unsupportedProductionSpecificTurbopackNextConfigOptions: string[] = []
-
 /**  */
 export async function validateTurboNextConfig({
   dir,
@@ -81,23 +78,26 @@ export async function validateTurboNextConfig({
         defaultConfig,
       })
     }
+    hasWebpackConfig = Boolean(rawNextConfig.webpack)
+    hasTurboConfig = Boolean(rawNextConfig.turbopack)
 
     const flattenKeys = (obj: any, prefix: string = ''): string[] => {
       let keys: string[] = []
 
       for (const key in obj) {
-        if (typeof obj?.[key] === 'undefined') {
+        const value = obj?.[key]
+        if (typeof value === 'undefined') {
           continue
         }
 
         const pre = prefix.length ? `${prefix}.` : ''
 
         if (
-          typeof obj[key] === 'object' &&
-          !Array.isArray(obj[key]) &&
-          obj[key] !== null
+          typeof value === 'object' &&
+          !Array.isArray(value) &&
+          value !== null
         ) {
-          keys = keys.concat(flattenKeys(obj[key], pre + key))
+          keys = keys.concat(flattenKeys(value, pre + key))
         } else {
           keys.push(pre + key)
         }
@@ -118,23 +118,13 @@ export async function validateTurboNextConfig({
 
     const customKeys = flattenKeys(rawNextConfig)
 
-    const unsupportedKeys = isDev
-      ? unsupportedTurbopackNextConfigOptions
-      : [
-          ...unsupportedTurbopackNextConfigOptions,
-          ...unsupportedProductionSpecificTurbopackNextConfigOptions,
-        ]
-
     for (const key of customKeys) {
-      if (key.startsWith('webpack') && rawNextConfig.webpack) {
-        hasWebpackConfig = true
-      }
-      if (key.startsWith('turbopack') || key.startsWith('experimental.turbo')) {
+      if (key.startsWith('experimental.turbo')) {
         hasTurboConfig = true
       }
 
       const isUnsupported =
-        unsupportedKeys.some(
+        unsupportedTurbopackNextConfigOptions.some(
           (unsupportedKey) =>
             // Either the key matches (or is a more specific subkey) of
             // unsupportedKey, or the key is the path to a specific subkey.

filename: test/e2e/config-turbopack/index.test.ts
patch: @@ -89,11 +89,7 @@ export default function Page() {
             'next.config.js': `
             module.exports = {
               turbopack: {
-                rules: {
-                  '*.foo': {
-                    loaders: ['foo-loader']
-                  }
-                }
+               
               },
               webpack: (config) => {
                 return config