next.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 setupTranspileModules = () => {
  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. 'hastscript',
  28. 'html-void-elements',
  29. 'is-absolute-url',
  30. 'longest-streak',
  31. 'property-information',
  32. 'space-separated-tokens',
  33. 'stringify-entities',
  34. 'trim-lines',
  35. 'trough',
  36. 'web-namespaces',
  37. 'vfile',
  38. 'zwitch',
  39. 'emoticon',
  40. 'direction', // for hast-util-select
  41. 'bcp-47-match', // for hast-util-select
  42. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'unist-']),
  43. ];
  44. // logger.info('{bold:Listing scoped packages for transpiling:}');
  45. // logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  46. return require('next-transpile-modules')(packages);
  47. };
  48. module.exports = async(phase, { defaultConfig }) => {
  49. const { i18n, localePath } = require('./config/next-i18next.config');
  50. /** @type {import('next').NextConfig} */
  51. const nextConfig = {
  52. // == DOES NOT WORK
  53. // see: https://github.com/vercel/next.js/discussions/27876
  54. // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
  55. eslint: {
  56. ignoreDuringBuilds: true,
  57. },
  58. reactStrictMode: true,
  59. swcMinify: true,
  60. typescript: {
  61. tsconfigPath: 'tsconfig.build.client.json',
  62. },
  63. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  64. i18n,
  65. /** @param config {import('next').NextConfig} */
  66. webpack(config, options) {
  67. // Avoid "Module not found: Can't resolve 'fs'"
  68. // See: https://stackoverflow.com/a/68511591
  69. if (!options.isServer) {
  70. config.resolve.fallback.fs = false;
  71. }
  72. // See: https://webpack.js.org/configuration/externals/
  73. // This provides a way of excluding dependencies from the output bundles
  74. config.externals.push('dtrace-provider');
  75. config.externals.push('mongoose');
  76. // setup i18next-hmr
  77. if (!options.isServer && options.dev) {
  78. const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
  79. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  80. }
  81. return config;
  82. },
  83. };
  84. // production server
  85. if (phase === PHASE_PRODUCTION_SERVER) {
  86. return withSuperjson()(nextConfig);
  87. }
  88. const withTM = setupTranspileModules();
  89. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  90. enabled: phase === PHASE_PRODUCTION_BUILD || process.env.ANALYZE === 'true',
  91. });
  92. return withBundleAnalyzer(withTM(withSuperjson()(nextConfig)));
  93. };