From 0676908d93787db5f7f816955065b5da43d31503 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 8 Oct 2026 21:02:53 +0800 Subject: [PATCH] fix(alias): insert string alias replacements literally --- packages/alias/src/index.ts | 8 +++++++- packages/alias/test/alias.test.ts | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/alias/src/index.ts b/packages/alias/src/index.ts index 2276194c2..be569f033 100755 --- a/packages/alias/src/index.ts +++ b/packages/alias/src/index.ts @@ -127,7 +127,13 @@ export default function alias(options: RollupAliasOptions = {}): Plugin { return null; } - const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement); + // When "find" is a string, the replacement must be inserted literally. + // Passing it as the second argument of String.replace would interpret + // "$$", "$&" etc. as special replacement patterns. + const updatedId = + typeof matchedEntry.find === 'string' + ? importee.replace(matchedEntry.find, () => matchedEntry.replacement) + : importee.replace(matchedEntry.find, matchedEntry.replacement); if (matchedEntry.resolverFunction) { return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions); diff --git a/packages/alias/test/alias.test.ts b/packages/alias/test/alias.test.ts index 46619bad0..bc2e540e9 100644 --- a/packages/alias/test/alias.test.ts +++ b/packages/alias/test/alias.test.ts @@ -167,6 +167,23 @@ describe('plugin-alias', () => { ]); }); + it('Replaces string aliases literally', async () => { + const result = await resolveAliasWithRollup( + { + entries: [ + { find: 'id', replacement: '$$' }, + { find: 'at', replacement: '$&co' } + ] + }, + ['$$', '$&co'], + [ + { source: 'id', importer: '/src/importer.js' }, + { source: 'at', importer: '/src/importer.js' } + ] + ); + expect(result).toEqual(['$$', '$&co']); + }); + it('Will not confuse modules with similar names', async () => { const result = await resolveAliasWithRollup( {