* Rollup deduplicate type names with a trailing `$1` or `$2`, which can be * confusing when showed in autocompletions. Try to replace with a better name
( this: PluginContext, chunk: OutputChunk, importBindings: ImportBindings[], )
| 265 | * confusing when showed in autocompletions. Try to replace with a better name |
| 266 | */ |
| 267 | function replaceConfusingTypeNames( |
| 268 | this: PluginContext, |
| 269 | chunk: OutputChunk, |
| 270 | importBindings: ImportBindings[], |
| 271 | ) { |
| 272 | const isInternalEntry = chunk.fileName.startsWith('internal.') |
| 273 | if (!isInternalEntry) { |
| 274 | for (const modName in identifierReplacements) { |
| 275 | const imp = importBindings.filter((imp) => imp.id === modName) |
| 276 | // Validate that `identifierReplacements` is not outdated if there's no match |
| 277 | if (imp.length === 0) { |
| 278 | this.warn( |
| 279 | `${chunk.fileName} does not import "${modName}" for replacement`, |
| 280 | ) |
| 281 | process.exitCode = 1 |
| 282 | continue |
| 283 | } |
| 284 | |
| 285 | const replacements = identifierReplacements[modName] |
| 286 | for (const id in replacements) { |
| 287 | // Validate that `identifierReplacements` is not outdated if there's no match |
| 288 | if (!imp.some((i) => i.locals.includes(id))) { |
| 289 | this.warn( |
| 290 | `${chunk.fileName} does not import "${id}" from "${modName}" for replacement`, |
| 291 | ) |
| 292 | process.exitCode = 1 |
| 293 | continue |
| 294 | } |
| 295 | |
| 296 | const betterId = replacements[id] |
| 297 | const regexEscapedId = escapeRegex(id) |
| 298 | // If the better id accesses a namespace, the existing `Foo as Foo$1` |
| 299 | // named import cannot be replaced with `Foo as Namespace.Foo`, so we |
| 300 | // pre-emptively remove the whole named import |
| 301 | if (betterId.includes('.')) { |
| 302 | chunk.code = chunk.code.replace( |
| 303 | new RegExp(`\\b\\w+\\b as ${regexEscapedId},?\\s?`), |
| 304 | '', |
| 305 | ) |
| 306 | } |
| 307 | chunk.code = chunk.code.replace( |
| 308 | new RegExp(`\\b${regexEscapedId}\\b`, 'g'), |
| 309 | betterId, |
| 310 | ) |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | const identifiers = unique( |
| 316 | Array.from( |
| 317 | chunk.code.matchAll(identifierWithTrailingDollarRE), |
| 318 | (m) => m[0], |
| 319 | ), |
| 320 | ) |
| 321 | const unreplacedIds = identifiers.filter( |
| 322 | (id) => !ignoreConfusingTypeNames.includes(id), |
| 323 | ) |
| 324 | if (unreplacedIds.length) { |
nothing calls this directly
no test coverage detected