next.config.js 4.4 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. ...listPrefixedPackages([
  58. 'remark-',
  59. 'rehype-',
  60. 'hast-',
  61. 'mdast-',
  62. 'micromark-',
  63. 'unist-',
  64. ]),
  65. ];
  66. // const eazyLogger = require('eazy-logger');
  67. // const logger = eazyLogger.Logger({
  68. // prefix: '[{green:next.config.js}] ',
  69. // useLevelPrefixes: false,
  70. // });
  71. // logger.info('{bold:Listing scoped packages for transpiling:}');
  72. // logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  73. return packages;
  74. };
  75. const optimizePackageImports = [
  76. '@growi/core',
  77. '@growi/editor',
  78. '@growi/pluginkit',
  79. '@growi/presentation',
  80. '@growi/preset-themes',
  81. '@growi/remark-attachment-refs',
  82. '@growi/remark-drawio',
  83. '@growi/remark-growi-directive',
  84. '@growi/remark-lsx',
  85. '@growi/slack',
  86. '@growi/ui',
  87. ];
  88. module.exports = async (phase) => {
  89. const { i18n, localePath } = require('./config/next-i18next.config');
  90. /** @type {import('next').NextConfig} */
  91. const nextConfig = {
  92. reactStrictMode: true,
  93. poweredByHeader: false,
  94. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  95. i18n,
  96. // for build
  97. eslint: {
  98. ignoreDuringBuilds: true,
  99. },
  100. typescript: {
  101. tsconfigPath: 'tsconfig.build.client.json',
  102. },
  103. transpilePackages:
  104. phase !== PHASE_PRODUCTION_SERVER ? getTranspilePackages() : undefined,
  105. experimental: {
  106. optimizePackageImports,
  107. },
  108. /** @param config {import('next').NextConfig} */
  109. webpack(config, options) {
  110. if (!options.isServer) {
  111. // Avoid "Module not found: Can't resolve 'fs'"
  112. // See: https://stackoverflow.com/a/68511591
  113. config.resolve.fallback.fs = false;
  114. // exclude packages from the output bundles
  115. config.module.rules.push(
  116. ...[
  117. /dtrace-provider/,
  118. /mongoose/,
  119. /mathjax-full/, // required from marp
  120. ].map((packageRegExp) => {
  121. return {
  122. test: packageRegExp,
  123. use: 'null-loader',
  124. };
  125. }),
  126. );
  127. }
  128. // extract sourcemap
  129. if (options.dev) {
  130. config.module.rules.push({
  131. test: /.(c|m)?js$/,
  132. exclude: [/node_modules/, path.resolve(__dirname)],
  133. enforce: 'pre',
  134. use: ['source-map-loader'],
  135. });
  136. }
  137. // setup i18next-hmr
  138. if (!options.isServer && options.dev) {
  139. const { I18NextHMRPlugin } = require('i18next-hmr/webpack');
  140. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  141. }
  142. return config;
  143. },
  144. };
  145. // production server
  146. if (phase === PHASE_PRODUCTION_SERVER) {
  147. return withSuperjson()(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. };