next.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { withSuperjson } = require('next-superjson');
  8. const { PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_SERVER } = require('next/constants');
  9. const getTranspilePackages = () => {
  10. const eazyLogger = require('eazy-logger');
  11. const { listScopedPackages, listPrefixedPackages } = require('./src/utils/next.config.utils');
  12. // setup logger
  13. const logger = eazyLogger.Logger({
  14. prefix: '[{green:next.config.js}] ',
  15. useLevelPrefixes: false,
  16. });
  17. // define transpiled packages for '@growi/*'
  18. const packages = [
  19. ...listScopedPackages(['@growi'], { ignorePackageNames: ['@growi/app'] }),
  20. // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
  21. 'react-markdown',
  22. 'unified',
  23. 'markdown-table',
  24. 'character-entities-html4',
  25. 'comma-separated-tokens',
  26. 'decode-named-character-reference',
  27. 'escape-string-regexp',
  28. 'hastscript',
  29. 'html-void-elements',
  30. 'is-absolute-url',
  31. 'longest-streak',
  32. 'property-information',
  33. 'space-separated-tokens',
  34. 'stringify-entities',
  35. 'trim-lines',
  36. 'trough',
  37. 'web-namespaces',
  38. 'vfile',
  39. 'vfile-location',
  40. 'vfile-message',
  41. 'zwitch',
  42. 'emoticon',
  43. 'direction', // for hast-util-select
  44. 'bcp-47-match', // for hast-util-select
  45. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'unist-']),
  46. ];
  47. // logger.info('{bold:Listing scoped packages for transpiling:}');
  48. // logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  49. return packages;
  50. };
  51. module.exports = async(phase, { defaultConfig }) => {
  52. const { i18n, localePath } = require('./config/next-i18next.config');
  53. /** @type {import('next').NextConfig} */
  54. const nextConfig = {
  55. reactStrictMode: true,
  56. poweredByHeader: false,
  57. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  58. i18n,
  59. // for build
  60. eslint: {
  61. ignoreDuringBuilds: true,
  62. },
  63. typescript: {
  64. tsconfigPath: 'tsconfig.build.client.json',
  65. },
  66. transpilePackages: phase !== PHASE_PRODUCTION_SERVER
  67. ? getTranspilePackages()
  68. : undefined,
  69. /** @param config {import('next').NextConfig} */
  70. webpack(config, options) {
  71. // Avoid "Module not found: Can't resolve 'fs'"
  72. // See: https://stackoverflow.com/a/68511591
  73. if (!options.isServer) {
  74. config.resolve.fallback.fs = false;
  75. }
  76. // See: https://webpack.js.org/configuration/externals/
  77. // This provides a way of excluding dependencies from the output bundles
  78. config.externals.push('dtrace-provider');
  79. config.externals.push('mongoose');
  80. // extract sourcemap
  81. if (options.dev) {
  82. config.module.rules.push({
  83. test: /.(c|m)?js$/,
  84. exclude: /node_modules/,
  85. enforce: 'pre',
  86. use: ['source-map-loader'],
  87. });
  88. }
  89. // setup i18next-hmr
  90. if (!options.isServer && options.dev) {
  91. const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
  92. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  93. }
  94. return config;
  95. },
  96. };
  97. // production server
  98. if (phase === PHASE_PRODUCTION_SERVER) {
  99. return withSuperjson()(nextConfig);
  100. }
  101. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  102. enabled: phase === PHASE_PRODUCTION_BUILD || process.env.ANALYZE === 'true',
  103. });
  104. return withBundleAnalyzer(withSuperjson()(nextConfig));
  105. };