Skip to content

Commit 0263bf0

Browse files
committed
feat(ruleset-bundler): plugins should be easy to override
1 parent a48381b commit 0263bf0

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

packages/ruleset-bundler/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { rollup, Plugin } from 'rollup';
22
import { isURL } from '@stoplight/path';
33
import { isPackageImport } from './utils/isPackageImport';
4+
import { dedupeRollupPlugins } from './utils/dedupeRollupPlugins';
45

56
export type BundleOptions = {
67
plugins: Plugin[];
@@ -17,7 +18,7 @@ export async function bundleRuleset(
1718
): Promise<string> {
1819
const bundle = await rollup({
1920
input,
20-
plugins,
21+
plugins: dedupeRollupPlugins(plugins),
2122
treeshake,
2223
watch: false,
2324
perf: false,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { Plugin } from 'rollup';
2+
3+
import { dedupeRollupPlugins } from '../dedupeRollupPlugins';
4+
5+
describe('dedupeRollupPlugins util', () => {
6+
it('should keep plugins with different names', () => {
7+
const plugins: Plugin[] = [
8+
{
9+
name: 'plugin 1',
10+
},
11+
{
12+
name: 'plugin 2',
13+
},
14+
{
15+
name: 'plugin 3',
16+
},
17+
];
18+
19+
expect(dedupeRollupPlugins([...plugins])).toStrictEqual(plugins);
20+
});
21+
22+
it('given the same plugin, should replace the first declaration', () => {
23+
const plugins: Plugin[] = [
24+
{
25+
name: 'plugin 1',
26+
cacheKey: 'key 1',
27+
},
28+
{
29+
name: 'plugin 2',
30+
},
31+
{
32+
name: 'plugin 1',
33+
cacheKey: 'key 2',
34+
},
35+
{
36+
name: 'plugin 1',
37+
cacheKey: 'key 3',
38+
},
39+
];
40+
41+
expect(dedupeRollupPlugins([...plugins])).toStrictEqual([
42+
{
43+
name: 'plugin 1',
44+
cacheKey: 'key 3',
45+
},
46+
{
47+
name: 'plugin 2',
48+
},
49+
]);
50+
});
51+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// this function makes sure we can only have one plugin with the same name
2+
// the last plugin definition has a precedence
3+
import type { Plugin } from 'rollup';
4+
5+
export function dedupeRollupPlugins(plugins: Plugin[]): Plugin[] {
6+
const map = new Map<string, Plugin>();
7+
for (const plugin of plugins) {
8+
map.set(plugin.name, plugin);
9+
}
10+
11+
return Array.from(map.values());
12+
}

0 commit comments

Comments
 (0)