MCPcopy Index your code
hub / github.com/TanStack/devtools / transformAndInject

Function transformAndInject

packages/devtools-vite/src/inject-plugin.ts:134–253  ·  view source on GitHub ↗
(
  code: string,
  injection: PluginInjection,
  devtoolsComponentName: string,
)

Source from the content-addressed store, hash-verified

132}
133
134export const transformAndInject = (
135 code: string,
136 injection: PluginInjection,
137 devtoolsComponentName: string,
138): { code: string; transformed: boolean } | null => {
139 const importName = injection.pluginImport?.importName
140 const pluginType = injection.pluginImport?.type || 'jsx'
141 const displayName = injection.pluginName
142
143 if (!importName) {
144 return null
145 }
146
147 try {
148 const result = parseSync('input.tsx', code, {
149 sourceType: 'module',
150 lang: 'tsx',
151 })
152 if (result.errors.length > 0) return null
153
154 const s = new MagicString(code)
155 const isNamespaceImport = devtoolsComponentName.includes('.')
156
157 walk(result.program, (node) => {
158 if (node.type !== 'JSXOpeningElement') return
159
160 let matches = false
161 if (isNamespaceImport) {
162 if (node.name.type === 'JSXMemberExpression') {
163 const fullName = `${node.name.object.type === 'JSXIdentifier' ? node.name.object.name : ''}.${node.name.property.name}`
164 matches = fullName === devtoolsComponentName
165 }
166 } else {
167 matches =
168 node.name.type === 'JSXIdentifier' &&
169 node.name.name === devtoolsComponentName
170 }
171
172 if (!matches) return
173
174 // Find the plugins prop
175 const pluginsProp = node.attributes.find(
176 (attr) =>
177 attr.type === 'JSXAttribute' &&
178 attr.name.type === 'JSXIdentifier' &&
179 attr.name.name === 'plugins',
180 )
181
182 if (pluginsProp && pluginsProp.type === 'JSXAttribute') {
183 if (
184 pluginsProp.value &&
185 pluginsProp.value.type === 'JSXExpressionContainer'
186 ) {
187 const expression = pluginsProp.value.expression
188 if (expression.type === 'ArrayExpression') {
189 if (
190 !pluginExists(
191 code,

Callers 2

testTransformFunction · 0.90
injectPluginIntoFileFunction · 0.85

Calls 3

walkFunction · 0.90
pluginExistsFunction · 0.85
buildPluginStringFunction · 0.85

Tested by 1

testTransformFunction · 0.72