next.config.utils.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // workaround by https://github.com/martpie/next-transpile-modules/issues/143#issuecomment-817467144
  2. const fs = require('fs');
  3. const path = require('path');
  4. const nodeModulesPath = path.resolve(__dirname, '../../../../node_modules');
  5. const defaultOpts = { ignorePackageNames: [] };
  6. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  7. export const listScopedPackages = (scopes, opts = defaultOpts) => {
  8. const scopedPackages = [];
  9. fs.readdirSync(nodeModulesPath)
  10. .filter(name => scopes.includes(name))
  11. .forEach((scope) => {
  12. fs.readdirSync(path.resolve(nodeModulesPath, scope))
  13. .filter(name => !name.startsWith('.'))
  14. .forEach((folderName) => {
  15. const { name, ignoreTranspileModules } = require(path.resolve(
  16. nodeModulesPath,
  17. scope,
  18. folderName,
  19. 'package.json',
  20. ));
  21. if (!ignoreTranspileModules && !opts.ignorePackageNames.includes(name)) {
  22. scopedPackages.push(name);
  23. }
  24. });
  25. });
  26. return scopedPackages;
  27. };
  28. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  29. export const listPrefixedPackages = (prefixes, opts = defaultOpts) => {
  30. const prefixedPackages = [];
  31. fs.readdirSync(nodeModulesPath)
  32. .filter(name => prefixes.some(prefix => name.startsWith(prefix)))
  33. .filter(name => !name.startsWith('.'))
  34. .forEach((folderName) => {
  35. const { name, ignoreTranspileModules } = require(path.resolve(
  36. nodeModulesPath,
  37. folderName,
  38. 'package.json',
  39. ));
  40. if (!ignoreTranspileModules && !opts.ignorePackageNames.includes(name)) {
  41. prefixedPackages.push(name);
  42. }
  43. });
  44. return prefixedPackages;
  45. };