next.config.js 3.8 KB

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