next.config.js 2.8 KB

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