next.config.utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  6. * @typedef { { ignorePackageNames: string[] } } Opts
  7. */
  8. /** @type {Opts} */
  9. const defaultOpts = { ignorePackageNames: [] };
  10. /**
  11. * @param scopes {string[]}
  12. */
  13. exports.listScopedPackages = (scopes, opts = defaultOpts) => {
  14. /** @type {string[]} */
  15. const scopedPackages = [];
  16. fs.readdirSync(nodeModulesPath)
  17. .filter(name => scopes.includes(name))
  18. .forEach((scope) => {
  19. fs.readdirSync(path.resolve(nodeModulesPath, scope))
  20. .filter(name => !name.startsWith('.'))
  21. .forEach((folderName) => {
  22. const { name } = require(path.resolve(
  23. nodeModulesPath,
  24. scope,
  25. folderName,
  26. 'package.json',
  27. ));
  28. if (!opts.ignorePackageNames.includes(name)) {
  29. scopedPackages.push(name);
  30. }
  31. });
  32. });
  33. return scopedPackages;
  34. };
  35. /**
  36. * @param prefixes {string[]}
  37. */
  38. exports.listPrefixedPackages = (prefixes, opts = defaultOpts) => {
  39. /** @type {string[]} */
  40. const prefixedPackages = [];
  41. fs.readdirSync(nodeModulesPath)
  42. .filter(name => prefixes.some(prefix => name.startsWith(prefix)))
  43. .filter(name => !name.startsWith('.'))
  44. .forEach((folderName) => {
  45. const { name } = require(path.resolve(
  46. nodeModulesPath,
  47. folderName,
  48. 'package.json',
  49. ));
  50. if (!opts.ignorePackageNames.includes(name)) {
  51. prefixedPackages.push(name);
  52. }
  53. });
  54. return prefixedPackages;
  55. };