MCPcopy Create free account
hub / github.com/FormidableLabs/babel-plugin-transform-define / plugin

Function plugin

lib/index.js:82–129  ·  view source on GitHub ↗
({ types: t })

Source from the content-addressed store, hash-verified

80const TYPEOF_PREFIX = "typeof ";
81
82const 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.
132module.exports = plugin;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…