2
0

next.config.js 3.3 KB

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