next.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // define additional entries
  10. const additionalWebpackEntries = {
  11. boot: './src/client/boot',
  12. };
  13. const setupTranspileModules = () => {
  14. const eazyLogger = require('eazy-logger');
  15. const { listScopedPackages, listPrefixedPackages } = require('./src/utils/next.config.utils');
  16. // setup logger
  17. const logger = eazyLogger.Logger({
  18. prefix: '[{green:next.config.js}] ',
  19. useLevelPrefixes: false,
  20. });
  21. // define transpiled packages for '@growi/*'
  22. const packages = [
  23. ...listScopedPackages(['@growi'], { ignorePackageNames: ['@growi/app'] }),
  24. // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
  25. 'react-markdown',
  26. 'unified',
  27. 'comma-separated-tokens',
  28. 'decode-named-character-reference',
  29. 'html-void-elements',
  30. 'property-information',
  31. 'space-separated-tokens',
  32. 'trim-lines',
  33. 'web-namespaces',
  34. 'vfile',
  35. 'zwitch',
  36. 'emoticon',
  37. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'micromark-', 'unist-']),
  38. ];
  39. logger.info('{bold:Listing scoped packages for transpiling:}');
  40. logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  41. return require('next-transpile-modules')(packages);
  42. };
  43. module.exports = async(phase, { defaultConfig }) => {
  44. const { i18n, localePath } = require('./config/next-i18next.config');
  45. /** @type {import('next').NextConfig} */
  46. const nextConfig = {
  47. // == DOES NOT WORK
  48. // see: https://github.com/vercel/next.js/discussions/27876
  49. // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
  50. reactStrictMode: true,
  51. swcMinify: true,
  52. typescript: {
  53. tsconfigPath: 'tsconfig.build.client.json',
  54. },
  55. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  56. i18n,
  57. /** @param config {import('next').NextConfig} */
  58. webpack(config, options) {
  59. // Avoid "Module not found: Can't resolve 'fs'"
  60. // See: https://stackoverflow.com/a/68511591
  61. if (!options.isServer) {
  62. config.resolve.fallback.fs = false;
  63. }
  64. // See: https://webpack.js.org/configuration/externals/
  65. // This provides a way of excluding dependencies from the output bundles
  66. config.externals.push('dtrace-provider');
  67. config.externals.push('mongoose');
  68. // configure additional entries
  69. const orgEntry = config.entry;
  70. config.entry = () => {
  71. return orgEntry().then((entry) => {
  72. return { ...entry, ...additionalWebpackEntries };
  73. });
  74. };
  75. const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
  76. config.plugins.push(
  77. new WebpackManifestPlugin({
  78. fileName: 'custom-manifest.json',
  79. }),
  80. );
  81. // setup i18next-hmr
  82. if (!options.isServer && options.dev) {
  83. const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
  84. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  85. }
  86. return config;
  87. },
  88. };
  89. // production server
  90. if (phase === PHASE_PRODUCTION_SERVER) {
  91. return withSuperjson()(nextConfig);
  92. }
  93. const withTM = setupTranspileModules();
  94. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  95. enabled: phase === PHASE_PRODUCTION_BUILD || process.env.ANALYZE === 'true',
  96. });
  97. return withBundleAnalyzer(withTM(withSuperjson()(nextConfig)));
  98. };