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

Update logic to rearrange based on peer deps #11

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 40 additions & 21 deletions src/getComponents.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import config from 'prismjs/components.js';

const getPath = (type, name) =>
`prismjs/${config[`${type}`].meta.path.replace(/\{id\}/g, name)}`;
const getPath = type => name =>
`prismjs/${config[type].meta.path.replace(/\{id\}/g, name)}`;

const getRequire = (type, name) => config[type][name].require;
const getPeers = (type, name) => config[type][name].peerDependencies;
const getNoCSS = (type, name) => config[type][name].noCSS;

const getTheme = theme => {
Expand All @@ -13,39 +14,57 @@ const getTheme = theme => {
theme = `prism-${theme}`;
}

return getPath('themes', theme);
return getPath('themes')(theme);
};

const getDependencies = (type, css) => function getDependencies(deps, require) {
if (!require) {
const getDependencies = (type) => function getDependencies(deps, newDeps) {
if (!newDeps) {
return deps;
}

if (!Array.isArray(require)) {
require = [require];
if (!Array.isArray(newDeps)) {
newDeps = [newDeps];
}

return require.reduce((deps, name) => {
deps = getDependencies(deps, getRequire(type, name));
deps = newDeps.reduce((deps, dep) => {
deps = getDependencies(deps, getRequire(type, dep));

if (deps.indexOf(getPath(type, name)) === -1) {
const add = [];

if (css && type === 'plugins' && !getNoCSS(type, name)) {
add.push(getPath(type, name) + '.css');
}

add.push(getPath(type, name));

deps = [...deps, ...add];
if (!deps.includes(dep)) {
deps.push(dep);
}

return deps;
}, deps);

if (type === 'languages') {
deps.sort((a, b) => {
const aPeers = getPeers(type, a);
const bPeers = getPeers(type, b);

if (aPeers && aPeers.includes(b)) {
return 1;
}

if (bPeers && bPeers.includes(a)) {
return -1;
}
return 0;
});
}

return deps;
};

export default ({ languages = [], plugins = [], theme, css = false } = {}) => [
...languages.reduce(getDependencies('languages'), []),
...plugins.reduce(getDependencies('plugins', css), []),
...languages.reduce(getDependencies('languages'), []).map(getPath('languages')),
...plugins.reduce(getDependencies('plugins'), []).reduce((deps, dep) => {
const add = [getPath('plugins')(dep)];

if (css && !getNoCSS('plugins', dep)) {
add.unshift(getPath('plugins')(dep) + '.css');
}

return [...deps, ...add];
}, []),
...(css && theme ? [getTheme(theme)] : [])
];
4 changes: 4 additions & 0 deletions test/fixtures/one-language-with-peer-dep/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Prism from "prismjs/components/prism-core";
import "prismjs/components/prism-clike";
import "prismjs/components/prism-markup";
import "prismjs/components/prism-javascript";
3 changes: 3 additions & 0 deletions test/fixtures/one-language-with-peer-dep/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"languages": ["javascript", "markup"]
}
2 changes: 1 addition & 1 deletion test/fixtures/several-languages/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Prism from "prismjs/components/prism-core";
import "prismjs/components/prism-clike";
import "prismjs/components/prism-javascript";
import "prismjs/components/prism-markup";
import "prismjs/components/prism-javascript";
import "prismjs/components/prism-jsx";
import "prismjs/components/prism-reason";