next.config.ts 4.0 KB

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