Skip to content

Commit 7758c8b

Browse files
authored
refactor: debug (#749)
* refactor(debug): make it easier to get debug logs without being spammy * fix: update docs to use correct namespace * remove very noisy log about css hash
1 parent 15d9562 commit 7758c8b

File tree

11 files changed

+65
-29
lines changed

11 files changed

+65
-29
lines changed

.changeset/poor-wasps-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
breaking(debug): remove 'vite:' and add suffixes to debug namespace

docs/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export default {
167167
There is no golden rule, but you can follow these recommendations:
168168

169169
1. **Never** combine plugins or preprocessors that rewrite imports with prebundling
170-
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.
170+
2. Start with index imports and if your dev-server or build process feels slow, run the process with `DEBUG="vite-plugin-svelte:stats"` to check compile stats to see if switching to deep imports can improve the experience.
171171
3. Do not mix deep and index imports for the same library, use one style consistently.
172172
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.
173173

packages/vite-plugin-svelte/src/handle-hot-update.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { toRollupError } from './utils/error.js';
1414
export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options) {
1515
if (!cache.has(svelteRequest)) {
1616
// file hasn't been requested yet (e.g. async component)
17-
log.debug(`handleHotUpdate called before initial transform for ${svelteRequest.id}`);
17+
log.debug(
18+
`handleHotUpdate called before initial transform for ${svelteRequest.id}`,
19+
undefined,
20+
'hmr'
21+
);
1822
return;
1923
}
2024
const { read, server, modules } = ctx;
@@ -39,15 +43,15 @@ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache,
3943
if (cssIdx > -1) {
4044
const cssUpdated = cssChanged(cachedCss, compileData.compiled.css);
4145
if (!cssUpdated) {
42-
log.debug(`skipping unchanged css for ${svelteRequest.cssId}`);
46+
log.debug(`skipping unchanged css for ${svelteRequest.cssId}`, undefined, 'hmr');
4347
affectedModules.splice(cssIdx, 1);
4448
}
4549
}
4650
const jsIdx = modules.findIndex((m) => m.id === svelteRequest.id);
4751
if (jsIdx > -1) {
4852
const jsUpdated = jsChanged(cachedJS, compileData.compiled.js, svelteRequest.filename);
4953
if (!jsUpdated) {
50-
log.debug(`skipping unchanged js for ${svelteRequest.id}`);
54+
log.debug(`skipping unchanged js for ${svelteRequest.id}`, undefined, 'hmr');
5155
affectedModules.splice(jsIdx, 1);
5256
// transform won't be called, log warnings here
5357
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
@@ -57,14 +61,20 @@ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache,
5761
// TODO is this enough? see also: https://github.com/vitejs/vite/issues/2274
5862
const ssrModulesToInvalidate = affectedModules.filter((m) => !!m.ssrTransformResult);
5963
if (ssrModulesToInvalidate.length > 0) {
60-
log.debug(`invalidating modules ${ssrModulesToInvalidate.map((m) => m.id).join(', ')}`);
64+
log.debug(
65+
`invalidating modules ${ssrModulesToInvalidate.map((m) => m.id).join(', ')}`,
66+
undefined,
67+
'hmr'
68+
);
6169
ssrModulesToInvalidate.forEach((moduleNode) => server.moduleGraph.invalidateModule(moduleNode));
6270
}
6371
if (affectedModules.length > 0) {
6472
log.debug(
6573
`handleHotUpdate for ${svelteRequest.id} result: ${affectedModules
6674
.map((m) => m.id)
67-
.join(', ')}`
75+
.join(', ')}`,
76+
undefined,
77+
'hmr'
6878
);
6979
}
7080
return affectedModules;

packages/vite-plugin-svelte/src/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function svelte(inlineOptions) {
5757
options = await preResolveOptions(inlineOptions, config, configEnv);
5858
// extra vite config
5959
const extraViteConfig = await buildExtraViteConfig(options, config);
60-
log.debug('additional vite config', extraViteConfig);
60+
log.debug('additional vite config', extraViteConfig, 'config');
6161
return extraViteConfig;
6262
},
6363

@@ -69,7 +69,7 @@ export function svelte(inlineOptions) {
6969
viteConfig = config;
7070
// TODO deep clone to avoid mutability from outside?
7171
api.options = options;
72-
log.debug('resolved options', options);
72+
log.debug('resolved options', options, 'config');
7373
},
7474

7575
async buildStart() {
@@ -105,13 +105,12 @@ export function svelte(inlineOptions) {
105105
if (query.svelte && query.type === 'style') {
106106
const css = cache.getCSS(svelteRequest);
107107
if (css) {
108-
log.debug(`load returns css for ${filename}`);
109108
return css;
110109
}
111110
}
112111
// prevent vite asset plugin from loading files as url that should be compiled in transform
113112
if (viteConfig.assetsInclude(filename)) {
114-
log.debug(`load returns raw content for ${filename}`);
113+
log.debug(`load returns raw content for ${filename}`, undefined, 'load');
115114
return fs.readFileSync(filename, 'utf-8');
116115
}
117116
}
@@ -125,7 +124,11 @@ export function svelte(inlineOptions) {
125124
if (svelteRequest.query.type === 'style' && !svelteRequest.raw) {
126125
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
127126
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
128-
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
127+
log.debug(
128+
`resolveId resolved virtual css module ${svelteRequest.cssId}`,
129+
undefined,
130+
'resolve'
131+
);
129132
return svelteRequest.cssId;
130133
}
131134
}
@@ -157,7 +160,6 @@ export function svelte(inlineOptions) {
157160
}
158161
}
159162
}
160-
log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
161163
return {
162164
...compileData.compiled.js,
163165
meta: {

packages/vite-plugin-svelte/src/utils/compile.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const _createCompileSvelte = (makeHot) => {
5656

5757
if (options.hot && options.emitCss) {
5858
const hash = `s-${safeBase64Hash(normalizedFilename)}`;
59-
log.debug(`setting cssHash ${hash} for ${normalizedFilename}`);
6059
compileOptions.cssHash = () => hash;
6160
}
6261
if (ssr && compileOptions.enableSourcemap !== false) {
@@ -107,7 +106,9 @@ export const _createCompileSvelte = (makeHot) => {
107106
});
108107
if (dynamicCompileOptions && log.debug.enabled) {
109108
log.debug(
110-
`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`
109+
`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`,
110+
undefined,
111+
'compile'
111112
);
112113
}
113114
const finalCompileOptions = dynamicCompileOptions

packages/vite-plugin-svelte/src/utils/load-raw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
6464
}" combined with direct in ${id}. supported are: ${supportedDirectTypes.join(', ')}`
6565
);
6666
}
67-
log.debug(`load returns direct result for ${id}`);
67+
log.debug(`load returns direct result for ${id}`, undefined, 'load');
6868
let directOutput = result.code;
6969
if (query.sourcemap && result.map?.toUrl) {
7070
const map = `sourceMappingURL=${result.map.toUrl()}`;
@@ -76,7 +76,7 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
7676
}
7777
return directOutput;
7878
} else if (query.raw) {
79-
log.debug(`load returns raw result for ${id}`);
79+
log.debug(`load returns raw result for ${id}`, undefined, 'load');
8080
return toRawExports(result);
8181
} else {
8282
throw new Error(`invalid raw mode in ${id}, supported are raw, direct`);

packages/vite-plugin-svelte/src/utils/load-svelte-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function findConfigToLoad(viteConfig, inlineOptions) {
104104
.map((candidate) => path.resolve(root, candidate))
105105
.filter((file) => fs.existsSync(file));
106106
if (existingKnownConfigFiles.length === 0) {
107-
log.debug(`no svelte config found at ${root}`);
107+
log.debug(`no svelte config found at ${root}`, undefined, 'config');
108108
return;
109109
} else if (existingKnownConfigFiles.length > 1) {
110110
log.warn(

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const prefix = 'vite-plugin-svelte';
88
/** @type {Record<import('../types/log.d.ts').LogLevel, any>} */
99
const loggers = {
1010
debug: {
11-
log: debug(`vite:${prefix}`),
11+
log: debug(`${prefix}`),
1212
enabled: false,
1313
isDebug: true
1414
},
@@ -65,7 +65,13 @@ function _log(logger, message, payload, namespace) {
6565
return;
6666
}
6767
if (logger.isDebug) {
68-
const log = namespace ? logger.log.extend(namespace) : logger.log;
68+
let log = logger.log;
69+
if (namespace) {
70+
if (!isDebugNamespaceEnabled(namespace)) {
71+
return;
72+
}
73+
log = logger.log.extend(namespace);
74+
}
6975
payload !== undefined ? log(message, payload) : log(message);
7076
} else {
7177
logger.log(
@@ -251,5 +257,5 @@ export function buildExtendedLogMessage(w) {
251257
* @returns {boolean}
252258
*/
253259
export function isDebugNamespaceEnabled(namespace) {
254-
return debug.enabled(`vite:${prefix}:${namespace}`);
260+
return debug.enabled(`${prefix}:${namespace}`);
255261
}

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export async function buildExtraViteConfig(options, config) {
429429
(options.hot && options.hot.partialAccept !== false)) && // deviate from svelte-hmr, default to true
430430
config.experimental?.hmrPartialAccept !== false
431431
) {
432-
log.debug('enabling "experimental.hmrPartialAccept" in vite config');
432+
log.debug('enabling "experimental.hmrPartialAccept" in vite config', undefined, 'config');
433433
extraViteConfig.experimental = { hmrPartialAccept: true };
434434
}
435435
validateViteConfig(extraViteConfig, config, options);
@@ -526,7 +526,7 @@ async function buildExtraConfigForDependencies(options, config) {
526526
].join('\n')}\n\nPlease see ${FAQ_LINK_MISSING_EXPORTS_CONDITION} for details.`
527527
);
528528
}
529-
log.debug('extra config for dependencies generated by vitefu', depsConfig);
529+
log.debug('extra config for dependencies generated by vitefu', depsConfig, 'config');
530530

531531
if (options.prebundleSvelteLibraries) {
532532
// prebundling enabled, so we don't need extra dependency excludes
@@ -560,7 +560,7 @@ async function buildExtraConfigForDependencies(options, config) {
560560
});
561561
}
562562

563-
log.debug('post-processed extra config for dependencies', depsConfig);
563+
log.debug('post-processed extra config for dependencies', depsConfig, 'config');
564564

565565
return depsConfig;
566566
}
@@ -577,11 +577,17 @@ function buildExtraConfigForSvelte(config) {
577577
if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
578578
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== 'svelte/ssr'); // not used on clientside
579579
log.debug(
580-
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `
580+
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `,
581+
undefined,
582+
'config'
581583
);
582584
include.push(...svelteImportsToInclude);
583585
} else {
584-
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
586+
log.debug(
587+
'"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.',
588+
undefined,
589+
'config'
590+
);
585591
}
586592
/** @type {(string | RegExp)[]} */
587593
const noExternal = [];

packages/vite-plugin-svelte/src/utils/preprocess.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,18 @@ function buildExtraPreprocessors(options, config) {
8484
log.debug(
8585
`Ignoring svelte preprocessors defined by these vite plugins: ${ignored
8686
.map((p) => p.name)
87-
.join(', ')}`
87+
.join(', ')}`,
88+
undefined,
89+
'preprocess'
8890
);
8991
}
9092
if (included.length > 0) {
9193
log.debug(
9294
`Adding svelte preprocessors defined by these vite plugins: ${included
9395
.map((p) => p.name)
94-
.join(', ')}`
96+
.join(', ')}`,
97+
undefined,
98+
'preprocess'
9599
);
96100
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
97101
}

packages/vite-plugin-svelte/src/utils/watch.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export function setupWatchers(options, cache, requestParser) {
2222
dependants.forEach((dependant) => {
2323
if (fs.existsSync(dependant)) {
2424
log.debug(
25-
`emitting virtual change event for "${dependant}" because depdendency "${filename}" changed`
25+
`emitting virtual change event for "${dependant}" because depdendency "${filename}" changed`,
26+
undefined,
27+
'hmr'
2628
);
2729
watcher.emit('change', dependant);
2830
}
@@ -34,7 +36,7 @@ export function setupWatchers(options, cache, requestParser) {
3436
if (svelteRequest) {
3537
const removedFromCache = cache.remove(svelteRequest);
3638
if (removedFromCache) {
37-
log.debug(`cleared VitePluginSvelteCache for deleted file ${filename}`);
39+
log.debug(`cleared VitePluginSvelteCache for deleted file ${filename}`, undefined, 'hmr');
3840
}
3941
}
4042
};

0 commit comments

Comments
 (0)