next.config.js 3.1 KB

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