Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 2ca9cc9

Browse files
committed
refactor: Extract etwp helper functions
1 parent 3fbdf66 commit 2ca9cc9

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

.eslintrc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"extends": "webpack"
3-
}
2+
"extends": "webpack",
3+
"rules": {
4+
"prefer-destructuring": 1,
5+
"prefer-rest-params": 1,
6+
"no-plusplus": 1,
7+
"consistent-return": 1,
8+
"no-param-reassign": 1
9+
}
10+
}

src/lib/helpers.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export function isInitialOrHasNoParents(chunk) {
2+
return chunk.isInitial() || chunk.parents.length === 0;
3+
}
4+
5+
export function isInvalidOrder(a, b) {
6+
const bBeforeA = a.getPrevModules().includes(b);
7+
const aBeforeB = b.getPrevModules().includes(a);
8+
return aBeforeB && bBeforeA;
9+
}
10+
11+
export function getOrder(a, b) {
12+
const aOrder = a.getOrder();
13+
const bOrder = b.getOrder();
14+
if (aOrder < bOrder) return -1;
15+
if (aOrder > bOrder) return 1;
16+
const aIndex = a.getOriginalModule().index2;
17+
const bIndex = b.getOriginalModule().index2;
18+
if (aIndex < bIndex) return -1;
19+
if (aIndex > bIndex) return 1;
20+
const bBeforeA = a.getPrevModules().includes(b);
21+
const aBeforeB = b.getPrevModules().includes(a);
22+
if (aBeforeB && !bBeforeA) return -1;
23+
if (!aBeforeB && bBeforeA) return 1;
24+
const ai = a.identifier();
25+
const bi = b.identifier();
26+
if (ai < bi) return -1;
27+
if (ai > bi) return 1;
28+
return 0;
29+
}
30+
31+
export function getLoaderObject(loader) {
32+
if (isString(loader)) {
33+
return { loader };
34+
}
35+
return loader;
36+
}
37+
38+
export function mergeOptions(a, b) {
39+
if (!b) return a;
40+
Object.keys(b).forEach((key) => {
41+
a[key] = b[key];
42+
});
43+
return a;
44+
}
45+
46+
export function isString(a) {
47+
return typeof a === 'string';
48+
}
49+
50+
export function isFunction(a) {
51+
return isType('Function', a);
52+
}
53+
54+
export function isType(type, obj) {
55+
return Object.prototype.toString.call(obj) === `[object ${type}]`;
56+
}

0 commit comments

Comments
 (0)