|
| 1 | +// @ts-check |
| 2 | +const { warn } = require('./logger') |
| 3 | + |
| 4 | +/** @typedef {{after?: string|Array<string>}} Apply */ |
| 5 | +/** @typedef {{id: string, apply: Apply}} Plugin */ |
| 6 | +/** @typedef {{after: Set<string>}} OrderParams */ |
| 7 | + |
| 8 | +/** @type {Map<string, OrderParams>} */ |
| 9 | +const orderParamsCache = new Map() |
| 10 | + |
| 11 | +/** |
| 12 | + * |
| 13 | + * @param {Plugin} plugin |
| 14 | + * @returns {OrderParams} |
| 15 | + */ |
| 16 | +function getOrderParams (plugin) { |
| 17 | + if (!process.env.VUE_CLI_TEST && orderParamsCache.has(plugin.id)) { |
| 18 | + return orderParamsCache.get(plugin.id) |
| 19 | + } |
| 20 | + const apply = plugin.apply |
| 21 | + |
| 22 | + let after = new Set() |
| 23 | + if (typeof apply.after === 'string') { |
| 24 | + after = new Set([apply.after]) |
| 25 | + } else if (Array.isArray(apply.after)) { |
| 26 | + after = new Set(apply.after) |
| 27 | + } |
| 28 | + if (!process.env.VUE_CLI_TEST) { |
| 29 | + orderParamsCache.set(plugin.id, { after }) |
| 30 | + } |
| 31 | + |
| 32 | + return { after } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * See leetcode 210 |
| 37 | + * @param {Array<Plugin>} plugins |
| 38 | + * @returns {Array<Plugin>} |
| 39 | + */ |
| 40 | +function topologicalSorting (plugins) { |
| 41 | + /** @type {Map<string, Plugin>} */ |
| 42 | + const pluginsMap = new Map(plugins.map(p => [p.id, p])) |
| 43 | + |
| 44 | + /** @type {Map<Plugin, number>} */ |
| 45 | + const indegrees = new Map() |
| 46 | + |
| 47 | + /** @type {Map<Plugin, Array<Plugin>>} */ |
| 48 | + const graph = new Map() |
| 49 | + |
| 50 | + plugins.forEach(p => { |
| 51 | + const after = getOrderParams(p).after |
| 52 | + indegrees.set(p, after.size) |
| 53 | + if (after.size === 0) return |
| 54 | + for (const id of after) { |
| 55 | + const prerequisite = pluginsMap.get(id) |
| 56 | + // remove invalid data |
| 57 | + if (!prerequisite) { |
| 58 | + indegrees.set(p, indegrees.get(p) - 1) |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + if (!graph.has(prerequisite)) { |
| 63 | + graph.set(prerequisite, []) |
| 64 | + } |
| 65 | + graph.get(prerequisite).push(p) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + const res = [] |
| 70 | + const queue = [] |
| 71 | + indegrees.forEach((d, p) => { |
| 72 | + if (d === 0) queue.push(p) |
| 73 | + }) |
| 74 | + while (queue.length) { |
| 75 | + const cur = queue.shift() |
| 76 | + res.push(cur) |
| 77 | + const neighbors = graph.get(cur) |
| 78 | + if (!neighbors) continue |
| 79 | + |
| 80 | + neighbors.forEach(n => { |
| 81 | + const degree = indegrees.get(n) - 1 |
| 82 | + indegrees.set(n, degree) |
| 83 | + if (degree === 0) { |
| 84 | + queue.push(n) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + const valid = res.length === plugins.length |
| 89 | + if (!valid) { |
| 90 | + warn(`No proper plugin execution order found.`) |
| 91 | + return plugins |
| 92 | + } |
| 93 | + return res |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Arrange plugins by 'after' property. |
| 98 | + * @param {Array<Plugin>} plugins |
| 99 | + * @returns {Array<Plugin>} |
| 100 | + */ |
| 101 | +function sortPlugins (plugins) { |
| 102 | + if (plugins.length < 2) return plugins |
| 103 | + |
| 104 | + return topologicalSorting(plugins) |
| 105 | +} |
| 106 | + |
| 107 | +module.exports = { |
| 108 | + topologicalSorting, |
| 109 | + sortPlugins |
| 110 | +} |
0 commit comments