next.config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'bail',
  18. 'ccount',
  19. 'character-entities',
  20. 'character-entities-html4',
  21. 'character-entities-legacy',
  22. 'comma-separated-tokens',
  23. 'decode-named-character-reference',
  24. 'escape-string-regexp',
  25. 'hastscript',
  26. 'html-void-elements',
  27. 'is-absolute-url',
  28. 'longest-streak',
  29. 'micromark',
  30. 'property-information',
  31. 'space-separated-tokens',
  32. 'stringify-entities',
  33. 'trim-lines',
  34. 'trough',
  35. 'web-namespaces',
  36. 'vfile',
  37. 'vfile-location',
  38. 'vfile-message',
  39. 'zwitch',
  40. 'emoticon',
  41. 'direction', // for hast-util-select
  42. 'bcp-47-match', // for hast-util-select
  43. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'unist-']),
  44. ];
  45. // const eazyLogger = require('eazy-logger');
  46. // const logger = eazyLogger.Logger({
  47. // prefix: '[{green:next.config.js}] ',
  48. // useLevelPrefixes: false,
  49. // });
  50. // logger.info('{bold:Listing scoped packages for transpiling:}');
  51. // logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  52. return packages;
  53. };
  54. module.exports = async(phase, { defaultConfig }) => {
  55. const { i18n, localePath } = require('./config/next-i18next.config');
  56. /** @type {import('next').NextConfig} */
  57. const nextConfig = {
  58. reactStrictMode: true,
  59. poweredByHeader: false,
  60. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  61. i18n,
  62. // for build
  63. eslint: {
  64. ignoreDuringBuilds: true,
  65. },
  66. typescript: {
  67. tsconfigPath: 'tsconfig.build.client.json',
  68. },
  69. transpilePackages: phase !== PHASE_PRODUCTION_SERVER
  70. ? getTranspilePackages()
  71. : undefined,
  72. /** @param config {import('next').NextConfig} */
  73. webpack(config, options) {
  74. if (!options.isServer) {
  75. // Avoid "Module not found: Can't resolve 'fs'"
  76. // See: https://stackoverflow.com/a/68511591
  77. config.resolve.fallback.fs = false;
  78. // exclude packages from the output bundles
  79. config.module.rules.push(
  80. ...[
  81. /dtrace-provider/,
  82. /mongoose/,
  83. /mathjax-full/, // required from marp
  84. ].map((packageRegExp) => {
  85. return {
  86. test: packageRegExp,
  87. use: 'null-loader',
  88. };
  89. }),
  90. );
  91. }
  92. // extract sourcemap
  93. if (options.dev) {
  94. config.module.rules.push({
  95. test: /.(c|m)?js$/,
  96. exclude: [/node_modules/, path.resolve(__dirname)],
  97. enforce: 'pre',
  98. use: ['source-map-loader'],
  99. });
  100. }
  101. // setup i18next-hmr
  102. if (!options.isServer && options.dev) {
  103. const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
  104. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  105. }
  106. return config;
  107. },
  108. };
  109. // production server
  110. if (phase === PHASE_PRODUCTION_SERVER) {
  111. return withSuperjson()(nextConfig);
  112. }
  113. const withBundleAnalyzer = require('@next/bundle-analyzer')({
  114. enabled: phase === PHASE_PRODUCTION_BUILD || process.env.ANALYZE === 'true',
  115. });
  116. return withBundleAnalyzer(withSuperjson()(nextConfig));
  117. };