Skip to content

Commit 568341e

Browse files
bluwydominikg
andauthored
fix: log stats in debug mode (#614)
Co-authored-by: Dominik G <[email protected]>
1 parent 45dd9e8 commit 568341e

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

.changeset/strong-coins-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
Log stats in debug mode and remove `experimental.disableCompileStats` option. Use `DEBUG="vite:vite-plugin-svelte:stats"` when starting the dev server or build to log the compile stats.

docs/config.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,3 @@ export default {
403403
rawWarnings: Warning[]; // raw compiler output
404404
};
405405
```
406-
407-
### disableCompileStats
408-
409-
- **Type** `boolean | 'dev' | 'build'`
410-
- **Default:** `false`
411-
412-
disable svelte compile statistics.

docs/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export default {
169169
There is no golden rule, but you can follow these recommendations:
170170

171171
1. **Never** combine plugins or preprocessors that rewrite imports with prebundling
172-
2. Start with index imports and if your dev-server or build process feels slow, check compile stats to see if switching to deep imports can improve the experience.
172+
2. Start with index imports and if your dev-server or build process feels slow, run the process with `DEBUG="vite:vite-plugin-svelte:stats"` to check compile stats to see if switching to deep imports can improve the experience.
173173
3. Do not mix deep and index imports for the same library, use one style consistently.
174174
4. Use different import styles for different libraries where it helps. E.g. deep imports for the few icons of that one huge icon library, but index import for the component library that is heavily used.
175175

packages/vite-plugin-svelte/src/utils/log.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { cyan, yellow, red } from 'kleur/colors';
33
import debug from 'debug';
44
import { ResolvedOptions, Warning } from './options';
55
import { SvelteRequest } from './id';
6-
76
const levels: string[] = ['debug', 'info', 'warn', 'error', 'silent'];
87
const prefix = 'vite-plugin-svelte';
98
const loggers: { [key: string]: any } = {
@@ -48,36 +47,43 @@ function setLevel(level: string) {
4847
}
4948
}
5049

51-
function _log(logger: any, message: string, payload?: any) {
50+
function _log(logger: any, message: string, payload?: any, namespace?: string) {
5251
if (!logger.enabled) {
5352
return;
5453
}
5554
if (logger.isDebug) {
56-
payload !== undefined ? logger.log(message, payload) : logger.log(message);
55+
const log = namespace ? logger.log.extend(namespace) : logger.log;
56+
payload !== undefined ? log(message, payload) : log(message);
5757
} else {
58-
logger.log(logger.color(`${new Date().toLocaleTimeString()} [${prefix}] ${message}`));
58+
logger.log(
59+
logger.color(
60+
`${new Date().toLocaleTimeString()} [${prefix}${
61+
namespace ? `:${namespace}` : ''
62+
}] ${message}`
63+
)
64+
);
5965
if (payload) {
6066
logger.log(payload);
6167
}
6268
}
6369
}
6470

6571
export interface LogFn {
66-
(message: string, payload?: any): void;
72+
(message: string, payload?: any, namespace?: string): void;
6773
enabled: boolean;
68-
once: (message: string, payload?: any) => void;
74+
once: (message: string, payload?: any, namespace?: string) => void;
6975
}
7076

7177
function createLogger(level: string): LogFn {
7278
const logger = loggers[level];
7379
const logFn: LogFn = _log.bind(null, logger) as LogFn;
7480
const logged = new Set<String>();
75-
const once = function (message: string, payload?: any) {
81+
const once = function (message: string, payload?: any, namespace?: string) {
7682
if (logged.has(message)) {
7783
return;
7884
}
7985
logged.add(message);
80-
logFn.apply(null, [message, payload]);
86+
logFn.apply(null, [message, payload, namespace]);
8187
};
8288
Object.defineProperty(logFn, 'enabled', {
8389
get() {
@@ -209,3 +215,7 @@ export function buildExtendedLogMessage(w: Warning) {
209215
}
210216
return parts.join('');
211217
}
218+
219+
export function isDebugNamespaceEnabled(namespace: string) {
220+
return debug.enabled(`vite:${prefix}:${namespace}`);
221+
}

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-vars */
22
import { ConfigEnv, ResolvedConfig, UserConfig, ViteDevServer, normalizePath } from 'vite';
3-
import { log } from './log';
3+
import { isDebugNamespaceEnabled, log } from './log';
44
import { loadSvelteConfig } from './load-svelte-config';
55
import {
66
SVELTE_EXPORT_CONDITIONS,
@@ -206,11 +206,7 @@ export function resolveOptions(
206206
enforceOptionsForHmr(merged);
207207
enforceOptionsForProduction(merged);
208208
// mergeConfigs would mangle functions on the stats class, so do this afterwards
209-
const isLogLevelInfo = [undefined, 'info'].includes(viteConfig.logLevel);
210-
const disableCompileStats = merged.experimental?.disableCompileStats;
211-
const statsEnabled =
212-
disableCompileStats !== true && disableCompileStats !== (merged.isBuild ? 'build' : 'dev');
213-
if (statsEnabled && isLogLevelInfo) {
209+
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
214210
merged.stats = new VitePluginSvelteStats();
215211
}
216212
return merged;
@@ -725,13 +721,6 @@ export interface ExperimentalOptions {
725721
*
726722
*/
727723
sendWarningsToBrowser?: boolean;
728-
729-
/**
730-
* disable svelte compile statistics
731-
*
732-
* @default false
733-
*/
734-
disableCompileStats?: 'dev' | 'build' | boolean;
735724
}
736725

737726
export interface InspectorOptions {

packages/vite-plugin-svelte/src/utils/vite-plugin-svelte-stats.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class VitePluginSvelteStats {
140140
stats.push(stat);
141141
if (!hasLoggedProgress && options.logInProgress(collection, now)) {
142142
hasLoggedProgress = true;
143-
log.info(`${name} in progress ...`);
143+
log.debug(`${name} in progress ...`, undefined, 'stats');
144144
}
145145
};
146146
},
@@ -164,7 +164,11 @@ export class VitePluginSvelteStats {
164164
const logResult = collection.options.logResult(collection);
165165
if (logResult) {
166166
await this._aggregateStatsResult(collection);
167-
log.info(`${collection.name} done.`, formatPackageStats(collection.packageStats!));
167+
log.debug(
168+
`${collection.name} done.\n${formatPackageStats(collection.packageStats!)}`,
169+
undefined,
170+
'stats'
171+
);
168172
}
169173
// cut some ties to free it for garbage collection
170174
const index = this._collections.indexOf(collection);
@@ -179,7 +183,7 @@ export class VitePluginSvelteStats {
179183
collection.finish = () => {};
180184
} catch (e) {
181185
// this should not happen, but stats taking also should not break the process
182-
log.debug.once(`failed to finish stats for ${collection.name}`, e);
186+
log.debug.once(`failed to finish stats for ${collection.name}\n`, e, 'stats');
183187
}
184188
}
185189

0 commit comments

Comments
 (0)