next.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 'space-separated-tokens',
  21. 'trim-lines',
  22. 'emoticon',
  23. ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'micromark-', 'unist-']),
  24. ];
  25. logger.info('{bold:Listing scoped packages for transpiling:}');
  26. logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
  27. return require('next-transpile-modules')(packages);
  28. };
  29. const withTM = setupWithTM();
  30. // define additional entries
  31. const additionalWebpackEntries = {
  32. boot: './src/client/boot',
  33. };
  34. /** @type {import('next').NextConfig} */
  35. const nextConfig = {
  36. // == DOES NOT WORK
  37. // see: https://github.com/vercel/next.js/discussions/27876
  38. // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
  39. reactStrictMode: true,
  40. typescript: {
  41. tsconfigPath: 'tsconfig.build.client.json',
  42. },
  43. pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  44. i18n,
  45. /** @param config {import('next').NextConfig} */
  46. webpack(config, options) {
  47. // Avoid "Module not found: Can't resolve 'fs'"
  48. // See: https://stackoverflow.com/a/68511591
  49. if (!options.isServer) {
  50. config.resolve.fallback.fs = false;
  51. }
  52. // See: https://webpack.js.org/configuration/externals/
  53. // This provides a way of excluding dependencies from the output bundles
  54. config.externals.push('dtrace-provider');
  55. // configure additional entries
  56. const orgEntry = config.entry;
  57. config.entry = () => {
  58. return orgEntry().then((entry) => {
  59. return { ...entry, ...additionalWebpackEntries };
  60. });
  61. };
  62. config.plugins.push(
  63. new WebpackManifestPlugin({
  64. fileName: 'custom-manifest.json',
  65. }),
  66. );
  67. // setup i18next-hmr
  68. if (!options.isServer && options.dev) {
  69. config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
  70. }
  71. return config;
  72. },
  73. };
  74. module.exports = withTM(nextConfig);