1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = _default;
|
7 |
|
8 | var _corejs2BuiltIns = _interopRequireDefault(require("../../../data/corejs2-built-ins.json"));
|
9 |
|
10 | var _getPlatformSpecificDefault = _interopRequireDefault(require("./get-platform-specific-default"));
|
11 |
|
12 | var _filterItems = _interopRequireDefault(require("../../filter-items"));
|
13 |
|
14 | var _builtInDefinitions = require("./built-in-definitions");
|
15 |
|
16 | var _utils = require("../../utils");
|
17 |
|
18 | var _debug = require("../../debug");
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | const NO_DIRECT_POLYFILL_IMPORT = `
|
23 | When setting \`useBuiltIns: 'usage'\`, polyfills are automatically imported when needed.
|
24 | Please remove the \`import '@babel/polyfill'\` call or use \`useBuiltIns: 'entry'\` instead.`;
|
25 |
|
26 | function _default({
|
27 | types: t
|
28 | }, {
|
29 | include,
|
30 | exclude,
|
31 | polyfillTargets,
|
32 | debug
|
33 | }) {
|
34 | const polyfills = (0, _filterItems.default)(_corejs2BuiltIns.default, include, exclude, polyfillTargets, (0, _getPlatformSpecificDefault.default)(polyfillTargets));
|
35 | const addAndRemovePolyfillImports = {
|
36 | ImportDeclaration(path) {
|
37 | if ((0, _utils.isPolyfillSource)((0, _utils.getImportSource)(path))) {
|
38 | console.warn(NO_DIRECT_POLYFILL_IMPORT);
|
39 | path.remove();
|
40 | }
|
41 | },
|
42 |
|
43 | Program(path) {
|
44 | path.get("body").forEach(bodyPath => {
|
45 | if ((0, _utils.isPolyfillSource)((0, _utils.getRequireSource)(bodyPath))) {
|
46 | console.warn(NO_DIRECT_POLYFILL_IMPORT);
|
47 | bodyPath.remove();
|
48 | }
|
49 | });
|
50 | },
|
51 |
|
52 | ReferencedIdentifier({
|
53 | node: {
|
54 | name
|
55 | },
|
56 | parent,
|
57 | scope
|
58 | }) {
|
59 | if (t.isMemberExpression(parent)) return;
|
60 | if (!(0, _utils.has)(_builtInDefinitions.BuiltIns, name)) return;
|
61 | if (scope.getBindingIdentifier(name)) return;
|
62 | const BuiltInDependencies = _builtInDefinitions.BuiltIns[name];
|
63 | this.addUnsupported(BuiltInDependencies);
|
64 | },
|
65 |
|
66 | CallExpression(path) {
|
67 | if (path.node.arguments.length) return;
|
68 | const callee = path.node.callee;
|
69 | if (!t.isMemberExpression(callee)) return;
|
70 | if (!callee.computed) return;
|
71 |
|
72 | if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
|
73 | return;
|
74 | }
|
75 |
|
76 | this.addImport("web.dom.iterable");
|
77 | },
|
78 |
|
79 | BinaryExpression(path) {
|
80 | if (path.node.operator !== "in") return;
|
81 | if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
82 | this.addImport("web.dom.iterable");
|
83 | },
|
84 |
|
85 | YieldExpression(path) {
|
86 | if (path.node.delegate) {
|
87 | this.addImport("web.dom.iterable");
|
88 | }
|
89 | },
|
90 |
|
91 | MemberExpression: {
|
92 | enter(path) {
|
93 | const {
|
94 | node
|
95 | } = path;
|
96 | const {
|
97 | object,
|
98 | property
|
99 | } = node;
|
100 | let evaluatedPropType = object.name;
|
101 | let propertyName = property.name;
|
102 | let instanceType = "";
|
103 |
|
104 | if (node.computed) {
|
105 | if (t.isStringLiteral(property)) {
|
106 | propertyName = property.value;
|
107 | } else {
|
108 | const result = path.get("property").evaluate();
|
109 |
|
110 | if (result.confident && result.value) {
|
111 | propertyName = result.value;
|
112 | }
|
113 | }
|
114 | }
|
115 |
|
116 | if (path.scope.getBindingIdentifier(object.name)) {
|
117 | const result = path.get("object").evaluate();
|
118 |
|
119 | if (result.value) {
|
120 | instanceType = (0, _utils.getType)(result.value);
|
121 | } else if (result.deopt && result.deopt.isIdentifier()) {
|
122 | evaluatedPropType = result.deopt.node.name;
|
123 | }
|
124 | }
|
125 |
|
126 | if ((0, _utils.has)(_builtInDefinitions.StaticProperties, evaluatedPropType)) {
|
127 | const BuiltInProperties = _builtInDefinitions.StaticProperties[evaluatedPropType];
|
128 |
|
129 | if ((0, _utils.has)(BuiltInProperties, propertyName)) {
|
130 | const StaticPropertyDependencies = BuiltInProperties[propertyName];
|
131 | this.addUnsupported(StaticPropertyDependencies);
|
132 | }
|
133 | }
|
134 |
|
135 | if ((0, _utils.has)(_builtInDefinitions.InstanceProperties, propertyName)) {
|
136 | let InstancePropertyDependencies = _builtInDefinitions.InstanceProperties[propertyName];
|
137 |
|
138 | if (instanceType) {
|
139 | InstancePropertyDependencies = InstancePropertyDependencies.filter(module => module.includes(instanceType));
|
140 | }
|
141 |
|
142 | this.addUnsupported(InstancePropertyDependencies);
|
143 | }
|
144 | },
|
145 |
|
146 | exit(path) {
|
147 | const {
|
148 | name
|
149 | } = path.node.object;
|
150 | if (!(0, _utils.has)(_builtInDefinitions.BuiltIns, name)) return;
|
151 | if (path.scope.getBindingIdentifier(name)) return;
|
152 | const BuiltInDependencies = _builtInDefinitions.BuiltIns[name];
|
153 | this.addUnsupported(BuiltInDependencies);
|
154 | }
|
155 |
|
156 | },
|
157 |
|
158 | VariableDeclarator(path) {
|
159 | const {
|
160 | node
|
161 | } = path;
|
162 | const {
|
163 | id,
|
164 | init
|
165 | } = node;
|
166 | if (!t.isObjectPattern(id)) return;
|
167 | if (init && path.scope.getBindingIdentifier(init.name)) return;
|
168 |
|
169 | for (const _ref of id.properties) {
|
170 | const {
|
171 | key
|
172 | } = _ref;
|
173 |
|
174 | if (!node.computed && t.isIdentifier(key) && (0, _utils.has)(_builtInDefinitions.InstanceProperties, key.name)) {
|
175 | const InstancePropertyDependencies = _builtInDefinitions.InstanceProperties[key.name];
|
176 | this.addUnsupported(InstancePropertyDependencies);
|
177 | }
|
178 | }
|
179 | }
|
180 |
|
181 | };
|
182 | return {
|
183 | name: "corejs2-usage",
|
184 |
|
185 | pre({
|
186 | path
|
187 | }) {
|
188 | this.polyfillsSet = new Set();
|
189 |
|
190 | this.addImport = function (builtIn) {
|
191 | if (!this.polyfillsSet.has(builtIn)) {
|
192 | this.polyfillsSet.add(builtIn);
|
193 | (0, _utils.createImport)(path, builtIn);
|
194 | }
|
195 | };
|
196 |
|
197 | this.addUnsupported = function (builtIn) {
|
198 | const modules = Array.isArray(builtIn) ? builtIn : [builtIn];
|
199 |
|
200 | for (const module of modules) {
|
201 | if (polyfills.has(module)) {
|
202 | this.addImport(module);
|
203 | }
|
204 | }
|
205 | };
|
206 | },
|
207 |
|
208 | post() {
|
209 | if (debug) {
|
210 | (0, _debug.logUsagePolyfills)(this.polyfillsSet, this.file.opts.filename, polyfillTargets, _corejs2BuiltIns.default);
|
211 | }
|
212 | },
|
213 |
|
214 | visitor: addAndRemovePolyfillImports
|
215 | };
|
216 | } |
\ | No newline at end of file |