Commit efb0eb9d authored by David Sveningsson's avatar David Sveningsson

feat(plugin): load `default` transformer if loading named transformer without name

parent ce68ba21
Pipeline #116029252 passed with stages
in 10 minutes and 47 seconds
......@@ -223,6 +223,8 @@ Users may then extend the preset using `plugin:name`, e.g.:
}
```
If you expose a transformer named `default` it will be loaded when using the plugin as name, i.e. `my-plugin` and `my-plugin:default` is equivalent.
## Extend metadata
Plugins can extend the available [element metadata](/usage/elements.html) by
......
......@@ -400,6 +400,7 @@ describe("config", () => {
"mock-plugin-named",
() => ({
transformer: {
default: transform as Transformer,
foobar: transform as Transformer,
},
}),
......@@ -409,6 +410,7 @@ describe("config", () => {
plugins: ["mock-plugin-unnamed", "mock-plugin-named"],
transform: {
"\\.unnamed$": "mock-plugin-unnamed",
"\\.default$": "mock-plugin-named",
"\\.named$": "mock-plugin-named:foobar",
"\\.nonplugin$": "mock-transform",
},
......@@ -454,6 +456,25 @@ describe("config", () => {
`);
});
it("unnamed default", () => {
expect.assertions(1);
source.filename = "bar.default";
expect(config.transformSource(source)).toMatchInlineSnapshot(`
Array [
Object {
"column": 3,
"data": "transformed from bar.default",
"filename": "bar.default",
"line": 2,
"offset": 4,
"transformedBy": Array [
"mock-plugin-named",
],
},
]
`);
});
it("non-plugin (regression test issue 54)", () => {
expect.assertions(1);
source.filename = "bar.nonplugin";
......
......@@ -601,6 +601,9 @@ export class Config {
}
if (typeof plugin.transformer !== "function") {
if (plugin.transformer.default) {
return plugin.transformer.default;
}
throw new ConfigError(
`Transformer "${name}" refers to unnamed transformer but plugin exposes only named.`
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment