-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ruleset-bundler): plugins should be easy to override
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/ruleset-bundler/src/utils/__tests__/dedupeRollupPlugins.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Plugin } from 'rollup'; | ||
|
||
import { dedupeRollupPlugins } from '../dedupeRollupPlugins'; | ||
|
||
describe('dedupeRollupPlugins util', () => { | ||
it('should keep plugins with different names', () => { | ||
const plugins: Plugin[] = [ | ||
{ | ||
name: 'plugin 1', | ||
}, | ||
{ | ||
name: 'plugin 2', | ||
}, | ||
{ | ||
name: 'plugin 3', | ||
}, | ||
]; | ||
|
||
expect(dedupeRollupPlugins([...plugins])).toStrictEqual(plugins); | ||
}); | ||
|
||
it('given the same plugin, should replace the first declaration', () => { | ||
const plugins: Plugin[] = [ | ||
{ | ||
name: 'plugin 1', | ||
cacheKey: 'key 1', | ||
}, | ||
{ | ||
name: 'plugin 2', | ||
}, | ||
{ | ||
name: 'plugin 1', | ||
cacheKey: 'key 2', | ||
}, | ||
{ | ||
name: 'plugin 1', | ||
cacheKey: 'key 3', | ||
}, | ||
]; | ||
|
||
expect(dedupeRollupPlugins([...plugins])).toStrictEqual([ | ||
{ | ||
name: 'plugin 1', | ||
cacheKey: 'key 3', | ||
}, | ||
{ | ||
name: 'plugin 2', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// this function makes sure we can only have one plugin with the same name | ||
// the last plugin definition has a precedence | ||
import type { Plugin } from 'rollup'; | ||
|
||
export function dedupeRollupPlugins(plugins: Plugin[]): Plugin[] { | ||
const map = new Map<string, Plugin>(); | ||
for (const plugin of plugins) { | ||
map.set(plugin.name, plugin); | ||
} | ||
|
||
return Array.from(map.values()); | ||
} |