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( {