Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardized dependency logic implementation #1998

Merged
merged 31 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d43e9c6
First draft: Unified dependency logic
RunDevelopment Jul 24, 2019
b57c3d1
Fix and test in index.html
RunDevelopment Jul 24, 2019
c2c1a33
Added tests
RunDevelopment Jul 24, 2019
e065553
Added native support for aliases
RunDevelopment Jul 30, 2019
f8803c9
Fixed types' scope
RunDevelopment Jul 30, 2019
bf81cfe
Resolved conflict
RunDevelopment Sep 3, 2019
382948b
More Doc + changed API
RunDevelopment Sep 3, 2019
c464239
Removed from index.html
RunDevelopment Sep 3, 2019
76e88a3
Fixed unknown ids and renames test file
RunDevelopment Sep 5, 2019
14d790a
Minor change
RunDevelopment Sep 5, 2019
479b372
More efficient implementation
RunDevelopment Sep 5, 2019
29fc8f9
Fixed npm test
RunDevelopment Sep 5, 2019
382ff4b
Merge branch 'master' into deps-update
RunDevelopment Sep 5, 2019
736103a
Added a check for circular dependencies
RunDevelopment Sep 5, 2019
3755eb6
Download page uses new dependency logic
RunDevelopment Sep 5, 2019
c0759f0
loadLanguages uses the new dependency logic
RunDevelopment Sep 5, 2019
839591c
Updated components.json
RunDevelopment Sep 5, 2019
1850b36
Resolved conflict
RunDevelopment Sep 5, 2019
bb12309
Added alias check
RunDevelopment Sep 6, 2019
6d59466
Extended deps test
RunDevelopment Sep 6, 2019
8fcf8b1
Test suite uses the new dependency logic
RunDevelopment Sep 6, 2019
946281f
Readded loadLanguages' load all languages behavior
RunDevelopment Sep 6, 2019
15ba68a
Added check for unknown dependency ids
RunDevelopment Sep 6, 2019
746a4dc
loadLanguages: Removed check for valid ids
RunDevelopment Sep 6, 2019
049f8c4
Removed type hack
RunDevelopment Oct 22, 2019
81e4b0d
Fixed async loading and added chainer
RunDevelopment Oct 22, 2019
148823d
Resolved conflict
RunDevelopment Oct 22, 2019
4ce06aa
String quote style
RunDevelopment Oct 22, 2019
6098aea
Merge branch 'master' into deps-update
RunDevelopment Dec 15, 2019
5275691
after → optional and some other renaming
RunDevelopment Dec 15, 2019
39ff76e
Fixed download page
RunDevelopment Dec 15, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

