next.config.js 2.8 KB

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