next.config.js 3.5 KB

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