next.config.utils.ts 1.7 KB

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