51 changes: 31 additions & 20 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"css": {
"title": "CSS",
"option": "default",
"peerDependencies": "markup"
"modify": "markup"
},
"clike": {
"title": "C-like",
Expand All @@ -70,7 +70,7 @@
"javascript": {
"title": "JavaScript",
"require": "clike",
"peerDependencies": "markup",
"modify": "markup",
"alias": "js",
"option": "default"
},
Expand All @@ -85,7 +85,7 @@
"actionscript": {
"title": "ActionScript",
"require": "javascript",
"peerDependencies": "markup",
"modify": "markup",
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
"owner": "Golmote"
},
"ada": {
Expand Down Expand Up @@ -222,6 +222,7 @@
"css-extras": {
"title": "CSS Extras",
"require": "css",
"modify": "css",
"owner": "milesj"
},
"d": {
Expand Down Expand Up @@ -351,7 +352,7 @@
"haml": {
"title": "Haml",
"require": "ruby",
"peerDependencies": [
"after": [
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
"css",
"css-extras",
"coffeescript",
Expand Down Expand Up @@ -386,8 +387,10 @@
},
"http": {
"title": "HTTP",
"peerDependencies": [
"after": [
"css",
"javascript",
"json",
"markup"
],
"owner": "danielgtaylor"
Expand Down Expand Up @@ -432,14 +435,17 @@
"javadoc": {
"title": "JavaDoc",
"require": ["markup", "java", "javadoclike"],
"peerDependencies": [
"modify": [
"java"
],
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
"after": [
"scala"
],
"owner": "RunDevelopment"
},
"javadoclike": {
"title": "JavaDoc-like",
"peerDependencies": [
"modify": [
"java",
"javascript",
"php"
Expand All @@ -462,7 +468,8 @@
"jsdoc": {
"title": "JSDoc",
"require": ["javascript", "javadoclike"],
"peerDependencies": [
"modify": "javascript",
"after": [
"actionscript",
"coffeescript"
],
Expand All @@ -471,7 +478,8 @@
"js-extras": {
"title": "JS Extras",
"require": "javascript",
"peerDependencies": [
"modify": "javascript",
"after": [
"actionscript",
"coffeescript",
"flow",
Expand All @@ -483,7 +491,8 @@
"js-templates": {
"title": "JS Templates",
"require": "javascript",
"peerDependencies": [
"modify": "javascript",
"after": [
"css",
"css-extras",
"graphql",
Expand Down Expand Up @@ -531,7 +540,7 @@
"less": {
"title": "Less",
"require": "css",
"peerDependencies": "css-extras",
"after": "css-extras",
"owner": "Golmote"
},
"lilypond": {
Expand Down Expand Up @@ -599,7 +608,7 @@
"n4js": {
"title": "N4JS",
"require": "javascript",
"peerDependencies": [
"after": [
"jsdoc"
],
"alias": "n4jsd",
Expand Down Expand Up @@ -642,7 +651,7 @@
"opencl": {
"title": "OpenCL",
"require": "cpp",
"peerDependencies": [
"modify": [
"c",
"cpp"
],
Expand Down Expand Up @@ -691,11 +700,13 @@
"phpdoc": {
"title": "PHPDoc",
"require": ["php", "javadoclike"],
"modify": "php",
"owner": "RunDevelopment"
},
"php-extras": {
"title": "PHP Extras",
"require": "php",
"modify": "php",
"owner": "milesj"
},
"plsql": {
Expand Down Expand Up @@ -728,7 +739,7 @@
"pug": {
"title": "Pug",
"require": ["markup", "javascript"],
"peerDependencies": [
"after": [
"coffeescript",
"ejs",
"handlebars",
Expand All @@ -747,7 +758,7 @@
},
"pure": {
"title": "Pure",
"peerDependencies": [
"after": [
"c",
"cpp",
"fortran"
Expand Down Expand Up @@ -775,7 +786,7 @@
"jsx": {
"title": "React JSX",
"require": ["markup", "javascript"],
"peerDependencies": [
"after": [
"jsdoc",
"js-extras",
"js-templates"
Expand All @@ -797,7 +808,7 @@
},
"regex": {
"title": "Regex",
"peerDependencies": [
"modify": [
"actionscript",
"coffeescript",
"flow",
Expand Down Expand Up @@ -847,7 +858,7 @@
"scss": {
"title": "Sass (Scss)",
"require": "css",
"peerDependencies": "css-extras",
"after": "css-extras",
"owner": "MoOx"
},
"scala": {
Expand Down Expand Up @@ -918,7 +929,7 @@
"textile": {
"title": "Textile",
"require": "markup",
"peerDependencies": "css",
"after": "css",
"owner": "Golmote"
},
"toml": {
Expand Down Expand Up @@ -946,7 +957,7 @@
"typescript": {
"title": "TypeScript",
"require": "javascript",
"peerDependencies": "js-templates",
"after": "js-templates",
"alias": "ts",
"owner": "vkbansal"
},
Expand Down
107 changes: 37 additions & 70 deletions components/index.js
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 getLoad = 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 `getLoad` will ignore invalid ones
const loaded = [...loadedLanguages, ...Object.keys(Prism.languages)];
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved

arr.forEach(function (language) {
if (!components.languages[language]) {
console.warn('Language does not exist ' + language);
getLoad(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;
Loading