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