@sveltejs/vite-plugin-svelte 6.0.0-next.1 → 6.0.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/index.js +27 -258
- package/src/plugins/compile-module.js +51 -0
- package/src/plugins/compile.js +73 -0
- package/src/plugins/configure.js +99 -0
- package/src/plugins/hot-update.js +181 -0
- package/src/plugins/load-compiled-css.js +46 -0
- package/src/plugins/load-custom.js +42 -0
- package/src/plugins/preprocess.js +133 -0
- package/src/{utils/optimizer-plugins.js → plugins/setup-optimizer.js} +153 -8
- package/src/types/compile.d.ts +20 -2
- package/src/types/id.d.ts +1 -8
- package/src/types/plugin-api.d.ts +14 -7
- package/src/utils/compile.js +18 -41
- package/src/utils/constants.js +1 -4
- package/src/utils/dependencies.js +0 -29
- package/src/utils/id.js +7 -4
- package/src/utils/options.js +4 -89
- package/src/utils/preprocess.js +2 -44
- package/src/utils/vite-plugin-svelte-cache.js +1 -60
- package/src/utils/vite-plugin-svelte-stats.js +59 -11
- package/src/utils/watch.js +3 -13
- package/types/index.d.ts +5 -0
- package/types/index.d.ts.map +1 -1
- package/src/handle-hot-update.js +0 -145
- package/src/utils/load-raw.js +0 -125
- package/src/utils/optimizer.js +0 -53
package/src/utils/compile.js
CHANGED
|
@@ -2,10 +2,7 @@ import * as svelte from 'svelte/compiler';
|
|
|
2
2
|
import { safeBase64Hash } from './hash.js';
|
|
3
3
|
import { log } from './log.js';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
checkPreprocessDependencies,
|
|
7
|
-
createInjectScopeEverythingRulePreprocessorGroup
|
|
8
|
-
} from './preprocess.js';
|
|
5
|
+
import { checkPreprocessDependencies } from './preprocess.js';
|
|
9
6
|
import { mapToRelative } from './sourcemaps.js';
|
|
10
7
|
import { enhanceCompileError } from './error.js';
|
|
11
8
|
|
|
@@ -21,9 +18,8 @@ const scriptLangRE =
|
|
|
21
18
|
export function createCompileSvelte() {
|
|
22
19
|
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined} */
|
|
23
20
|
let stats;
|
|
24
|
-
const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
|
|
25
21
|
/** @type {import('../types/compile.d.ts').CompileSvelte} */
|
|
26
|
-
return async function compileSvelte(svelteRequest, code, options) {
|
|
22
|
+
return async function compileSvelte(svelteRequest, code, options, preprocessed) {
|
|
27
23
|
const { filename, normalizedFilename, cssId, ssr, raw } = svelteRequest;
|
|
28
24
|
const { emitCss = true } = options;
|
|
29
25
|
/** @type {string[]} */
|
|
@@ -55,6 +51,7 @@ export function createCompileSvelte() {
|
|
|
55
51
|
// also they for hmr updates too
|
|
56
52
|
}
|
|
57
53
|
}
|
|
54
|
+
|
|
58
55
|
/** @type {import('svelte/compiler').CompileOptions} */
|
|
59
56
|
const compileOptions = {
|
|
60
57
|
...options.compilerOptions,
|
|
@@ -62,31 +59,7 @@ export function createCompileSvelte() {
|
|
|
62
59
|
generate: ssr ? 'server' : 'client'
|
|
63
60
|
};
|
|
64
61
|
|
|
65
|
-
if (
|
|
66
|
-
const hash = `s-${safeBase64Hash(normalizedFilename)}`;
|
|
67
|
-
compileOptions.cssHash = () => hash;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let preprocessed;
|
|
71
|
-
let preprocessors = options.preprocess;
|
|
72
|
-
if (!options.isBuild && options.emitCss && compileOptions.hmr) {
|
|
73
|
-
// inject preprocessor that ensures css hmr works better
|
|
74
|
-
if (!Array.isArray(preprocessors)) {
|
|
75
|
-
preprocessors = preprocessors
|
|
76
|
-
? [preprocessors, devStylePreprocessor]
|
|
77
|
-
: [devStylePreprocessor];
|
|
78
|
-
} else {
|
|
79
|
-
preprocessors = preprocessors.concat(devStylePreprocessor);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (preprocessors) {
|
|
83
|
-
try {
|
|
84
|
-
preprocessed = await svelte.preprocess(code, preprocessors, { filename }); // full filename here so postcss works
|
|
85
|
-
} catch (e) {
|
|
86
|
-
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ''}`;
|
|
87
|
-
throw e;
|
|
88
|
-
}
|
|
89
|
-
|
|
62
|
+
if (preprocessed) {
|
|
90
63
|
if (preprocessed.dependencies?.length) {
|
|
91
64
|
const checked = checkPreprocessDependencies(filename, preprocessed.dependencies);
|
|
92
65
|
if (checked.warnings.length) {
|
|
@@ -99,16 +72,19 @@ export function createCompileSvelte() {
|
|
|
99
72
|
|
|
100
73
|
if (preprocessed.map) compileOptions.sourcemap = preprocessed.map;
|
|
101
74
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
75
|
+
|
|
76
|
+
let finalCode = code;
|
|
77
|
+
if (compileOptions.hmr && options.emitCss) {
|
|
78
|
+
const hash = `s-${safeBase64Hash(normalizedFilename)}`;
|
|
79
|
+
compileOptions.cssHash = () => hash;
|
|
80
|
+
const closeStylePos = code.lastIndexOf('</style>');
|
|
81
|
+
if (closeStylePos > -1) {
|
|
82
|
+
// inject rule that forces compile to attach scope class to every node in the template
|
|
83
|
+
// this reduces the amount of js hot updates when editing css in .svelte files
|
|
84
|
+
finalCode = finalCode.slice(0, closeStylePos) + ' *{}' + finalCode.slice(closeStylePos);
|
|
85
|
+
}
|
|
110
86
|
}
|
|
111
|
-
|
|
87
|
+
|
|
112
88
|
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
|
|
113
89
|
filename,
|
|
114
90
|
code: finalCode,
|
|
@@ -145,7 +121,7 @@ export function createCompileSvelte() {
|
|
|
145
121
|
);
|
|
146
122
|
}
|
|
147
123
|
} catch (e) {
|
|
148
|
-
enhanceCompileError(e, code,
|
|
124
|
+
enhanceCompileError(e, code, options.preprocess);
|
|
149
125
|
throw e;
|
|
150
126
|
}
|
|
151
127
|
|
|
@@ -181,6 +157,7 @@ export function createCompileSvelte() {
|
|
|
181
157
|
return {
|
|
182
158
|
filename,
|
|
183
159
|
normalizedFilename,
|
|
160
|
+
cssId,
|
|
184
161
|
lang,
|
|
185
162
|
compiled,
|
|
186
163
|
ssr,
|
package/src/utils/constants.js
CHANGED
|
@@ -30,7 +30,4 @@ export const DEFAULT_SVELTE_EXT = ['.svelte'];
|
|
|
30
30
|
export const DEFAULT_SVELTE_MODULE_INFIX = ['.svelte.'];
|
|
31
31
|
export const DEFAULT_SVELTE_MODULE_EXT = ['.js', '.ts'];
|
|
32
32
|
|
|
33
|
-
export const
|
|
34
|
-
export const SVELTE_VIRTUAL_STYLE_ID_REGEX = new RegExp(
|
|
35
|
-
`${SVELTE_VIRTUAL_STYLE_SUFFIX.replace(/[?.]/g, '\\$&')}$`
|
|
36
|
-
);
|
|
33
|
+
export const SVELTE_VIRTUAL_STYLE_ID_REGEX = /[?&]svelte&type=style&lang.css$/;
|
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fs from 'node:fs/promises';
|
|
3
|
-
import { findDepPkgJsonPath } from 'vitefu';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {{
|
|
7
|
-
* dir: string;
|
|
8
|
-
* pkg: Record<string, any>;
|
|
9
|
-
* }} DependencyData
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @param {string} dep
|
|
14
|
-
* @param {string} parent
|
|
15
|
-
* @returns {Promise<DependencyData | undefined>}
|
|
16
|
-
*/
|
|
17
|
-
export async function resolveDependencyData(dep, parent) {
|
|
18
|
-
const depDataPath = await findDepPkgJsonPath(dep, parent);
|
|
19
|
-
if (!depDataPath) return undefined;
|
|
20
|
-
try {
|
|
21
|
-
return {
|
|
22
|
-
dir: path.dirname(depDataPath),
|
|
23
|
-
pkg: JSON.parse(await fs.readFile(depDataPath, 'utf-8'))
|
|
24
|
-
};
|
|
25
|
-
} catch {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
1
|
const COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD = [
|
|
31
2
|
'@lukeed/uuid',
|
|
32
3
|
'@playwright/test',
|
package/src/utils/id.js
CHANGED
|
@@ -5,7 +5,8 @@ import { log } from './log.js';
|
|
|
5
5
|
import {
|
|
6
6
|
DEFAULT_SVELTE_EXT,
|
|
7
7
|
DEFAULT_SVELTE_MODULE_EXT,
|
|
8
|
-
DEFAULT_SVELTE_MODULE_INFIX
|
|
8
|
+
DEFAULT_SVELTE_MODULE_INFIX,
|
|
9
|
+
SVELTE_VIRTUAL_STYLE_ID_REGEX
|
|
9
10
|
} from './constants.js';
|
|
10
11
|
import { arraify } from './options.js';
|
|
11
12
|
|
|
@@ -179,13 +180,15 @@ export function buildIdFilter(options) {
|
|
|
179
180
|
.map(escapeRE)
|
|
180
181
|
.join('|')})(?:[?#]|$)`
|
|
181
182
|
);
|
|
182
|
-
|
|
183
|
+
return {
|
|
183
184
|
id: {
|
|
184
185
|
include: [extensionsRE, .../**@type {Array<string|RegExp>}*/ arraify(include)],
|
|
185
|
-
exclude: /**@type {Array<string|RegExp>}*/
|
|
186
|
+
exclude: /**@type {Array<string|RegExp>}*/ [
|
|
187
|
+
SVELTE_VIRTUAL_STYLE_ID_REGEX, // exclude from regular pipeline, we load it in a separate plugin
|
|
188
|
+
...arraify(exclude)
|
|
189
|
+
]
|
|
186
190
|
}
|
|
187
191
|
};
|
|
188
|
-
return filter;
|
|
189
192
|
}
|
|
190
193
|
|
|
191
194
|
/**
|
package/src/utils/options.js
CHANGED
|
@@ -5,11 +5,9 @@ const {
|
|
|
5
5
|
defaultServerMainFields,
|
|
6
6
|
defaultClientConditions,
|
|
7
7
|
defaultServerConditions,
|
|
8
|
-
normalizePath
|
|
9
|
-
//@ts-expect-error rolldownVersion not in type
|
|
10
|
-
rolldownVersion
|
|
8
|
+
normalizePath
|
|
11
9
|
} = vite;
|
|
12
|
-
import {
|
|
10
|
+
import { log } from './log.js';
|
|
13
11
|
import { loadSvelteConfig } from './load-svelte-config.js';
|
|
14
12
|
import {
|
|
15
13
|
DEFAULT_SVELTE_EXT,
|
|
@@ -20,12 +18,6 @@ import {
|
|
|
20
18
|
} from './constants.js';
|
|
21
19
|
|
|
22
20
|
import path from 'node:path';
|
|
23
|
-
import {
|
|
24
|
-
optimizeSvelteModulePluginName,
|
|
25
|
-
optimizeSveltePluginName,
|
|
26
|
-
patchESBuildOptimizerPlugin,
|
|
27
|
-
patchRolldownOptimizerPlugin
|
|
28
|
-
} from './optimizer-plugins.js';
|
|
29
21
|
import { addExtraPreprocessors } from './preprocess.js';
|
|
30
22
|
import deepmerge from 'deepmerge';
|
|
31
23
|
import {
|
|
@@ -37,7 +29,6 @@ import {
|
|
|
37
29
|
} from 'vitefu';
|
|
38
30
|
|
|
39
31
|
import { isCommonDepWithoutSvelteField } from './dependencies.js';
|
|
40
|
-
import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
|
|
41
32
|
|
|
42
33
|
const allowedPluginOptions = new Set([
|
|
43
34
|
'include',
|
|
@@ -198,10 +189,9 @@ function mergeConfigs(...configs) {
|
|
|
198
189
|
*
|
|
199
190
|
* @param {import('../types/options.d.ts').PreResolvedOptions} preResolveOptions
|
|
200
191
|
* @param {import('vite').ResolvedConfig} viteConfig
|
|
201
|
-
* @param {import('./vite-plugin-svelte-cache.js').VitePluginSvelteCache} cache
|
|
202
192
|
* @returns {import('../types/options.d.ts').ResolvedOptions}
|
|
203
193
|
*/
|
|
204
|
-
export function resolveOptions(preResolveOptions, viteConfig
|
|
194
|
+
export function resolveOptions(preResolveOptions, viteConfig) {
|
|
205
195
|
const css = preResolveOptions.emitCss ? 'external' : 'injected';
|
|
206
196
|
/** @type {Partial<import('../public.d.ts').Options>} */
|
|
207
197
|
const defaultOptions = {
|
|
@@ -230,10 +220,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
|
|
|
230
220
|
addExtraPreprocessors(merged, viteConfig);
|
|
231
221
|
enforceOptionsForHmr(merged, viteConfig);
|
|
232
222
|
enforceOptionsForProduction(merged);
|
|
233
|
-
|
|
234
|
-
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
|
|
235
|
-
merged.stats = new VitePluginSvelteStats(cache);
|
|
236
|
-
}
|
|
223
|
+
|
|
237
224
|
return merged;
|
|
238
225
|
}
|
|
239
226
|
|
|
@@ -384,46 +371,6 @@ export async function buildExtraViteConfig(options, config) {
|
|
|
384
371
|
]
|
|
385
372
|
};
|
|
386
373
|
|
|
387
|
-
// handle prebundling for svelte files
|
|
388
|
-
if (options.prebundleSvelteLibraries) {
|
|
389
|
-
extraViteConfig.optimizeDeps = {
|
|
390
|
-
...extraViteConfig.optimizeDeps,
|
|
391
|
-
// Experimental Vite API to allow these extensions to be scanned and prebundled
|
|
392
|
-
extensions: options.extensions ?? ['.svelte']
|
|
393
|
-
};
|
|
394
|
-
// Add optimizer plugins to prebundle Svelte files.
|
|
395
|
-
// Currently a placeholder as more information is needed after Vite config is resolved,
|
|
396
|
-
// the added plugins are patched in `patchResolvedViteConfig()`
|
|
397
|
-
if (rolldownVersion) {
|
|
398
|
-
/**
|
|
399
|
-
*
|
|
400
|
-
* @param {string} name
|
|
401
|
-
* @returns {import('vite').Rollup.Plugin}
|
|
402
|
-
*/
|
|
403
|
-
const placeholderRolldownOptimizerPlugin = (name) => ({
|
|
404
|
-
name,
|
|
405
|
-
options() {},
|
|
406
|
-
buildStart() {},
|
|
407
|
-
buildEnd() {},
|
|
408
|
-
transform: { filter: { id: /^$/ }, handler() {} }
|
|
409
|
-
});
|
|
410
|
-
//@ts-expect-error rolldown types not finished
|
|
411
|
-
extraViteConfig.optimizeDeps.rollupOptions = {
|
|
412
|
-
plugins: [
|
|
413
|
-
placeholderRolldownOptimizerPlugin(optimizeSveltePluginName),
|
|
414
|
-
placeholderRolldownOptimizerPlugin(optimizeSvelteModulePluginName)
|
|
415
|
-
]
|
|
416
|
-
};
|
|
417
|
-
} else {
|
|
418
|
-
extraViteConfig.optimizeDeps.esbuildOptions = {
|
|
419
|
-
plugins: [
|
|
420
|
-
{ name: optimizeSveltePluginName, setup: () => {} },
|
|
421
|
-
{ name: optimizeSvelteModulePluginName, setup: () => {} }
|
|
422
|
-
]
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
374
|
// enable hmrPartialAccept if not explicitly disabled
|
|
428
375
|
if (config.experimental?.hmrPartialAccept !== false) {
|
|
429
376
|
log.debug('enabling "experimental.hmrPartialAccept" in vite config', undefined, 'config');
|
|
@@ -607,38 +554,6 @@ function buildExtraConfigForSvelte(config) {
|
|
|
607
554
|
return { optimizeDeps: { include, exclude }, ssr: { noExternal, external } };
|
|
608
555
|
}
|
|
609
556
|
|
|
610
|
-
/**
|
|
611
|
-
* @param {import('vite').ResolvedConfig} viteConfig
|
|
612
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
613
|
-
*/
|
|
614
|
-
export function patchResolvedViteConfig(viteConfig, options) {
|
|
615
|
-
if (options.preprocess) {
|
|
616
|
-
for (const preprocessor of arraify(options.preprocess)) {
|
|
617
|
-
if (preprocessor.style && '__resolvedConfig' in preprocessor.style) {
|
|
618
|
-
preprocessor.style.__resolvedConfig = viteConfig;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
if (rolldownVersion) {
|
|
623
|
-
const plugins =
|
|
624
|
-
// @ts-expect-error not typed
|
|
625
|
-
viteConfig.optimizeDeps.rollupOptions?.plugins?.filter((p) =>
|
|
626
|
-
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
|
|
627
|
-
) ?? [];
|
|
628
|
-
for (const plugin of plugins) {
|
|
629
|
-
patchRolldownOptimizerPlugin(plugin, options);
|
|
630
|
-
}
|
|
631
|
-
} else {
|
|
632
|
-
const plugins =
|
|
633
|
-
viteConfig.optimizeDeps.esbuildOptions?.plugins?.filter((p) =>
|
|
634
|
-
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
|
|
635
|
-
) ?? [];
|
|
636
|
-
for (const plugin of plugins) {
|
|
637
|
-
patchESBuildOptimizerPlugin(plugin, options);
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
557
|
/**
|
|
643
558
|
* Mutates `config` to ensure `resolve.mainFields` is set. If unset, it emulates Vite's default fallback.
|
|
644
559
|
* @param {string} name
|
package/src/utils/preprocess.js
CHANGED
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
import MagicString from 'magic-string';
|
|
2
1
|
import { log } from './log.js';
|
|
3
|
-
import path from 'node:path';
|
|
4
2
|
import { normalizePath } from 'vite';
|
|
5
3
|
|
|
6
|
-
/**
|
|
7
|
-
* this appends a *{} rule to component styles to force the svelte compiler to add style classes to all nodes
|
|
8
|
-
* That means adding/removing class rules from <style> node won't trigger js updates as the scope classes are not changed
|
|
9
|
-
*
|
|
10
|
-
* only used during dev with enabled css hmr
|
|
11
|
-
*
|
|
12
|
-
* @returns {import('svelte/compiler').PreprocessorGroup}
|
|
13
|
-
*/
|
|
14
|
-
export function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
15
|
-
return {
|
|
16
|
-
name: 'inject-scope-everything-rule',
|
|
17
|
-
style({ content, filename }) {
|
|
18
|
-
const s = new MagicString(content);
|
|
19
|
-
s.append(' *{}');
|
|
20
|
-
return {
|
|
21
|
-
code: s.toString(),
|
|
22
|
-
map: s.generateDecodedMap({
|
|
23
|
-
source: filename ? path.basename(filename) : undefined,
|
|
24
|
-
hires: true
|
|
25
|
-
})
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
4
|
/**
|
|
32
5
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
33
6
|
* @param {import('vite').ResolvedConfig} config
|
|
@@ -42,28 +15,13 @@ function buildExtraPreprocessors(options, config) {
|
|
|
42
15
|
/** @type {import('svelte/compiler').PreprocessorGroup[]} */
|
|
43
16
|
const appendPreprocessors = [];
|
|
44
17
|
|
|
45
|
-
|
|
46
|
-
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
|
|
18
|
+
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p.api?.sveltePreprocess);
|
|
47
19
|
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|
|
48
20
|
log.warn(
|
|
49
|
-
`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to
|
|
21
|
+
`The following plugins use the deprecated 'plugin.api.sveltePreprocess' field. Please contact their maintainers and ask them to use a vite plugin transform instead: ${pluginsWithPreprocessorsDeprecated
|
|
50
22
|
.map((p) => p.name)
|
|
51
23
|
.join(', ')}`
|
|
52
24
|
);
|
|
53
|
-
// patch plugin to avoid breaking
|
|
54
|
-
pluginsWithPreprocessorsDeprecated.forEach((p) => {
|
|
55
|
-
if (!p.api) {
|
|
56
|
-
p.api = {};
|
|
57
|
-
}
|
|
58
|
-
if (p.api.sveltePreprocess === undefined) {
|
|
59
|
-
// @ts-expect-error not typed
|
|
60
|
-
p.api.sveltePreprocess = p.sveltePreprocess;
|
|
61
|
-
} else {
|
|
62
|
-
log.error(
|
|
63
|
-
`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
25
|
}
|
|
68
26
|
/** @type {import('vite').Plugin[]} */
|
|
69
27
|
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.api?.sveltePreprocess);
|
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs';
|
|
2
|
-
import { dirname } from 'node:path';
|
|
3
|
-
import { findClosestPkgJsonPath } from 'vitefu';
|
|
4
|
-
import { normalizePath } from 'vite';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {{
|
|
8
|
-
* name: string;
|
|
9
|
-
* version: string;
|
|
10
|
-
* svelte?: string;
|
|
11
|
-
* path: string;
|
|
12
|
-
* }} PackageInfo
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
1
|
/**
|
|
16
2
|
* @class
|
|
17
3
|
*/
|
|
@@ -26,8 +12,6 @@ export class VitePluginSvelteCache {
|
|
|
26
12
|
#dependants = new Map();
|
|
27
13
|
/** @type {Map<string, any>} */
|
|
28
14
|
#errors = new Map();
|
|
29
|
-
/** @type {PackageInfo[]} */
|
|
30
|
-
#packageInfos = [];
|
|
31
15
|
|
|
32
16
|
/**
|
|
33
17
|
* @param {import('../types/compile.d.ts').CompileData} compileData
|
|
@@ -149,6 +133,7 @@ export class VitePluginSvelteCache {
|
|
|
149
133
|
return this.#js.get(svelteRequest.normalizedFilename);
|
|
150
134
|
}
|
|
151
135
|
}
|
|
136
|
+
|
|
152
137
|
/**
|
|
153
138
|
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
|
|
154
139
|
* @returns {any}
|
|
@@ -165,48 +150,4 @@ export class VitePluginSvelteCache {
|
|
|
165
150
|
const dependants = this.#dependants.get(path);
|
|
166
151
|
return dependants ? [...dependants] : [];
|
|
167
152
|
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* @param {string} file
|
|
171
|
-
* @returns {Promise<PackageInfo>}
|
|
172
|
-
*/
|
|
173
|
-
async getPackageInfo(file) {
|
|
174
|
-
let info = this.#packageInfos.find((pi) => file.startsWith(pi.path));
|
|
175
|
-
if (!info) {
|
|
176
|
-
info = await findPackageInfo(file);
|
|
177
|
-
this.#packageInfos.push(info);
|
|
178
|
-
}
|
|
179
|
-
return info;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* utility to get some info from the closest package.json with a "name" set
|
|
185
|
-
*
|
|
186
|
-
* @param {string} file to find info for
|
|
187
|
-
* @returns {Promise<PackageInfo>}
|
|
188
|
-
*/
|
|
189
|
-
async function findPackageInfo(file) {
|
|
190
|
-
/** @type {PackageInfo} */
|
|
191
|
-
const info = {
|
|
192
|
-
name: '$unknown',
|
|
193
|
-
version: '0.0.0-unknown',
|
|
194
|
-
path: '$unknown'
|
|
195
|
-
};
|
|
196
|
-
let path = await findClosestPkgJsonPath(file, (pkgPath) => {
|
|
197
|
-
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
198
|
-
if (pkg.name != null) {
|
|
199
|
-
info.name = pkg.name;
|
|
200
|
-
if (pkg.version != null) {
|
|
201
|
-
info.version = pkg.version;
|
|
202
|
-
}
|
|
203
|
-
info.svelte = pkg.svelte;
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
return false;
|
|
207
|
-
});
|
|
208
|
-
// return normalized path with appended '/' so .startsWith works for future file checks
|
|
209
|
-
path = normalizePath(dirname(path ?? file)) + '/';
|
|
210
|
-
info.path = path;
|
|
211
|
-
return info;
|
|
212
153
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { log } from './log.js';
|
|
2
2
|
import { performance } from 'node:perf_hooks';
|
|
3
3
|
import { normalizePath } from 'vite';
|
|
4
|
+
import { findClosestPkgJsonPath } from 'vitefu';
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
4
7
|
|
|
5
8
|
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').CollectionOptions} */
|
|
6
9
|
const defaultCollectionOptions = {
|
|
@@ -63,19 +66,12 @@ function formatPackageStats(pkgStats) {
|
|
|
63
66
|
* @class
|
|
64
67
|
*/
|
|
65
68
|
export class VitePluginSvelteStats {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
/** @type {PackageInfo[]} */
|
|
70
|
+
#packageInfos = [];
|
|
71
|
+
|
|
69
72
|
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection[]} */
|
|
70
73
|
#collections = [];
|
|
71
74
|
|
|
72
|
-
/**
|
|
73
|
-
* @param {import('./vite-plugin-svelte-cache.js').VitePluginSvelteCache} cache
|
|
74
|
-
*/
|
|
75
|
-
constructor(cache) {
|
|
76
|
-
this.#cache = cache;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
75
|
/**
|
|
80
76
|
* @param {string} name
|
|
81
77
|
* @param {Partial<import('../types/vite-plugin-svelte-stats.d.ts').CollectionOptions>} [opts]
|
|
@@ -173,7 +169,7 @@ export class VitePluginSvelteStats {
|
|
|
173
169
|
async #aggregateStatsResult(collection) {
|
|
174
170
|
const stats = collection.stats;
|
|
175
171
|
for (const stat of stats) {
|
|
176
|
-
stat.pkg = (await this.#
|
|
172
|
+
stat.pkg = (await this.#getPackageInfo(stat.file)).name;
|
|
177
173
|
}
|
|
178
174
|
|
|
179
175
|
// group stats
|
|
@@ -197,4 +193,56 @@ export class VitePluginSvelteStats {
|
|
|
197
193
|
groups.sort((a, b) => b.duration - a.duration);
|
|
198
194
|
collection.packageStats = groups;
|
|
199
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* @param {string} file
|
|
198
|
+
* @returns {Promise<PackageInfo>}
|
|
199
|
+
*/
|
|
200
|
+
async #getPackageInfo(file) {
|
|
201
|
+
let info = this.#packageInfos.find((pi) => file.startsWith(pi.path));
|
|
202
|
+
if (!info) {
|
|
203
|
+
info = await findPackageInfo(file);
|
|
204
|
+
this.#packageInfos.push(info);
|
|
205
|
+
}
|
|
206
|
+
return info;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @typedef {{
|
|
212
|
+
* name: string;
|
|
213
|
+
* version: string;
|
|
214
|
+
* svelte?: string;
|
|
215
|
+
* path: string;
|
|
216
|
+
* }} PackageInfo
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* utility to get some info from the closest package.json with a "name" set
|
|
221
|
+
*
|
|
222
|
+
* @param {string} file to find info for
|
|
223
|
+
* @returns {Promise<PackageInfo>}
|
|
224
|
+
*/
|
|
225
|
+
async function findPackageInfo(file) {
|
|
226
|
+
/** @type {PackageInfo} */
|
|
227
|
+
const info = {
|
|
228
|
+
name: '$unknown',
|
|
229
|
+
version: '0.0.0-unknown',
|
|
230
|
+
path: '$unknown'
|
|
231
|
+
};
|
|
232
|
+
let path = await findClosestPkgJsonPath(file, (pkgPath) => {
|
|
233
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
234
|
+
if (pkg.name != null) {
|
|
235
|
+
info.name = pkg.name;
|
|
236
|
+
if (pkg.version != null) {
|
|
237
|
+
info.version = pkg.version;
|
|
238
|
+
}
|
|
239
|
+
info.svelte = pkg.svelte;
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
return false;
|
|
243
|
+
});
|
|
244
|
+
// return normalized path with appended '/' so .startsWith works for future file checks
|
|
245
|
+
path = normalizePath(dirname(path ?? file)) + '/';
|
|
246
|
+
info.path = path;
|
|
247
|
+
return info;
|
|
200
248
|
}
|
package/src/utils/watch.js
CHANGED
|
@@ -6,10 +6,9 @@ import path from 'node:path';
|
|
|
6
6
|
/**
|
|
7
7
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
8
8
|
* @param {import('./vite-plugin-svelte-cache.js').VitePluginSvelteCache} cache
|
|
9
|
-
* @param {import('../types/id.d.ts').IdParser} requestParser
|
|
10
9
|
* @returns {void}
|
|
11
10
|
*/
|
|
12
|
-
export function setupWatchers(options, cache
|
|
11
|
+
export function setupWatchers(options, cache) {
|
|
13
12
|
const { server, configFile: svelteConfigFile } = options;
|
|
14
13
|
if (!server) {
|
|
15
14
|
return;
|
|
@@ -30,16 +29,7 @@ export function setupWatchers(options, cache, requestParser) {
|
|
|
30
29
|
}
|
|
31
30
|
});
|
|
32
31
|
};
|
|
33
|
-
|
|
34
|
-
const removeUnlinkedFromCache = (filename) => {
|
|
35
|
-
const svelteRequest = requestParser(filename, false);
|
|
36
|
-
if (svelteRequest) {
|
|
37
|
-
const removedFromCache = cache.remove(svelteRequest);
|
|
38
|
-
if (removedFromCache) {
|
|
39
|
-
log.debug(`cleared VitePluginSvelteCache for deleted file ${filename}`, undefined, 'hmr');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
32
|
+
|
|
43
33
|
/** @type {(filename: string) => void} */
|
|
44
34
|
const triggerViteRestart = (filename) => {
|
|
45
35
|
if (serverConfig.middlewareMode) {
|
|
@@ -63,7 +53,7 @@ export function setupWatchers(options, cache, requestParser) {
|
|
|
63
53
|
const listenerCollection = {
|
|
64
54
|
add: [],
|
|
65
55
|
change: [emitChangeEventOnDependants],
|
|
66
|
-
unlink: [
|
|
56
|
+
unlink: [emitChangeEventOnDependants]
|
|
67
57
|
};
|
|
68
58
|
|
|
69
59
|
if (svelteConfigFile !== false) {
|
package/types/index.d.ts
CHANGED
|
@@ -206,6 +206,11 @@ declare module '@sveltejs/vite-plugin-svelte' {
|
|
|
206
206
|
*/
|
|
207
207
|
style?: boolean | InlineConfig | ResolvedConfig;
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* returns a list of plugins to handle svelte files
|
|
211
|
+
* plugins are named `vite-plugin-svelte:<task>`
|
|
212
|
+
*
|
|
213
|
+
* */
|
|
209
214
|
export function svelte(inlineOptions?: Partial<Options>): import("vite").Plugin[];
|
|
210
215
|
export function vitePreprocess(opts?: VitePreprocessOptions): import("svelte/compiler").PreprocessorGroup;
|
|
211
216
|
export function loadSvelteConfig(viteConfig?: import("vite").UserConfig, inlineOptions?: Partial<Options>): Promise<Partial<SvelteConfig> | undefined>;
|
package/types/index.d.ts.map
CHANGED
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
null,
|
|
27
27
|
null
|
|
28
28
|
],
|
|
29
|
-
"mappings": ";;;;aAIYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA
|
|
29
|
+
"mappings": ";;;;aAIYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA;;;;;;;;;;;;;;;;;;iBChLtBC,MAAMA;iBCMNC,cAAcA;iBCFRC,gBAAgBA",
|
|
30
30
|
"ignoreList": []
|
|
31
31
|
}
|