2
0

next.config.js 3.8 KB

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