Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/alias/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions packages/alias/test/alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
Loading