@modern-js/plugin 0.0.0-next-20221121124710 → 0.0.0-next-20221122140537
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -3
- package/dist/js/modern/manager/shared.js +3 -23
- package/dist/js/modern/utils/pluginDagSort.js +52 -0
- package/dist/js/node/manager/shared.js +4 -23
- package/dist/js/node/utils/pluginDagSort.js +61 -0
- package/dist/js/treeshaking/manager/shared.js +2 -46
- package/dist/js/treeshaking/utils/pluginDagSort.js +79 -0
- package/dist/types/utils/pluginDagSort.d.ts +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
# @modern-js/plugin
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20221122140537
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- dda38c9c: chore: v2
|
|
8
8
|
|
|
9
9
|
### Patch Changes
|
|
10
10
|
|
|
11
|
-
-
|
|
11
|
+
- b8bbe036: feat: change type logic
|
|
12
12
|
feat: 修改类型相关的逻辑
|
|
13
|
+
- f1797493: fix: the sortPlugins api not work
|
|
14
|
+
|
|
15
|
+
fix: sortPlugins 没有真实生效
|
|
13
16
|
|
|
14
17
|
## 2.0.0-beta.0
|
|
15
18
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { dagSort } from "../utils/pluginDagSort";
|
|
1
2
|
export const checkPlugins = plugins => {
|
|
2
3
|
plugins.forEach(origin => {
|
|
3
4
|
origin.rivals.forEach(rival => {
|
|
@@ -15,29 +16,8 @@ export const checkPlugins = plugins => {
|
|
|
15
16
|
});
|
|
16
17
|
};
|
|
17
18
|
export function sortPlugins(input) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
for (let i = 0; i < plugins.length; i++) {
|
|
21
|
-
const plugin = plugins[i];
|
|
22
|
-
|
|
23
|
-
for (const pre of plugin.pre) {
|
|
24
|
-
for (let j = i + 1; j < plugins.length; j++) {
|
|
25
|
-
if (plugins[j].name === pre) {
|
|
26
|
-
plugins = [...plugins.slice(0, i), plugins[j], ...plugins.slice(i, j), ...plugins.slice(j + 1, plugins.length)];
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
for (const post of plugin.post) {
|
|
32
|
-
for (let j = 0; j < i; j++) {
|
|
33
|
-
if (plugins[j].name === post) {
|
|
34
|
-
plugins = [...plugins.slice(0, j), ...plugins.slice(j + 1, i + 1), plugins[j], ...plugins.slice(i + 1, plugins.length)];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return plugins;
|
|
19
|
+
const plugins = input.slice();
|
|
20
|
+
return dagSort(plugins);
|
|
41
21
|
}
|
|
42
22
|
export const includePlugin = (plugins, input) => plugins.some(plugin => plugin.name === input.name);
|
|
43
23
|
export const isObject = obj => obj !== null && typeof obj === 'object';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const dagSort = (plugins, key = 'name', preKey = 'pre', postKey = 'post') => {
|
|
2
|
+
let allLines = [];
|
|
3
|
+
|
|
4
|
+
function getPluginByAny(q) {
|
|
5
|
+
const target = plugins.find(item => typeof q === 'string' ? item[key] === q : item[key] === q[key]); // current plugin design can't guarantee the plugins in pre/post existed
|
|
6
|
+
|
|
7
|
+
if (!target) {
|
|
8
|
+
throw new Error(`plugin ${q} not existed`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return target;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
plugins.forEach(item => {
|
|
15
|
+
item[preKey].forEach(p => {
|
|
16
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
17
|
+
if (plugins.find(ap => ap.name === p)) {
|
|
18
|
+
allLines.push([getPluginByAny(p)[key], getPluginByAny(item)[key]]);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
item[postKey].forEach(pt => {
|
|
22
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
23
|
+
if (plugins.find(ap => ap.name === pt)) {
|
|
24
|
+
allLines.push([getPluginByAny(item)[key], getPluginByAny(pt)[key]]);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}); // search the zero input plugin
|
|
28
|
+
|
|
29
|
+
let zeroEndPoints = plugins.filter(item => !allLines.find(l => l[1] === item[key]));
|
|
30
|
+
const sortedPoint = [];
|
|
31
|
+
|
|
32
|
+
while (zeroEndPoints.length) {
|
|
33
|
+
const zep = zeroEndPoints.shift();
|
|
34
|
+
sortedPoint.push(getPluginByAny(zep));
|
|
35
|
+
allLines = allLines.filter(l => l[0] !== getPluginByAny(zep)[key]);
|
|
36
|
+
const restPoints = plugins.filter(item => !sortedPoint.find(sp => sp[key] === item[key]));
|
|
37
|
+
zeroEndPoints = restPoints.filter( // eslint-disable-next-line @typescript-eslint/no-loop-func
|
|
38
|
+
item => !allLines.find(l => l[1] === item[key]));
|
|
39
|
+
} // if has ring, throw error
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if (allLines.length) {
|
|
43
|
+
const restInRingPoints = {};
|
|
44
|
+
allLines.forEach(l => {
|
|
45
|
+
restInRingPoints[l[0]] = true;
|
|
46
|
+
restInRingPoints[l[1]] = true;
|
|
47
|
+
});
|
|
48
|
+
throw new Error(`plugins dependences has loop: ${Object.keys(restInRingPoints).join(',')}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return sortedPoint;
|
|
52
|
+
};
|
|
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.isObject = exports.includePlugin = exports.hasOwnProperty = exports.checkPlugins = void 0;
|
|
7
7
|
exports.sortPlugins = sortPlugins;
|
|
8
8
|
|
|
9
|
+
var _pluginDagSort = require("../utils/pluginDagSort");
|
|
10
|
+
|
|
9
11
|
const checkPlugins = plugins => {
|
|
10
12
|
plugins.forEach(origin => {
|
|
11
13
|
origin.rivals.forEach(rival => {
|
|
@@ -26,29 +28,8 @@ const checkPlugins = plugins => {
|
|
|
26
28
|
exports.checkPlugins = checkPlugins;
|
|
27
29
|
|
|
28
30
|
function sortPlugins(input) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
for (let i = 0; i < plugins.length; i++) {
|
|
32
|
-
const plugin = plugins[i];
|
|
33
|
-
|
|
34
|
-
for (const pre of plugin.pre) {
|
|
35
|
-
for (let j = i + 1; j < plugins.length; j++) {
|
|
36
|
-
if (plugins[j].name === pre) {
|
|
37
|
-
plugins = [...plugins.slice(0, i), plugins[j], ...plugins.slice(i, j), ...plugins.slice(j + 1, plugins.length)];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (const post of plugin.post) {
|
|
43
|
-
for (let j = 0; j < i; j++) {
|
|
44
|
-
if (plugins[j].name === post) {
|
|
45
|
-
plugins = [...plugins.slice(0, j), ...plugins.slice(j + 1, i + 1), plugins[j], ...plugins.slice(i + 1, plugins.length)];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return plugins;
|
|
31
|
+
const plugins = input.slice();
|
|
32
|
+
return (0, _pluginDagSort.dagSort)(plugins);
|
|
52
33
|
}
|
|
53
34
|
|
|
54
35
|
const includePlugin = (plugins, input) => plugins.some(plugin => plugin.name === input.name);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.dagSort = void 0;
|
|
7
|
+
|
|
8
|
+
const dagSort = (plugins, key = 'name', preKey = 'pre', postKey = 'post') => {
|
|
9
|
+
let allLines = [];
|
|
10
|
+
|
|
11
|
+
function getPluginByAny(q) {
|
|
12
|
+
const target = plugins.find(item => typeof q === 'string' ? item[key] === q : item[key] === q[key]); // current plugin design can't guarantee the plugins in pre/post existed
|
|
13
|
+
|
|
14
|
+
if (!target) {
|
|
15
|
+
throw new Error(`plugin ${q} not existed`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return target;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
plugins.forEach(item => {
|
|
22
|
+
item[preKey].forEach(p => {
|
|
23
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
24
|
+
if (plugins.find(ap => ap.name === p)) {
|
|
25
|
+
allLines.push([getPluginByAny(p)[key], getPluginByAny(item)[key]]);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
item[postKey].forEach(pt => {
|
|
29
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
30
|
+
if (plugins.find(ap => ap.name === pt)) {
|
|
31
|
+
allLines.push([getPluginByAny(item)[key], getPluginByAny(pt)[key]]);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}); // search the zero input plugin
|
|
35
|
+
|
|
36
|
+
let zeroEndPoints = plugins.filter(item => !allLines.find(l => l[1] === item[key]));
|
|
37
|
+
const sortedPoint = [];
|
|
38
|
+
|
|
39
|
+
while (zeroEndPoints.length) {
|
|
40
|
+
const zep = zeroEndPoints.shift();
|
|
41
|
+
sortedPoint.push(getPluginByAny(zep));
|
|
42
|
+
allLines = allLines.filter(l => l[0] !== getPluginByAny(zep)[key]);
|
|
43
|
+
const restPoints = plugins.filter(item => !sortedPoint.find(sp => sp[key] === item[key]));
|
|
44
|
+
zeroEndPoints = restPoints.filter( // eslint-disable-next-line @typescript-eslint/no-loop-func
|
|
45
|
+
item => !allLines.find(l => l[1] === item[key]));
|
|
46
|
+
} // if has ring, throw error
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if (allLines.length) {
|
|
50
|
+
const restInRingPoints = {};
|
|
51
|
+
allLines.forEach(l => {
|
|
52
|
+
restInRingPoints[l[0]] = true;
|
|
53
|
+
restInRingPoints[l[1]] = true;
|
|
54
|
+
});
|
|
55
|
+
throw new Error(`plugins dependences has loop: ${Object.keys(restInRingPoints).join(',')}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return sortedPoint;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
exports.dagSort = dagSort;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
|
-
import
|
|
3
|
-
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
|
|
2
|
+
import { dagSort } from "../utils/pluginDagSort";
|
|
4
3
|
export var checkPlugins = function checkPlugins(plugins) {
|
|
5
4
|
plugins.forEach(function (origin) {
|
|
6
5
|
origin.rivals.forEach(function (rival) {
|
|
@@ -21,50 +20,7 @@ export var checkPlugins = function checkPlugins(plugins) {
|
|
|
21
20
|
};
|
|
22
21
|
export function sortPlugins(input) {
|
|
23
22
|
var plugins = input.slice();
|
|
24
|
-
|
|
25
|
-
for (var i = 0; i < plugins.length; i++) {
|
|
26
|
-
var plugin = plugins[i];
|
|
27
|
-
|
|
28
|
-
var _iterator = _createForOfIteratorHelper(plugin.pre),
|
|
29
|
-
_step;
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
33
|
-
var pre = _step.value;
|
|
34
|
-
|
|
35
|
-
for (var j = i + 1; j < plugins.length; j++) {
|
|
36
|
-
if (plugins[j].name === pre) {
|
|
37
|
-
plugins = [].concat(_toConsumableArray(plugins.slice(0, i)), [plugins[j]], _toConsumableArray(plugins.slice(i, j)), _toConsumableArray(plugins.slice(j + 1, plugins.length)));
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
} catch (err) {
|
|
42
|
-
_iterator.e(err);
|
|
43
|
-
} finally {
|
|
44
|
-
_iterator.f();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
var _iterator2 = _createForOfIteratorHelper(plugin.post),
|
|
48
|
-
_step2;
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
52
|
-
var post = _step2.value;
|
|
53
|
-
|
|
54
|
-
for (var _j = 0; _j < i; _j++) {
|
|
55
|
-
if (plugins[_j].name === post) {
|
|
56
|
-
plugins = [].concat(_toConsumableArray(plugins.slice(0, _j)), _toConsumableArray(plugins.slice(_j + 1, i + 1)), [plugins[_j]], _toConsumableArray(plugins.slice(i + 1, plugins.length)));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
} catch (err) {
|
|
61
|
-
_iterator2.e(err);
|
|
62
|
-
} finally {
|
|
63
|
-
_iterator2.f();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return plugins;
|
|
23
|
+
return dagSort(plugins);
|
|
68
24
|
}
|
|
69
25
|
export var includePlugin = function includePlugin(plugins, input) {
|
|
70
26
|
return plugins.some(function (plugin) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export var dagSort = function dagSort(plugins) {
|
|
2
|
+
var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'name';
|
|
3
|
+
var preKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'pre';
|
|
4
|
+
var postKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'post';
|
|
5
|
+
var allLines = [];
|
|
6
|
+
|
|
7
|
+
function getPluginByAny(q) {
|
|
8
|
+
var target = plugins.find(function (item) {
|
|
9
|
+
return typeof q === 'string' ? item[key] === q : item[key] === q[key];
|
|
10
|
+
}); // current plugin design can't guarantee the plugins in pre/post existed
|
|
11
|
+
|
|
12
|
+
if (!target) {
|
|
13
|
+
throw new Error("plugin ".concat(q, " not existed"));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return target;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
plugins.forEach(function (item) {
|
|
20
|
+
item[preKey].forEach(function (p) {
|
|
21
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
22
|
+
if (plugins.find(function (ap) {
|
|
23
|
+
return ap.name === p;
|
|
24
|
+
})) {
|
|
25
|
+
allLines.push([getPluginByAny(p)[key], getPluginByAny(item)[key]]);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
item[postKey].forEach(function (pt) {
|
|
29
|
+
// compatibility: do not add the plugin-name that plugins not have
|
|
30
|
+
if (plugins.find(function (ap) {
|
|
31
|
+
return ap.name === pt;
|
|
32
|
+
})) {
|
|
33
|
+
allLines.push([getPluginByAny(item)[key], getPluginByAny(pt)[key]]);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}); // search the zero input plugin
|
|
37
|
+
|
|
38
|
+
var zeroEndPoints = plugins.filter(function (item) {
|
|
39
|
+
return !allLines.find(function (l) {
|
|
40
|
+
return l[1] === item[key];
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
var sortedPoint = [];
|
|
44
|
+
|
|
45
|
+
var _loop = function _loop() {
|
|
46
|
+
var zep = zeroEndPoints.shift();
|
|
47
|
+
sortedPoint.push(getPluginByAny(zep));
|
|
48
|
+
allLines = allLines.filter(function (l) {
|
|
49
|
+
return l[0] !== getPluginByAny(zep)[key];
|
|
50
|
+
});
|
|
51
|
+
var restPoints = plugins.filter(function (item) {
|
|
52
|
+
return !sortedPoint.find(function (sp) {
|
|
53
|
+
return sp[key] === item[key];
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
zeroEndPoints = restPoints.filter( // eslint-disable-next-line @typescript-eslint/no-loop-func
|
|
57
|
+
function (item) {
|
|
58
|
+
return !allLines.find(function (l) {
|
|
59
|
+
return l[1] === item[key];
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
while (zeroEndPoints.length) {
|
|
65
|
+
_loop();
|
|
66
|
+
} // if has ring, throw error
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if (allLines.length) {
|
|
70
|
+
var restInRingPoints = {};
|
|
71
|
+
allLines.forEach(function (l) {
|
|
72
|
+
restInRingPoints[l[0]] = true;
|
|
73
|
+
restInRingPoints[l[1]] = true;
|
|
74
|
+
});
|
|
75
|
+
throw new Error("plugins dependences has loop: ".concat(Object.keys(restInRingPoints).join(',')));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return sortedPoint;
|
|
79
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const dagSort: <P extends Record<string, any>>(plugins: P[], key?: string, preKey?: string, postKey?: string) => P[];
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "0.0.0-next-
|
|
14
|
+
"version": "0.0.0-next-20221122140537",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"@types/node": "^14",
|
|
37
37
|
"typescript": "^4",
|
|
38
38
|
"jest": "^27",
|
|
39
|
-
"@scripts/build": "0.0.0-next-
|
|
40
|
-
"@scripts/jest-config": "0.0.0-next-
|
|
39
|
+
"@scripts/build": "0.0.0-next-20221122140537",
|
|
40
|
+
"@scripts/jest-config": "0.0.0-next-20221122140537"
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"publishConfig": {
|