next.config.js 3.4 KB

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