-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardized dependency logic implementation (#1998)
This adds a standard logic for handling dependencies between Prism components. Right now, the download page, the `loadLanguages` function, and the test suite use the new dependency system.
- Loading branch information
1 parent
a7f7009
commit 7a4a0c7
Showing
9 changed files
with
860 additions
and
197 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,49 @@ | ||
var components = require('../components.js'); | ||
var peerDependentsMap = null; | ||
|
||
function getPeerDependentsMap() { | ||
var peerDependentsMap = {}; | ||
Object.keys(components.languages).forEach(function (language) { | ||
if (language === 'meta') { | ||
return false; | ||
} | ||
if (components.languages[language].peerDependencies) { | ||
var peerDependencies = components.languages[language].peerDependencies; | ||
if (!Array.isArray(peerDependencies)) { | ||
peerDependencies = [peerDependencies]; | ||
} | ||
peerDependencies.forEach(function (peerDependency) { | ||
if (!peerDependentsMap[peerDependency]) { | ||
peerDependentsMap[peerDependency] = []; | ||
} | ||
peerDependentsMap[peerDependency].push(language); | ||
}); | ||
} | ||
}); | ||
return peerDependentsMap; | ||
} | ||
|
||
function getPeerDependents(mainLanguage) { | ||
if (!peerDependentsMap) { | ||
peerDependentsMap = getPeerDependentsMap(); | ||
const components = require('../components.js'); | ||
const getLoader = require('../dependencies'); | ||
|
||
|
||
/** | ||
* The set of all languages which have been loaded using the below function. | ||
* | ||
* @type {Set<string>} | ||
*/ | ||
const loadedLanguages = new Set(); | ||
|
||
/** | ||
* Loads the given languages and adds them to the current Prism instance. | ||
* | ||
* If no languages are provided, __all__ Prism languages will be loaded. | ||
* | ||
* @param {string|string[]} [languages] | ||
* @returns {void} | ||
*/ | ||
function loadLanguages(languages) { | ||
if (languages === undefined) { | ||
languages = Object.keys(components.languages).filter(l => l != 'meta'); | ||
} else if (!Array.isArray(languages)) { | ||
languages = [languages]; | ||
} | ||
return peerDependentsMap[mainLanguage] || []; | ||
} | ||
|
||
function loadLanguages(arr, withoutDependencies) { | ||
// If no argument is passed, load all components | ||
if (!arr) { | ||
arr = Object.keys(components.languages).filter(function (language) { | ||
return language !== 'meta'; | ||
}); | ||
} | ||
if (arr && !arr.length) { | ||
return; | ||
} | ||
|
||
if (!Array.isArray(arr)) { | ||
arr = [arr]; | ||
} | ||
// the user might have loaded languages via some other way or used `prism.js` which already includes some | ||
// we don't need to validate the ids because `getLoader` will ignore invalid ones | ||
const loaded = [...loadedLanguages, ...Object.keys(Prism.languages)]; | ||
|
||
arr.forEach(function (language) { | ||
if (!components.languages[language]) { | ||
console.warn('Language does not exist ' + language); | ||
getLoader(components, languages, loaded).load(lang => { | ||
if (!(lang in components.languages)) { | ||
console.warn('Language does not exist: ' + lang); | ||
return; | ||
} | ||
// Load dependencies first | ||
if (!withoutDependencies && components.languages[language].require) { | ||
loadLanguages(components.languages[language].require); | ||
} | ||
|
||
var pathToLanguage = './prism-' + language; | ||
const pathToLanguage = './prism-' + lang; | ||
|
||
// remove from require cache and from Prism | ||
delete require.cache[require.resolve(pathToLanguage)]; | ||
delete Prism.languages[language]; | ||
delete Prism.languages[lang]; | ||
|
||
require(pathToLanguage); | ||
|
||
// Reload dependents | ||
var dependents = getPeerDependents(language).filter(function (dependent) { | ||
// If dependent language was already loaded, | ||
// we want to reload it. | ||
if (Prism.languages[dependent]) { | ||
delete Prism.languages[dependent]; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (dependents.length) { | ||
loadLanguages(dependents, true); | ||
} | ||
loadedLanguages.add(lang); | ||
}); | ||
} | ||
|
||
module.exports = function (arr) { | ||
// Don't expose withoutDependencies | ||
loadLanguages(arr); | ||
}; | ||
module.exports = loadLanguages; |
Oops, something went wrong.