next.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 'escape-string-regexp',
  28. 'hastscript',
  29. 'html-void-elements',
  30. 'is-absolute-url',
  31. 'is-plain-obj',
  32. 'longest-streak',
  33. 'micromark',
  34. 'property-information',
  35. 'space-separated-tokens',
  36. 'stringify-entities',
  37. 'trim-lines',
  38. 'trough',
  39. 'web-namespaces',
  40. 'vfile',
  41. 'vfile-location',
  42. 'vfile-message',
  43. 'zwitch',
  44. 'emoticon',
  45. 'direction', // for hast-util-select
  46. 'bcp-47-match', // for hast-util-select
  47. 'parse-entities',
  48. 'character-reference-invalid',
  49. 'is-hexadecimal',
  50. 'is-alphabetical',
  51. 'is-alphanumerical',
  52. 'github-slugger',
  53. 'html-url-attributes',
  54. 'estree-util-is-identifier-name',
  55. 'superjson',
  56. ...listPrefixedPackages([
  57. 'remark-',
  58. 'rehype-',
  59. 'hast-',
  60. 'mdast-',
  61. 'micromark-',
  62. 'unist-',
  63. ]),
  64. ];
  65. return packages;
  66. };
  67. const optimizePackageImports: string[] = [
  68. '@growi/core',
  69. '@growi/editor',
  70. '@growi/pluginkit',
  71. '@growi/presentation',
  72. '@growi/preset-themes',
  73. '@growi/remark-attachment-refs',
  74. '@growi/remark-drawio',
  75. '@growi/remark-growi-directive',
  76. '@growi/remark-lsx',
  77. '@growi/slack',
  78. '@growi/ui',
  79. ];
  80. // This config is used at build time only (next build / next dev).
  81. // Production runtime uses next.config.prod.cjs (installed as next.config.js by assemble-prod.sh).
  82. const nextConfig: NextConfig = {
  83. reactStrictMode: true,
  84. poweredByHeader: false,
  85. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  86. i18n,
  87. serverExternalPackages: [
  88. 'handsontable', // Legacy v6.2.2 requires @babel/polyfill which is unavailable; client-only via dynamic import
  89. ],
  90. // for build
  91. typescript: {
  92. tsconfigPath: 'tsconfig.build.client.json',
  93. },
  94. transpilePackages: getTranspilePackages(),
  95. sassOptions: {
  96. loadPaths: [path.resolve(__dirname, 'src')],
  97. },
  98. experimental: {
  99. optimizePackageImports,
  100. },
  101. turbopack: {
  102. rules: {
  103. // Server-only: auto-wrap getServerSideProps with SuperJSON serialization
  104. '*.page.ts': [
  105. {
  106. condition: { not: 'browser' },
  107. loaders: [
  108. path.resolve(__dirname, 'src/utils/superjson-ssr-loader.ts'),
  109. ],
  110. as: '*.ts',
  111. },
  112. ],
  113. '*.page.tsx': [
  114. {
  115. condition: { not: 'browser' },
  116. loaders: [
  117. path.resolve(__dirname, 'src/utils/superjson-ssr-loader.ts'),
  118. ],
  119. as: '*.tsx',
  120. },
  121. ],
  122. },
  123. resolveAlias: {
  124. // Exclude fs from client bundle
  125. fs: { browser: './src/lib/empty-module.ts' },
  126. // Exclude server-only packages from client bundle
  127. 'dtrace-provider': { browser: './src/lib/empty-module.ts' },
  128. mongoose: { browser: './src/lib/empty-module.ts' },
  129. 'i18next-fs-backend': { browser: './src/lib/empty-module.ts' },
  130. bunyan: { browser: './src/lib/empty-module.ts' },
  131. 'bunyan-format': { browser: './src/lib/empty-module.ts' },
  132. 'core-js': { browser: './src/lib/empty-module.ts' },
  133. },
  134. },
  135. };
  136. export default nextConfig;