| 26 | // Turbopack's resolveAlias does not handle absolute paths (it prepends "./"), |
| 27 | // so we resolve paths relative to __dirname at runtime in next.config.js. |
| 28 | function patchNextConfig({ targetDir }) { |
| 29 | const nextConfigPath = path.join(targetDir, 'next.config.js'); |
| 30 | let content = fs.readFileSync(nextConfigPath, 'utf8'); |
| 31 | |
| 32 | // Silence the "multiple lockfiles" warning by telling Next.js the |
| 33 | // monorepo root is the output file tracing root. |
| 34 | if (content.includes('turbopack: {},')) { |
| 35 | content = content.replace( |
| 36 | 'turbopack: {},', |
| 37 | [ |
| 38 | `outputFileTracingRoot: require('path').resolve(__dirname, '../..'),`, |
| 39 | ` turbopack: {`, |
| 40 | ` resolveAlias: {`, |
| 41 | ` react: './' + require('path').relative(__dirname, require('path').dirname(require.resolve('react/package.json'))),`, |
| 42 | ` 'react-dom': './' + require('path').relative(__dirname, require('path').dirname(require.resolve('react-dom/package.json'))),`, |
| 43 | ` },`, |
| 44 | ` },`, |
| 45 | ].join('\n') |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | fs.writeFileSync(nextConfigPath, content); |
| 50 | } |
| 51 | |
| 52 | export default patchNextConfig; |