utils.js
3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getType = getType;
exports.intersection = intersection;
exports.filterStageFromList = filterStageFromList;
exports.getImportSource = getImportSource;
exports.getRequireSource = getRequireSource;
exports.isPolyfillSource = isPolyfillSource;
exports.getModulePath = getModulePath;
exports.createImport = createImport;
exports.isNamespaced = isNamespaced;
exports.has = void 0;
var t = _interopRequireWildcard(require("@babel/types"));
var _helperModuleImports = require("@babel/helper-module-imports");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const has = Object.hasOwnProperty.call.bind(Object.hasOwnProperty);
exports.has = has;
function getType(target) {
return Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
}
function intersection(first, second, third) {
const result = new Set();
for (const el of first) {
if (second.has(el) && third.has(el)) result.add(el);
}
return result;
}
function filterStageFromList(list, stageList) {
return Object.keys(list).reduce((result, item) => {
if (!stageList.has(item)) {
result[item] = list[item];
}
return result;
}, {});
}
function getImportSource({
node
}) {
if (node.specifiers.length === 0) return node.source.value;
}
function getRequireSource({
node
}) {
if (!t.isExpressionStatement(node)) return;
const {
expression
} = node;
const isRequire = t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0]);
if (isRequire) return expression.arguments[0].value;
}
function isPolyfillSource(source) {
return source === "@babel/polyfill" || source === "core-js";
}
const modulePathMap = {
"regenerator-runtime": "regenerator-runtime/runtime"
};
function getModulePath(mod) {
return modulePathMap[mod] || `core-js/modules/${mod}`;
}
function createImport(path, mod) {
return (0, _helperModuleImports.addSideEffect)(path, getModulePath(mod));
}
function isNamespaced(path) {
if (!path.node) return false;
const binding = path.scope.getBinding(path.node.name);
if (!binding) return false;
return binding.path.isImportNamespaceSpecifier();
}