diff --git a/package-lock.json b/package-lock.json index 559107c..1f06e96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1599,9 +1599,9 @@ "dev": true }, "clipboard": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/clipboard/-/clipboard-2.0.1.tgz", - "integrity": "sha512-7yhQBmtN+uYZmfRjjVjKa0dZdWuabzpSKGtyQZN+9C8xlC788SSJjOHWh7tzurfwTqTD5UDYAhIv5fRJg3sHjQ==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/clipboard/-/clipboard-2.0.4.tgz", + "integrity": "sha512-Vw26VSLRpJfBofiVaFb/I8PVfdI1OxKcYShe6fm0sP/DtmiWQNCjhM/okTvdCo0G+lMMm1rMYbk4IK4x1X+kgQ==", "dev": true, "optional": true, "requires": { @@ -3721,9 +3721,9 @@ "dev": true }, "prismjs": { - "version": "1.14.0", - "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.14.0.tgz", - "integrity": "sha512-sa2s4m60bXs+kU3jcuUwx3ZCrUH7o0kuqnOOINbODqlRrDB7KY8SRx+xR/D7nHLlgfDdG7zXbRO8wJ+su5Ls0A==", + "version": "1.16.0", + "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.16.0.tgz", + "integrity": "sha512-OA4MKxjFZHSvZcisLGe14THYsug/nF6O1f0pAJc0KN0wTyAcLqmsbE+lTGKSpyh+9pEW57+k6pg2AfYR+coyHA==", "dev": true, "requires": { "clipboard": "^2.0.0" @@ -4355,9 +4355,9 @@ "dev": true }, "tiny-emitter": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.0.2.tgz", - "integrity": "sha512-2NM0auVBGft5tee/OxP4PI3d8WItkDM+fPnaRAVo6xTDI2knbz9eC5ArWGqtGlYqiH3RU5yMpdyTTO7MguC4ow==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz", + "integrity": "sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==", "dev": true, "optional": true }, diff --git a/src/getComponents.js b/src/getComponents.js index 2114dcd..08f83e8 100644 --- a/src/getComponents.js +++ b/src/getComponents.js @@ -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 => { @@ -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)] : []) ]; diff --git a/test/fixtures/one-language-with-peer-dep/expected.js b/test/fixtures/one-language-with-peer-dep/expected.js new file mode 100644 index 0000000..1ef6ec5 --- /dev/null +++ b/test/fixtures/one-language-with-peer-dep/expected.js @@ -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"; diff --git a/test/fixtures/one-language-with-peer-dep/options.json b/test/fixtures/one-language-with-peer-dep/options.json new file mode 100644 index 0000000..a85b3af --- /dev/null +++ b/test/fixtures/one-language-with-peer-dep/options.json @@ -0,0 +1,3 @@ +{ + "languages": ["javascript", "markup"] +} diff --git a/test/fixtures/several-languages/expected.js b/test/fixtures/several-languages/expected.js index 6c93a2a..68d904e 100644 --- a/test/fixtures/several-languages/expected.js +++ b/test/fixtures/several-languages/expected.js @@ -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";