|
2 | 2 | import { cyan, yellow, red } from 'kleur/colors'; |
3 | 3 | import debug from 'debug'; |
4 | 4 | import { ResolvedOptions, Warning } from './options'; |
| 5 | +import { SvelteRequest } from './id'; |
5 | 6 |
|
6 | 7 | const levels: string[] = ['debug', 'info', 'warn', 'error', 'silent']; |
7 | 8 | const prefix = 'vite-plugin-svelte'; |
@@ -99,18 +100,54 @@ export const log = { |
99 | 100 | setLevel |
100 | 101 | }; |
101 | 102 |
|
102 | | -export function logCompilerWarnings(warnings: Warning[], options: ResolvedOptions) { |
| 103 | +export type SvelteWarningsMessage = { |
| 104 | + id: string; |
| 105 | + filename: string; |
| 106 | + normalizedFilename: string; |
| 107 | + timestamp: number; |
| 108 | + warnings: Warning[]; // allWarnings filtered by warnings where onwarn did not call the default handler |
| 109 | + allWarnings: Warning[]; // includes warnings filtered by onwarn and our extra vite plugin svelte warnings |
| 110 | + rawWarnings: Warning[]; // raw compiler output |
| 111 | +}; |
| 112 | + |
| 113 | +export function logCompilerWarnings( |
| 114 | + svelteRequest: SvelteRequest, |
| 115 | + warnings: Warning[], |
| 116 | + options: ResolvedOptions |
| 117 | +) { |
103 | 118 | const { emitCss, onwarn, isBuild } = options; |
104 | | - const warn = isBuild ? warnBuild : warnDev; |
105 | | - const notIgnoredWarnings = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss)); |
106 | | - const extraWarnings = buildExtraWarnings(warnings, isBuild); |
107 | | - [...notIgnoredWarnings, ...extraWarnings].forEach((warning) => { |
| 119 | + const sendViaWS = !isBuild && options.experimental?.sendWarningsToBrowser; |
| 120 | + let warn = isBuild ? warnBuild : warnDev; |
| 121 | + const handledByDefaultWarn: Warning[] = []; |
| 122 | + const notIgnored = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss)); |
| 123 | + const extra = buildExtraWarnings(warnings, isBuild); |
| 124 | + const allWarnings = [...notIgnored, ...extra]; |
| 125 | + if (sendViaWS) { |
| 126 | + warn = (w: Warning) => { |
| 127 | + handledByDefaultWarn.push(w); |
| 128 | + warn(w); |
| 129 | + }; |
| 130 | + } |
| 131 | + allWarnings.forEach((warning) => { |
108 | 132 | if (onwarn) { |
109 | 133 | onwarn(warning, warn); |
110 | 134 | } else { |
111 | 135 | warn(warning); |
112 | 136 | } |
113 | 137 | }); |
| 138 | + if (sendViaWS) { |
| 139 | + const message: SvelteWarningsMessage = { |
| 140 | + id: svelteRequest.id, |
| 141 | + filename: svelteRequest.filename, |
| 142 | + normalizedFilename: svelteRequest.normalizedFilename, |
| 143 | + timestamp: svelteRequest.timestamp, |
| 144 | + warnings: handledByDefaultWarn, // allWarnings filtered by warnings where onwarn did not call the default handler |
| 145 | + allWarnings, // includes warnings filtered by onwarn and our extra vite plugin svelte warnings |
| 146 | + rawWarnings: warnings // raw compiler output |
| 147 | + }; |
| 148 | + log.debug(`sending svelte:warnings message for ${svelteRequest.normalizedFilename}`); |
| 149 | + options.server?.ws?.send('svelte:warnings', message); |
| 150 | + } |
114 | 151 | } |
115 | 152 |
|
116 | 153 | function ignoreCompilerWarning( |
|
0 commit comments