Skip to content

Commit

Permalink
assertNoDuplicates throw with more context (#10419)
Browse files Browse the repository at this point in the history
When users see errors like

```
Duplicate plugin/preset detected.
If you'd like to use two separate instances of a plugin,
they need separate names, e.g.

  plugins: [
    ['some-plugin', {}],
    ['some-plugin', {}, 'some unique name'],
  ]
```

It can be difficult to determine the source of the conflict, especially
in a larger build system.

This commit outputs what is known about the plugins that actually
conflict, which can be helpful for users to determine the root cause of
the conflict.

Partially addresses #9778
  • Loading branch information
hjdivad authored and nicolo-ribaudo committed Oct 2, 2019
1 parent a219b6d commit fa5a40c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/babel-core/src/config/config-descriptors.js
Expand Up @@ -345,6 +345,7 @@ function assertNoDuplicates(items: Array<UnloadedDescriptor>): void {
}

if (nameMap.has(item.name)) {
const conflicts = items.filter(i => i.value === item.value);
throw new Error(
[
`Duplicate plugin/preset detected.`,
Expand All @@ -355,6 +356,9 @@ function assertNoDuplicates(items: Array<UnloadedDescriptor>): void {
` ['some-plugin', {}],`,
` ['some-plugin', {}, 'some unique name'],`,
` ]`,
``,
`Duplicates detected are:`,
`${JSON.stringify(conflicts, null, 2)}`,
].join("\n"),
);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/babel-core/test/option-manager.js
Expand Up @@ -27,14 +27,19 @@ describe("option-manager", () => {
return { plugin, calls };
}

it("should throw if a plugin is repeated", () => {
const { calls, plugin } = makePlugin();
it("should throw if a plugin is repeated, with information about the repeated plugin", () => {
const { calls, plugin } = makePlugin("my-plugin");

expect(() => {
loadOptions({
plugins: [plugin, plugin],
plugins: [
[plugin, undefined, "my-plugin"],
[plugin, undefined, "my-plugin"],
],
});
}).toThrow(/Duplicate plugin\/preset detected/);
}).toThrow(
/Duplicate plugin\/preset detected.*Duplicates detected are.*my-plugin.*my-plugin/ms,
);
expect(calls).toEqual([]);
});

Expand Down

0 comments on commit fa5a40c

Please sign in to comment.