Skip to content

feat: add option to send warnings to dev client via websocket #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-pianos-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

New experimental option sendWarningsToBrowser
15 changes: 15 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,18 @@ export default defineConfig({
]
});
```

### sendWarningsToBrowser

- **Type:** `boolean`
- **Default:** `false`

Sends a websocket message `svelte:warnings` with the warnings that are passed to `onwarn`. This is only useful if you build a custom browser based integration where you want to display these.

**Example**

```js
import.meta.hot.on('svelte:warnings', (warnings) =>
console.log('received svelte warnings', warnings)
);
```
6 changes: 5 additions & 1 deletion packages/vite-plugin-svelte/src/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ export function logCompilerWarnings(warnings: Warning[], options: ResolvedOption
const warn = isBuild ? warnBuild : warnDev;
const notIgnoredWarnings = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
const extraWarnings = buildExtraWarnings(warnings, isBuild);
[...notIgnoredWarnings, ...extraWarnings].forEach((warning) => {
const warningsToSend = [...notIgnoredWarnings, ...extraWarnings];
warningsToSend.forEach((warning) => {
if (onwarn) {
onwarn(warning, warn);
} else {
warn(warning);
}
});
if (!isBuild && options.experimental?.sendWarningsToBrowser) {
options.server?.ws?.send('svelte:warnings', warningsToSend);
}
}

function ignoreCompilerWarning(
Expand Down
8 changes: 7 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ export interface ExperimentalOptions {
* enable svelte inspector
*/
inspector?: InspectorOptions | boolean;

/**
* send a websocket message with svelte compiler warnings during dev
*
*/
sendWarningsToBrowser?: boolean;
}

export interface InspectorOptions {
Expand Down Expand Up @@ -609,7 +615,7 @@ export interface InspectorOptions {
export interface PreResolvedOptions extends Options {
// these options are non-nullable after resolve
compilerOptions: CompileOptions;
experimental: ExperimentalOptions;
experimental?: ExperimentalOptions;
// extra options
root: string;
isBuild: boolean;
Expand Down