Skip to content
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

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

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

Filter by extension

Filter by extension

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

New experimental option sendWarningsToBrowser
30 changes: 30 additions & 0 deletions docs/config.md
Expand Up @@ -332,3 +332,33 @@ 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', (message) => {
// handle warnings message, eg log to console
console.warn(`Warnings for ${message.filename}`, message.warnings);
});
```

**Message format**

```ts
type SvelteWarningsMessage = {
id: string;
filename: string;
normalizedFilename: string;
timestamp: number;
warnings: Warning[]; // allWarnings filtered by warnings where onwarn did not call the default handler
allWarnings: Warning[]; // includes warnings filtered by onwarn and our extra vite plugin svelte warnings
rawWarnings: Warning[]; // raw compiler output
};
```
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/handle-hot-update.ts
Expand Up @@ -47,7 +47,7 @@ export async function handleHotUpdate(

if (!jsUpdated) {
// transform won't be called, log warnings here
logCompilerWarnings(compileData.compiled.warnings, options);
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
}

const result = [...affectedModules].filter(Boolean) as ModuleNode[];
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-svelte/src/index.ts
Expand Up @@ -185,7 +185,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
} catch (e) {
throw toRollupError(e, options);
}
logCompilerWarnings(compileData.compiled.warnings, options);
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
cache.update(compileData);
if (compileData.dependencies?.length && options.server) {
compileData.dependencies.forEach((d) => {
Expand Down Expand Up @@ -232,3 +232,5 @@ export {
Processed,
Warning
} from './utils/options';

export { SvelteWarningsMessage } from './utils/log';
47 changes: 42 additions & 5 deletions packages/vite-plugin-svelte/src/utils/log.ts
Expand Up @@ -2,6 +2,7 @@
import { cyan, yellow, red } from 'kleur/colors';
import debug from 'debug';
import { ResolvedOptions, Warning } from './options';
import { SvelteRequest } from './id';

const levels: string[] = ['debug', 'info', 'warn', 'error', 'silent'];
const prefix = 'vite-plugin-svelte';
Expand Down Expand Up @@ -99,18 +100,54 @@ export const log = {
setLevel
};

export function logCompilerWarnings(warnings: Warning[], options: ResolvedOptions) {
export type SvelteWarningsMessage = {
id: string;
filename: string;
normalizedFilename: string;
timestamp: number;
warnings: Warning[]; // allWarnings filtered by warnings where onwarn did not call the default handler
allWarnings: Warning[]; // includes warnings filtered by onwarn and our extra vite plugin svelte warnings
rawWarnings: Warning[]; // raw compiler output
};

export function logCompilerWarnings(
svelteRequest: SvelteRequest,
warnings: Warning[],
options: ResolvedOptions
) {
const { emitCss, onwarn, isBuild } = options;
const warn = isBuild ? warnBuild : warnDev;
const notIgnoredWarnings = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
const extraWarnings = buildExtraWarnings(warnings, isBuild);
[...notIgnoredWarnings, ...extraWarnings].forEach((warning) => {
const sendViaWS = !isBuild && options.experimental?.sendWarningsToBrowser;
let warn = isBuild ? warnBuild : warnDev;
const handledByDefaultWarn: Warning[] = [];
const notIgnored = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
const extra = buildExtraWarnings(warnings, isBuild);
const allWarnings = [...notIgnored, ...extra];
if (sendViaWS) {
warn = (w: Warning) => {
handledByDefaultWarn.push(w);
warn(w);
};
}
allWarnings.forEach((warning) => {
if (onwarn) {
onwarn(warning, warn);
} else {
warn(warning);
}
});
if (sendViaWS) {
const message: SvelteWarningsMessage = {
id: svelteRequest.id,
filename: svelteRequest.filename,
normalizedFilename: svelteRequest.normalizedFilename,
timestamp: svelteRequest.timestamp,
warnings: handledByDefaultWarn, // allWarnings filtered by warnings where onwarn did not call the default handler
allWarnings, // includes warnings filtered by onwarn and our extra vite plugin svelte warnings
rawWarnings: warnings // raw compiler output
};
log.debug(`sending svelte:warnings message for ${svelteRequest.normalizedFilename}`);
options.server?.ws?.send('svelte:warnings', message);
}
}

function ignoreCompilerWarning(
Expand Down
8 changes: 7 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
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