next.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * == Notes for production build==
  3. * The modules required from this file must be transpiled before running `next build`.
  4. *
  5. * See: https://github.com/vercel/next.js/discussions/35969#discussioncomment-2522954
  6. */
  7. import type { NextConfig } from 'next';
  8. import path from 'node:path';
  9. import nextI18nConfig from './config/next-i18next.config';
  10. import { listPrefixedPackages } from './src/utils/next.config.utils';
  11. const { i18n } = nextI18nConfig;
  12. const getTranspilePackages = (): string[] => {
  13. const packages = [
  14. // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
  15. 'react-markdown',
  16. 'unified',
  17. 'markdown-table',
  18. 'bail',
  19. 'ccount',
  20. 'character-entities',
  21. 'character-entities-html4',
  22. 'character-entities-legacy',
  23. 'comma-separated-tokens',
  24. 'decode-named-character-reference',
  25. 'devlop',
  26. 'fault',
  27. 'hastscript',
  28. 'html-void-elements',
  29. 'is-absolute-url',
  30. 'is-plain-obj',
  31. 'longest-streak',
  32. 'micromark',
  33. 'property-information',
  34. 'space-separated-tokens',
  35. 'stringify-entities',
  36. 'trim-lines',
  37. 'trough',
  38. 'web-namespaces',
  39. 'vfile',
  40. 'vfile-location',
  41. 'vfile-message',
  42. 'zwitch',
  43. 'emoticon',
  44. 'direction', // for hast-util-select
  45. 'bcp-47-match', // for hast-util-select
  46. 'parse-entities',
  47. 'character-reference-invalid',
  48. 'is-hexadecimal',
  49. 'is-alphabetical',
  50. 'is-alphanumerical',
  51. 'github-slugger',
  52. 'html-url-attributes',
  53. 'estree-util-is-identifier-name',
  54. 'superjson',
  55. ...listPrefixedPackages([
  56. 'remark-',
  57. 'rehype-',
  58. 'hast-',
  59. 'mdast-',
  60. 'micromark-',
  61. 'unist-',
  62. ]),
  63. ];
  64. return packages;
  65. };
  66. const optimizePackageImports: string[] = [
  67. '@growi/core',
  68. '@growi/editor',
  69. '@growi/pluginkit',
  70. '@growi/presentation',
  71. '@growi/preset-themes',
  72. '@growi/remark-attachment-refs',
  73. '@growi/remark-drawio',
  74. '@growi/remark-growi-directive',
  75. '@growi/remark-lsx',
  76. '@growi/slack',
  77. '@growi/ui',
  78. ];
  79. // This config is used at build time only (next build / next dev).
  80. // Production runtime uses next.config.prod.cjs (installed as next.config.js by assemble-prod.sh).
  81. const nextConfig: NextConfig = {
  82. reactStrictMode: true,
  83. poweredByHeader: false,
  84. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  85. i18n,
  86. serverExternalPackages: [
  87. 'handsontable', // Legacy v6.2.2 requires @babel/polyfill which is unavailable; client-only via dynamic import
  88. ],
  89. // for build
  90. typescript: {
  91. tsconfigPath: 'tsconfig.build.client.json',
  92. },
  93. transpilePackages: getTranspilePackages(),
  94. sassOptions: {
  95. loadPaths: [path.resolve(__dirname, 'src')],
  96. },
  97. experimental: {
  98. optimizePackageImports,
  99. },
  100. turbopack: {
  101. rules: {
  102. // Server-only: auto-wrap getServerSideProps with SuperJSON serialization
  103. '*.page.ts': [
  104. {
  105. condition: { not: 'browser' },
  106. loaders: [
  107. path.resolve(__dirname, 'src/utils/superjson-ssr-loader.ts'),
  108. ],
  109. as: '*.ts',
  110. },
  111. ],
  112. '*.page.tsx': [
  113. {
  114. condition: { not: 'browser' },
  115. loaders: [
  116. path.resolve(__dirname, 'src/utils/superjson-ssr-loader.ts'),
  117. ],
  118. as: '*.tsx',
  119. },
  120. ],
  121. },
  122. resolveAlias: {
  123. // Exclude fs from client bundle
  124. fs: { browser: './src/lib/empty-module.ts' },
  125. // Exclude server-only packages from client bundle
  126. mongoose: { browser: './src/lib/empty-module.ts' },
  127. 'i18next-fs-backend': { browser: './src/lib/empty-module.ts' },
  128. 'core-js': { browser: './src/lib/empty-module.ts' },
  129. },
  130. },
  131. };
  132. export default nextConfig;