Skip to content

Commit

Permalink
Add this.error() method in plugin context
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Aug 31, 2021
1 parent 3425fc7 commit 9c7be51
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eight-teachers-sit.md
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Fix missing `this.error()` method in Rollup plugins
10 changes: 10 additions & 0 deletions packages/wmr/src/lib/rollup-plugin-container.js
Expand Up @@ -156,6 +156,16 @@ export function createPluginContainer(plugins, opts = {}) {
warn(...args) {
// eslint-disable-next-line no-console
console.log(`[${plugin.name}]`, ...args);
},
// Rollup has typed the return value as `never` because they
// throw and abort the build. Doing that in "development"
// mode would be very annoying so we intentionally have a
// type mismatch.
// @ts-ignore
error(err) {
if (typeof err === 'string') err = { message: err };
// eslint-disable-next-line no-console
console.log(`[${plugin.name}]`, err.message);
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/wmr/test/fixtures/plugin-error/public/index.html
@@ -0,0 +1,2 @@
<h1>check console</h1>
<script type="module" src="index.js"></script>
Empty file.
16 changes: 16 additions & 0 deletions packages/wmr/test/fixtures/plugin-error/wmr.config.mjs
@@ -0,0 +1,16 @@
export default {
plugins: [
{
name: 'plugin-a',
resolveId() {
this.error(`oh no #1`);
},
load() {
this.error(`oh no #2`);
},
transform() {
this.error(`oh no #3`);
}
}
]
};
11 changes: 10 additions & 1 deletion packages/wmr/test/plugins/plugins.test.js
@@ -1,4 +1,4 @@
import { loadFixture, runWmrFast, setupTest, teardown, waitForMessage, withLog } from '../test-helpers.js';
import { getOutput, loadFixture, runWmrFast, setupTest, teardown, waitForMessage, withLog } from '../test-helpers.js';

jest.setTimeout(30000);

Expand Down Expand Up @@ -45,5 +45,14 @@ describe('config', () => {
await waitForMessage(instance.output, /OPTIONS format: esm/);
});
});

it('should support `this.error()`', async () => {
await loadFixture('plugin-error', env);
instance = await runWmrFast(env.tmp.path);
await getOutput(env, instance);
await waitForMessage(instance.output, /oh no #1/);
await waitForMessage(instance.output, /oh no #2/);
await waitForMessage(instance.output, /oh no #3/);
});
});
});

0 comments on commit 9c7be51

Please sign in to comment.