(
code: string,
id: string,
ignore: {
files?: Array<string | RegExp>
components?: Array<string | RegExp>
} = {},
)
| 185 | } |
| 186 | |
| 187 | export function addSourceToJsx( |
| 188 | code: string, |
| 189 | id: string, |
| 190 | ignore: { |
| 191 | files?: Array<string | RegExp> |
| 192 | components?: Array<string | RegExp> |
| 193 | } = {}, |
| 194 | ) { |
| 195 | const filePath = id.split('?')[0]! |
| 196 | const location = filePath.replace(normalizePath(process.cwd()), '') |
| 197 | |
| 198 | const fileIgnored = matcher(ignore.files || [], location) |
| 199 | if (fileIgnored) return |
| 200 | |
| 201 | try { |
| 202 | const result = parseSync(filePath, code, { |
| 203 | sourceType: 'module', |
| 204 | lang: 'tsx', |
| 205 | }) |
| 206 | if (result.errors.length > 0) return |
| 207 | |
| 208 | const offsetToLoc = createLocMapper(code) |
| 209 | const s = new MagicString(code) |
| 210 | const annotated = new Set<number>() |
| 211 | |
| 212 | const didTransform = visitFunctions( |
| 213 | result.program, |
| 214 | annotated, |
| 215 | location, |
| 216 | ignore.components || [], |
| 217 | offsetToLoc, |
| 218 | s, |
| 219 | code, |
| 220 | ) |
| 221 | |
| 222 | if (!didTransform) return |
| 223 | |
| 224 | return { |
| 225 | code: s.toString(), |
| 226 | map: s.generateMap({ |
| 227 | source: filePath, |
| 228 | file: id, |
| 229 | includeContent: true, |
| 230 | }), |
| 231 | } |
| 232 | } catch (e) { |
| 233 | return |
| 234 | } |
| 235 | } |
no test coverage detected