No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var components = require('../components.js');
  2. var peerDependentsMap = null;
  3. function getPeerDependentsMap() {
  4. var peerDependentsMap = {};
  5. Object.keys(components.languages).forEach(function (language) {
  6. if (language === 'meta') {
  7. return false;
  8. }
  9. if (components.languages[language].peerDependencies) {
  10. var peerDependencies = components.languages[language].peerDependencies;
  11. if (!Array.isArray(peerDependencies)) {
  12. peerDependencies = [peerDependencies];
  13. }
  14. peerDependencies.forEach(function (peerDependency) {
  15. if (!peerDependentsMap[peerDependency]) {
  16. peerDependentsMap[peerDependency] = [];
  17. }
  18. peerDependentsMap[peerDependency].push(language);
  19. });
  20. }
  21. });
  22. return peerDependentsMap;
  23. }
  24. function getPeerDependents(mainLanguage) {
  25. if (!peerDependentsMap) {
  26. peerDependentsMap = getPeerDependentsMap();
  27. }
  28. return peerDependentsMap[mainLanguage] || [];
  29. }
  30. function loadLanguages(arr, withoutDependencies) {
  31. // If no argument is passed, load all components
  32. if (!arr) {
  33. arr = Object.keys(components.languages).filter(function (language) {
  34. return language !== 'meta';
  35. });
  36. }
  37. if (arr && !arr.length) {
  38. return;
  39. }
  40. if (!Array.isArray(arr)) {
  41. arr = [arr];
  42. }
  43. arr.forEach(function (language) {
  44. if (!components.languages[language]) {
  45. console.warn('Language does not exist ' + language);
  46. return;
  47. }
  48. // Load dependencies first
  49. if (!withoutDependencies && components.languages[language].require) {
  50. loadLanguages(components.languages[language].require);
  51. }
  52. var pathToLanguage = './prism-' + language;
  53. delete require.cache[require.resolve(pathToLanguage)];
  54. delete Prism.languages[language];
  55. require(pathToLanguage);
  56. // Reload dependents
  57. var dependents = getPeerDependents(language).filter(function (dependent) {
  58. // If dependent language was already loaded,
  59. // we want to reload it.
  60. if (Prism.languages[dependent]) {
  61. delete Prism.languages[dependent];
  62. return true;
  63. }
  64. return false;
  65. });
  66. if (dependents.length) {
  67. loadLanguages(dependents, true);
  68. }
  69. });
  70. }
  71. module.exports = function (arr) {
  72. // Don't expose withoutDependencies
  73. loadLanguages(arr);
  74. };