({ types: t })
| 80 | const TYPEOF_PREFIX = "typeof "; |
| 81 | |
| 82 | const plugin = function ({ types: t }) { |
| 83 | return { |
| 84 | visitor: { |
| 85 | |
| 86 | // process.env.NODE_ENV; |
| 87 | MemberExpression(nodePath, state) { |
| 88 | processNode(state.opts, nodePath, t.valueToNode, memberExpressionComparator); |
| 89 | }, |
| 90 | |
| 91 | // const x = { version: VERSION }; |
| 92 | Identifier(nodePath, state) { |
| 93 | const binding = nodePath.scope.getBinding(nodePath.node.name); |
| 94 | |
| 95 | if ( |
| 96 | binding |
| 97 | // Don't transform import identifiers. This is meant to mimic webpack's |
| 98 | // DefinePlugin behavior. |
| 99 | || isImportIdentifier(nodePath) |
| 100 | // Do not transform Object keys / properties unless they are computed like {[key]: value} |
| 101 | || nodePath.key === "key" && nodePath.parent.computed === false |
| 102 | || nodePath.key === "property" && nodePath.parent.computed === false |
| 103 | ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | processNode(state.opts, nodePath, t.valueToNode, identifierComparator); |
| 108 | }, |
| 109 | |
| 110 | // typeof window |
| 111 | UnaryExpression(nodePath, state) { |
| 112 | if (nodePath.node.operator !== "typeof") { return; } |
| 113 | |
| 114 | const { opts } = state; |
| 115 | const keys = Object.keys(opts); |
| 116 | const typeofValues = {}; |
| 117 | |
| 118 | keys.forEach((key) => { |
| 119 | if (key.substring(0, TYPEOF_PREFIX.length) === TYPEOF_PREFIX) { |
| 120 | typeofValues[key.substring(TYPEOF_PREFIX.length)] = opts[key]; |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | processNode(typeofValues, nodePath, t.valueToNode, unaryExpressionComparator); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | }; |
| 129 | }; |
| 130 | |
| 131 | // Exports. |
| 132 | module.exports = plugin; |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…