next.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. const path = require('node:path');
  8. const { withSuperjson } = require('next-superjson');
  9. const {
  10. PHASE_PRODUCTION_BUILD,
  11. PHASE_PRODUCTION_SERVER,
  12. } = require('next/constants');
  13. const getTranspilePackages = () => {
  14. const { listPrefixedPackages } = require('./src/utils/next.config.utils');
  15. const packages = [
  16. // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
  17. 'react-markdown',
  18. 'unified',
  19. 'markdown-table',
  20. 'bail',
  21. 'ccount',
  22. 'character-entities',
  23. 'character-entities-html4',
  24. 'character-entities-legacy',
  25. 'comma-separated-tokens',
  26. 'decode-named-character-reference',
  27. 'devlop',
  28. 'fault',
  29. 'escape-string-regexp',
  30. 'hastscript',
  31. 'html-void-elements',
  32. 'is-absolute-url',
  33. 'is-plain-obj',
  34. 'longest-streak',
  35. 'micromark',
  36. 'property-information',
  37. 'space-separated-tokens',
  38. 'stringify-entities',
  39. 'trim-lines',
  40. 'trough',
  41. 'web-namespaces',
  42. 'vfile',
  43. 'vfile-location',
  44. 'vfile-message',
  45. 'zwitch',
  46. 'emoticon',
  47. 'direction', // for hast-util-select
  48. 'bcp-47-match', // for hast-util-select
  49. 'parse-entities',
  50. 'character-reference-invalid',
  51. 'is-hexadecimal',
  52. 'is-alphabetical',
  53. 'is-alphanumerical',
  54. 'github-slugger',
  55. 'html-url-attributes',
  56. 'estree-util-is-identifier-name',
  57. 'superjson',
  58. ...listPrefixedPackages([
  59. 'remark-',
  60. 'rehype-',
  61. 'hast-',
  62. 'mdast-',
  63. 'micromark-',
  64. 'unist-',
  65. ]),
  66. ];
  67. // const eazyLogger = require('eazy-logger');
  68. // const logger = eazyLogger.Logger({
  69. // prefix: '[{green:next.config.js}] ',
  70. // useLevelPrefixes: false,
  71. // });
  72. // logger.info('{bold:Listing scoped packages for transpiling:}');
  73. // logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  74. return packages;
  75. };
  76. const optimizePackageImports = [
  77. '@growi/core',
  78. '@growi/editor',
  79. '@growi/pluginkit',
  80. '@growi/presentation',
  81. '@growi/preset-themes',
  82. '@growi/remark-attachment-refs',
  83. '@growi/remark-drawio',
  84. '@growi/remark-growi-directive',
  85. '@growi/remark-lsx',
  86. '@growi/slack',
  87. '@growi/ui',
  88. ];
  89. module.exports = async (phase) => {
  90. const { i18n, localePath } = require('./config/next-i18next.config');
  91. /** @type {import('next').NextConfig} */
  92. const nextConfig = {
  93. reactStrictMode: true,
  94. poweredByHeader: false,
  95. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  96. i18n,
  97. // for build
  98. typescript: {
  99. tsconfigPath: 'tsconfig.build.client.json',
  100. },
  101. transpilePackages:
  102. phase !== PHASE_PRODUCTION_SERVER ? getTranspilePackages() : undefined,
  103. experimental: {
  104. optimizePackageImports,
  105. },
  106. /** @param config {import('next').NextConfig} */
  107. webpack(config, options) {
  108. if (!options.isServer) {
  109. // Avoid "Module not found: Can't resolve 'fs'"
  110. // See: https://stackoverflow.com/a/68511591
  111. config.resolve.fallback.fs = false;
  112. // exclude packages from the output bundles
  113. config.module.rules.push(
  114. ...[
  115. /dtrace-provider/,
  116. /mongoose/,
  117. /mathjax-full/, // required from marp
  118. ].map((packageRegExp) => {
  119. return {
  120. test: packageRegExp,
  121. use: 'null-loader',
  122. };
  123. }),
  124. );
  125. }
  126. // extract sourcemap
  127. if (options.dev) {
  128. config.module.rules.push({
  129. test: /.(c|m)?js$/,
  130. exclude: [/node_modules/, path.resolve(__dirname)],
  131. enforce: 'pre',
  132. use: ['source-map-loader'],
  133. });
  134. }
  135. // setup i18next-hmr
  136. if (!options.isServer && options.dev) {
  137. const { I18NextHMRPlugin } = require('i18next-hmr/webpack');
  138. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  139. }
  140. return config;
  141. },
  142. };
  143. // production server
  144. // Skip withSuperjson() in production server phase because the pages directory
  145. // doesn't exist in the production build and withSuperjson() tries to find it
  146. if (phase === PHASE_PRODUCTION_SERVER) {
  147. return nextConfig;
  148. }
  149. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  150. enabled:
  151. phase === PHASE_PRODUCTION_BUILD &&
  152. (process.env.ANALYZE === 'true' || process.env.ANALYZE === '1'),
  153. });
  154. return withBundleAnalyzer(withSuperjson()(nextConfig));
  155. };