| 1 | import defineProvider, { |
| 2 | type Utils, |
| 3 | type MetaDescriptor, |
| 4 | } from "@babel/helper-define-polyfill-provider"; |
| 5 | import type { NodePath } from "@babel/traverse"; |
| 6 | |
| 7 | import polyfills from "../data/polyfills.js"; |
| 8 | |
| 9 | import { |
| 10 | type Descriptor, |
| 11 | Globals, |
| 12 | StaticProperties, |
| 13 | InstanceProperties, |
| 14 | } from "./mappings"; |
| 15 | |
| 16 | export default defineProvider<{}>(function ({ |
| 17 | shouldInjectPolyfill, |
| 18 | assertDependency, |
| 19 | createMetaResolver, |
| 20 | debug, |
| 21 | babel: { template, types: t }, |
| 22 | }) { |
| 23 | const expr = template.expression.ast; |
| 24 | |
| 25 | const resolvePolyfill = createMetaResolver<Descriptor[]>({ |
| 26 | global: Globals, |
| 27 | static: StaticProperties, |
| 28 | instance: InstanceProperties, |
| 29 | }); |
| 30 | |
| 31 | function createDescIterator( |
| 32 | cb: (desc: Descriptor, utils: Utils, path: NodePath) => void, |
| 33 | instance?: ( |
| 34 | meta: MetaDescriptor, |
| 35 | resolved: any, |
| 36 | utils: Utils, |
| 37 | path: any, |
| 38 | ) => void, |
| 39 | ) { |
| 40 | return (meta: MetaDescriptor, utils: Utils, path: NodePath) => { |
| 41 | if (path.parentPath.isUnaryExpression({ operator: "delete" })) return; |
| 42 | |
| 43 | const resolved = resolvePolyfill(meta); |
| 44 | if (!resolved) return; |
| 45 | |
| 46 | if (instance && resolved.kind === "instance") { |
| 47 | instance(meta, resolved, utils, path); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | for (const desc of resolved.desc) { |
| 52 | if (!desc.exclude?.(meta, path) && shouldInjectPolyfill(desc.name)) { |
| 53 | cb(desc, utils, path); |
| 54 | |
| 55 | // Since global and static polyfills are unambiguous, we only need to |
| 56 | // inject the first non-excluded one. |
| 57 | if (resolved.kind !== "instance") { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | function injectDefault(desc, utils) { |
| 66 | assertDependency(desc.package, desc.version); |
| 67 | debug(desc.name); |
| 68 | |
| 69 | return utils.injectDefaultImport(desc.path, desc.nameHint || desc.name); |
| 70 | } |
| 71 | |
| 72 | const seen = new WeakSet(); |
| 73 | |
| 74 | return { |
| 75 | name: "es-shims", |
| 76 | polyfills, |
| 77 | |
| 78 | usageGlobal: createDescIterator((desc, utils) => { |
| 79 | if (desc.global === false) return; |
| 80 | |
| 81 | assertDependency(desc.package, desc.version); |
| 82 | |
| 83 | utils.injectGlobalImport(`${desc.path}/auto`); |
| 84 | |
| 85 | debug(desc.name); |
| 86 | }), |
| 87 | |
| 88 | usagePure: createDescIterator( |
| 89 | (desc, utils, path) => { |
| 90 | path.replaceWith(injectDefault(desc, utils)); |
| 91 | }, |
| 92 | (meta, resolved, utils, path) => { |
| 93 | if (meta.kind !== "property") return; |
| 94 | if (path.parentPath.isAssignmentExpression({ left: path.node })) return; |
| 95 | |
| 96 | if (seen.has(path.node)) return; |
| 97 | seen.add(path.node); |
| 98 | |
| 99 | const isCall = path.parentPath.isCallExpression({ callee: path.node }); |
| 100 | const isGetter = resolved.desc[0].getter; |
| 101 | |
| 102 | const matchesPolyfill = ({ name }) => |
| 103 | name.startsWith(meta.object as any as string); |
| 104 | |
| 105 | let index = -1; |
| 106 | if ( |
| 107 | // This is the actual method for sure. |
| 108 | meta.kind === "property" && |
| 109 | meta.placement === "prototype" && |
| 110 | meta.object != null && |
| 111 | (index = resolved.desc.findIndex(matchesPolyfill)) !== -1 |
| 112 | ) { |
| 113 | const desc = resolved.desc[index]; |
| 114 | if (!shouldInjectPolyfill(desc.name)) return; |
| 115 | |
| 116 | const id = injectDefault(desc, utils); |
| 117 | |
| 118 | if (isGetter) { |
| 119 | path.replaceWith(expr`${id}(${path.node.object})`); |
| 120 | } else if (isCall) { |
| 121 | path.parentPath.unshiftContainer("arguments", path.node.object); |
| 122 | path.replaceWith(id); |
| 123 | } else if ( |
| 124 | path.parentPath.isMemberExpression({ |
| 125 | object: path.node, |
| 126 | computed: false, |
| 127 | }) && |
| 128 | path.parent.property.name === "call" && |
| 129 | path.parentPath.parentPath.isCallExpression({ callee: path.parent }) |
| 130 | ) { |
| 131 | path.parentPath.replaceWith(id); |
| 132 | } else { |
| 133 | path.replaceWith(expr`${id}.getPolyfill()`); |
| 134 | } |
| 135 | } else { |
| 136 | let tmp; |
| 137 | let parent; |
| 138 | let replacement = path.node; |
| 139 | const { object } = path.node; |
| 140 | |
| 141 | for (const desc of resolved.desc) { |
| 142 | const { thisCheck } = desc; |
| 143 | if ( |
| 144 | !thisCheck || |
| 145 | desc.exclude?.(meta) || |
| 146 | !shouldInjectPolyfill(desc.name) |
| 147 | ) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | if (!tmp) { |
| 152 | tmp = path.scope.generateUidIdentifierBasedOnNode(object); |
| 153 | path.scope.push({ id: t.cloneNode(tmp) }); |
| 154 | path.get("object").replaceWith(tmp); |
| 155 | } |
| 156 | |
| 157 | const id = injectDefault(desc, utils); |
| 158 | |
| 159 | replacement = t.conditionalExpression( |
| 160 | thisCheck(t.cloneNode(tmp)), |
| 161 | isGetter |
| 162 | ? expr`${id}(${t.cloneNode(tmp)})` |
| 163 | : isCall |
| 164 | ? id |
| 165 | : expr`${id}.getPolyfill()`, |
| 166 | replacement, |
| 167 | ); |
| 168 | |
| 169 | parent = parent ?? replacement; |
| 170 | } |
| 171 | |
| 172 | if (!parent) return; |
| 173 | |
| 174 | replacement = expr`(${t.cloneNode(tmp)} = ${object}, ${replacement})`; |
| 175 | |
| 176 | if (!isGetter && isCall) { |
| 177 | parent.alternate = expr`Function.call.bind(${parent.alternate})`; |
| 178 | path.parentPath.unshiftContainer("arguments", t.cloneNode(tmp)); |
| 179 | } |
| 180 | |
| 181 | path.replaceWith(replacement); |
| 182 | } |
| 183 | }, |
| 184 | ), |
| 185 | }; |
| 186 | }); |