utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const fs = require('fs');
  2. const path = require('path');
  3. exports.contains = function contains(arr, val) {
  4. return arr && arr.indexOf(val) !== -1;
  5. };
  6. const atPrefix = new RegExp('^@', 'g');
  7. exports.readDir = function readDir(dirName) {
  8. if (!fs.existsSync(dirName)) {
  9. return [];
  10. }
  11. try {
  12. return fs
  13. .readdirSync(dirName)
  14. .map(function (module) {
  15. if (atPrefix.test(module)) {
  16. // reset regexp
  17. atPrefix.lastIndex = 0;
  18. try {
  19. return fs
  20. .readdirSync(path.join(dirName, module))
  21. .map(function (scopedMod) {
  22. return module + '/' + scopedMod;
  23. });
  24. } catch (e) {
  25. return [module];
  26. }
  27. }
  28. return module;
  29. })
  30. .reduce(function (prev, next) {
  31. return prev.concat(next);
  32. }, []);
  33. } catch (e) {
  34. return [];
  35. }
  36. };
  37. exports.readFromPackageJson = function readFromPackageJson(options) {
  38. if (typeof options !== 'object') {
  39. options = {};
  40. }
  41. const includeInBundle = options.exclude || options.includeInBundle;
  42. const excludeFromBundle = options.include || options.excludeFromBundle;
  43. // read the file
  44. let packageJson;
  45. try {
  46. const fileName = options.fileName || 'package.json';
  47. const packageJsonString = fs.readFileSync(
  48. path.resolve(process.cwd(), fileName),
  49. 'utf8'
  50. );
  51. packageJson = JSON.parse(packageJsonString);
  52. } catch (e) {
  53. return [];
  54. }
  55. // sections to search in package.json
  56. let sections = [
  57. 'dependencies',
  58. 'devDependencies',
  59. 'peerDependencies',
  60. 'optionalDependencies',
  61. ];
  62. if (excludeFromBundle) {
  63. sections = [].concat(excludeFromBundle);
  64. }
  65. if (includeInBundle) {
  66. sections = sections.filter(function (section) {
  67. return [].concat(includeInBundle).indexOf(section) === -1;
  68. });
  69. }
  70. // collect dependencies
  71. const deps = {};
  72. sections.forEach(function (section) {
  73. Object.keys(packageJson[section] || {}).forEach(function (dep) {
  74. deps[dep] = true;
  75. });
  76. });
  77. return Object.keys(deps);
  78. };
  79. exports.containsPattern = function containsPattern(arr, val) {
  80. return (
  81. arr &&
  82. arr.some(function (pattern) {
  83. if (pattern instanceof RegExp) {
  84. return pattern.test(val);
  85. } else if (typeof pattern === 'function') {
  86. return pattern(val);
  87. } else {
  88. return pattern == val;
  89. }
  90. })
  91. );
  92. };
  93. exports.validateOptions = function (options) {
  94. options = options || {};
  95. const results = [];
  96. const mistakes = {
  97. allowlist: ['allowslist', 'whitelist', 'allow'],
  98. importType: ['import', 'importype', 'importtype'],
  99. modulesDir: ['moduledir', 'moduledirs'],
  100. modulesFromFile: ['modulesfile'],
  101. includeAbsolutePaths: ['includeAbsolutesPaths'],
  102. additionalModuleDirs: ['additionalModulesDirs', 'additionalModulesDir'],
  103. };
  104. const optionsKeys = Object.keys(options);
  105. const optionsKeysLower = optionsKeys.map(function (optionName) {
  106. return optionName && optionName.toLowerCase();
  107. });
  108. Object.keys(mistakes).forEach(function (correctTerm) {
  109. if (!options.hasOwnProperty(correctTerm)) {
  110. mistakes[correctTerm]
  111. .concat(correctTerm.toLowerCase())
  112. .forEach(function (mistake) {
  113. const ind = optionsKeysLower.indexOf(mistake.toLowerCase());
  114. if (ind > -1) {
  115. results.push({
  116. message: `Option '${optionsKeys[ind]}' is not supported. Did you mean '${correctTerm}'?`,
  117. wrongTerm: optionsKeys[ind],
  118. correctTerm: correctTerm,
  119. });
  120. }
  121. });
  122. }
  123. });
  124. return results;
  125. };
  126. exports.log = function (message) {
  127. console.log(`[webpack-node-externals] : ${message}`);
  128. };
  129. exports.error = function (errors) {
  130. throw new Error(
  131. errors
  132. .map(function (error) {
  133. return `[webpack-node-externals] : ${error}`;
  134. })
  135. .join('\r\n')
  136. );
  137. };