next.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 eazyLogger = require('eazy-logger');
  8. const { withSuperjson } = require('next-superjson');
  9. const { i18n, localePath } = require('./src/next-i18next.config');
  10. const isProduction = process.env.NODE_ENV === 'production';
  11. let isServer = true;
  12. // define additional entries
  13. const additionalWebpackEntries = {
  14. boot: './src/client/boot',
  15. };
  16. /** @type {import('next').NextConfig} */
  17. const nextConfig = {
  18. // == DOES NOT WORK
  19. // see: https://github.com/vercel/next.js/discussions/27876
  20. // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
  21. reactStrictMode: true,
  22. typescript: {
  23. tsconfigPath: 'tsconfig.build.client.json',
  24. },
  25. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  26. i18n,
  27. /** @param config {import('next').NextConfig} */
  28. webpack(config, options) {
  29. isServer = options.isServer;
  30. // Avoid "Module not found: Can't resolve 'fs'"
  31. // See: https://stackoverflow.com/a/68511591
  32. if (!isServer) {
  33. config.resolve.fallback.fs = false;
  34. }
  35. // See: https://webpack.js.org/configuration/externals/
  36. // This provides a way of excluding dependencies from the output bundles
  37. config.externals.push('dtrace-provider');
  38. // configure additional entries
  39. const orgEntry = config.entry;
  40. config.entry = () => {
  41. return orgEntry().then((entry) => {
  42. return { ...entry, ...additionalWebpackEntries };
  43. });
  44. };
  45. const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
  46. config.plugins.push(
  47. new WebpackManifestPlugin({
  48. fileName: 'custom-manifest.json',
  49. }),
  50. );
  51. // setup i18next-hmr
  52. if (!options.isServer && options.dev) {
  53. const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
  54. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  55. }
  56. return config;
  57. },
  58. };
  59. const passThrough = nextConfig => nextConfig;
  60. let withTM = passThrough;
  61. if (!isProduction || !isServer) {
  62. const { listScopedPackages, listPrefixedPackages } = require('./src/utils/next.config.utils');
  63. // setup logger
  64. const logger = eazyLogger.Logger({
  65. prefix: '[{green:next.config.js}] ',
  66. useLevelPrefixes: false,
  67. });
  68. const setupWithTM = () => {
  69. // define transpiled packages for '@growi/*'
  70. const packages = [
  71. ...listScopedPackages(['@growi'], { ignorePackageNames: ['@growi/app'] }),
  72. // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
  73. 'react-markdown',
  74. 'unified',
  75. 'comma-separated-tokens',
  76. 'decode-named-character-reference',
  77. 'html-void-elements',
  78. 'property-information',
  79. 'space-separated-tokens',
  80. 'trim-lines',
  81. 'web-namespaces',
  82. 'vfile',
  83. 'zwitch',
  84. 'emoticon',
  85. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'micromark-', 'unist-']),
  86. ];
  87. logger.info('{bold:Listing scoped packages for transpiling:}');
  88. logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  89. return require('next-transpile-modules')(packages);
  90. };
  91. withTM = setupWithTM();
  92. }
  93. module.exports = withSuperjson()(withTM(nextConfig));