next.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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('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, { defaultConfig }) => {
  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. eslint: {
  99. ignoreDuringBuilds: true,
  100. },
  101. typescript: {
  102. tsconfigPath: 'tsconfig.build.client.json',
  103. },
  104. transpilePackages:
  105. phase !== PHASE_PRODUCTION_SERVER ? getTranspilePackages() : undefined,
  106. experimental: {
  107. optimizePackageImports,
  108. },
  109. /** @param config {import('next').NextConfig} */
  110. webpack(config, options) {
  111. if (!options.isServer) {
  112. // Avoid "Module not found: Can't resolve 'fs'"
  113. // See: https://stackoverflow.com/a/68511591
  114. config.resolve.fallback.fs = false;
  115. // exclude packages from the output bundles
  116. config.module.rules.push(
  117. ...[
  118. /dtrace-provider/,
  119. /mongoose/,
  120. /mathjax-full/, // required from marp
  121. ].map((packageRegExp) => {
  122. return {
  123. test: packageRegExp,
  124. use: 'null-loader',
  125. };
  126. }),
  127. );
  128. }
  129. // extract sourcemap
  130. if (options.dev) {
  131. config.module.rules.push({
  132. test: /.(c|m)?js$/,
  133. exclude: [/node_modules/, path.resolve(__dirname)],
  134. enforce: 'pre',
  135. use: ['source-map-loader'],
  136. });
  137. }
  138. // setup i18next-hmr
  139. if (!options.isServer && options.dev) {
  140. const { I18NextHMRPlugin } = require('i18next-hmr/webpack');
  141. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  142. }
  143. return config;
  144. },
  145. };
  146. // production server
  147. if (phase === PHASE_PRODUCTION_SERVER) {
  148. return withSuperjson()(nextConfig);
  149. }
  150. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  151. enabled:
  152. phase === PHASE_PRODUCTION_BUILD &&
  153. (process.env.ANALYZE === 'true' || process.env.ANALYZE === '1'),
  154. });
  155. return withBundleAnalyzer(withSuperjson()(nextConfig));
  156. };