Skip to content

Commit

Permalink
feat(ruleset-bundler): plugins should be easy to override
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Feb 25, 2022
1 parent a48381b commit 0263bf0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ruleset-bundler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { rollup, Plugin } from 'rollup';
import { isURL } from '@stoplight/path';
import { isPackageImport } from './utils/isPackageImport';
import { dedupeRollupPlugins } from './utils/dedupeRollupPlugins';

export type BundleOptions = {
plugins: Plugin[];
Expand All @@ -17,7 +18,7 @@ export async function bundleRuleset(
): Promise<string> {
const bundle = await rollup({
input,
plugins,
plugins: dedupeRollupPlugins(plugins),
treeshake,
watch: false,
perf: false,
Expand Down
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',
},
]);
});
});
12 changes: 12 additions & 0 deletions packages/ruleset-bundler/src/utils/dedupeRollupPlugins.ts
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());
}

0 comments on commit 0263bf0

Please sign in to comment.