next.config.js 4.4 KB

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