@sveltejs/vite-plugin-svelte 7.0.0 → 7.1.0

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.
@@ -1,3 +1,8 @@
1
+ /** @import { Options, SvelteConfig } from '../public.js' */
2
+ /** @import { PreResolvedOptions, ResolvedOptions } from '../types/options.js' */
3
+ /** @import { ConfigEnv, EnvironmentOptions, Plugin, ResolvedConfig, UserConfig } from 'vite' */
4
+ /** @import { CrawlFrameworkPkgsResult } from 'vitefu' */
5
+
1
6
  import process from 'node:process';
2
7
  import * as vite from 'vite';
3
8
  const {
@@ -16,7 +21,8 @@ import {
16
21
  FAQ_LINK_MISSING_EXPORTS_CONDITION,
17
22
  LINK_TRANSFORM_WITH_PLUGIN,
18
23
  SVELTE_EXPORT_CONDITIONS,
19
- SVELTE_IMPORTS,
24
+ SVELTE_CLIENT_IMPORTS,
25
+ SVELTE_DEDUPED_IMPORTS,
20
26
  SVELTE_RUNTIME_DEPENDENCIES
21
27
  } from './constants.js';
22
28
 
@@ -48,7 +54,7 @@ const knownRootOptions = new Set(['extensions', 'compilerOptions', 'preprocess',
48
54
  const allowedInlineOptions = new Set(['configFile', ...allowedPluginOptions, ...knownRootOptions]);
49
55
 
50
56
  /**
51
- * @param {Partial<import('../public.d.ts').Options>} [inlineOptions]
57
+ * @param {Partial<Options>} [inlineOptions]
52
58
  */
53
59
  export function validateInlineOptions(inlineOptions) {
54
60
  const invalidKeys = Object.keys(inlineOptions || {}).filter(
@@ -60,8 +66,8 @@ export function validateInlineOptions(inlineOptions) {
60
66
  }
61
67
 
62
68
  /**
63
- * @param {Partial<import('../public.d.ts').SvelteConfig>} [config]
64
- * @returns {Partial<import('../public.d.ts').Options> | undefined}
69
+ * @param {Partial<SvelteConfig>} [config]
70
+ * @returns {Partial<Options> | undefined}
65
71
  */
66
72
  function convertPluginOptions(config) {
67
73
  if (!config) {
@@ -111,7 +117,7 @@ function convertPluginOptions(config) {
111
117
  delete pluginOptions[unkownOption];
112
118
  });
113
119
  }
114
- /** @type {import('../public.d.ts').Options} */
120
+ /** @type {Options} */
115
121
  const result = {
116
122
  ...config,
117
123
  ...pluginOptions
@@ -124,22 +130,22 @@ function convertPluginOptions(config) {
124
130
 
125
131
  /**
126
132
  * used in config phase, merges the default options, svelte config, and inline options
127
- * @param {Partial<import('../public.d.ts').Options> | undefined} inlineOptions
128
- * @param {import('vite').UserConfig} viteUserConfig
129
- * @param {import('vite').ConfigEnv} viteEnv
130
- * @returns {Promise<import('../types/options.d.ts').PreResolvedOptions>}
133
+ * @param {Partial<Options> | undefined} inlineOptions
134
+ * @param {UserConfig} viteUserConfig
135
+ * @param {ConfigEnv} viteEnv
136
+ * @returns {Promise<PreResolvedOptions>}
131
137
  */
132
138
  export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv) {
133
139
  if (!inlineOptions) {
134
140
  inlineOptions = {};
135
141
  }
136
- /** @type {import('vite').UserConfig} */
142
+ /** @type {UserConfig} */
137
143
  const viteConfigWithResolvedRoot = {
138
144
  ...viteUserConfig,
139
145
  root: resolveViteRoot(viteUserConfig)
140
146
  };
141
147
  const isBuild = viteEnv.command === 'build';
142
- /** @type {Partial<import('../types/options.d.ts').PreResolvedOptions>} */
148
+ /** @type {Partial<PreResolvedOptions>} */
143
149
  const defaultOptions = {
144
150
  extensions: DEFAULT_SVELTE_EXT,
145
151
  emitCss: true,
@@ -148,7 +154,7 @@ export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv)
148
154
  const svelteConfig = convertPluginOptions(
149
155
  await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions)
150
156
  );
151
- /** @type {Partial<import('../types/options.d.ts').PreResolvedOptions>} */
157
+ /** @type {Partial<PreResolvedOptions>} */
152
158
  const extraOptions = {
153
159
  root: viteConfigWithResolvedRoot.root,
154
160
  isBuild,
@@ -156,7 +162,7 @@ export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv)
156
162
  isDebug: process.env.DEBUG != null
157
163
  };
158
164
 
159
- const merged = /** @type {import('../types/options.d.ts').PreResolvedOptions} */ (
165
+ const merged = /** @type {PreResolvedOptions} */ (
160
166
  mergeConfigs(defaultOptions, svelteConfig, inlineOptions, extraOptions)
161
167
  );
162
168
  // configFile of svelteConfig contains the absolute path it was loaded from,
@@ -187,13 +193,13 @@ function mergeConfigs(...configs) {
187
193
  /**
188
194
  * used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors. also validates the final config.
189
195
  *
190
- * @param {import('../types/options.d.ts').PreResolvedOptions} preResolveOptions
191
- * @param {import('vite').ResolvedConfig} viteConfig
192
- * @returns {import('../types/options.d.ts').ResolvedOptions}
196
+ * @param {PreResolvedOptions} preResolveOptions
197
+ * @param {ResolvedConfig} viteConfig
198
+ * @returns {ResolvedOptions}
193
199
  */
194
200
  export function resolveOptions(preResolveOptions, viteConfig) {
195
201
  const css = preResolveOptions.emitCss ? 'external' : 'injected';
196
- /** @type {Partial<import('../public.d.ts').Options>} */
202
+ /** @type {Partial<Options>} */
197
203
  const defaultOptions = {
198
204
  compilerOptions: {
199
205
  css,
@@ -206,12 +212,12 @@ export function resolveOptions(preResolveOptions, viteConfig) {
206
212
  }
207
213
  };
208
214
 
209
- /** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
215
+ /** @type {Partial<ResolvedOptions>} */
210
216
  const extraOptions = {
211
217
  root: viteConfig.root,
212
218
  isProduction: viteConfig.isProduction
213
219
  };
214
- const merged = /** @type {import('../types/options.d.ts').ResolvedOptions}*/ (
220
+ const merged = /** @type {ResolvedOptions}*/ (
215
221
  mergeConfigs(defaultOptions, preResolveOptions, extraOptions)
216
222
  );
217
223
 
@@ -225,8 +231,8 @@ export function resolveOptions(preResolveOptions, viteConfig) {
225
231
  }
226
232
 
227
233
  /**
228
- * @param {import('../types/options.d.ts').ResolvedOptions} options
229
- * @param {import('vite').ResolvedConfig} viteConfig
234
+ * @param {ResolvedOptions} options
235
+ * @param {ResolvedConfig} viteConfig
230
236
  */
231
237
  function enforceOptionsForHmr(options, viteConfig) {
232
238
  if (options.compilerOptions.hmr && viteConfig.server?.hmr === false) {
@@ -265,7 +271,7 @@ function enforceOptionsForHmr(options, viteConfig) {
265
271
  }
266
272
 
267
273
  /**
268
- * @param {import('../types/options.d.ts').ResolvedOptions} options
274
+ * @param {ResolvedOptions} options
269
275
  */
270
276
  function enforceOptionsForProduction(options) {
271
277
  if (options.isProduction) {
@@ -285,7 +291,7 @@ function enforceOptionsForProduction(options) {
285
291
  }
286
292
 
287
293
  /**
288
- * @param {import('../types/options.d.ts').ResolvedOptions} options
294
+ * @param {ResolvedOptions} options
289
295
  */
290
296
  function removeIgnoredOptions(options) {
291
297
  const ignoredCompilerOptions = ['generate', 'format', 'filename'];
@@ -305,7 +311,7 @@ function removeIgnoredOptions(options) {
305
311
  }
306
312
 
307
313
  /**
308
- * @param {import('../types/options.d.ts').ResolvedOptions} options
314
+ * @param {ResolvedOptions} options
309
315
  */
310
316
  function handleDeprecatedOptions(options) {
311
317
  const experimental = /** @type {Record<string, any>} */ (options.experimental);
@@ -324,10 +330,10 @@ function handleDeprecatedOptions(options) {
324
330
  }
325
331
 
326
332
  /**
327
- * @param {import('vite').ResolvedConfig} config
333
+ * @param {ResolvedConfig} config
328
334
  */
329
335
  function logRemovedPluginAPI(config) {
330
- /** @type {import('vite').Plugin[]} */
336
+ /** @type {Plugin[]} */
331
337
  const pluginsWithPreprocessors = config.plugins.filter((p) => p?.api?.sveltePreprocess);
332
338
 
333
339
  if (pluginsWithPreprocessors.length > 0) {
@@ -348,7 +354,7 @@ function logRemovedPluginAPI(config) {
348
354
  * @see https://github.com/sveltejs/vite-plugin-svelte/issues/113
349
355
  * @see https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
350
356
  *
351
- * @param {import('vite').UserConfig} viteConfig
357
+ * @param {UserConfig} viteConfig
352
358
  * @returns {string | undefined}
353
359
  */
354
360
  function resolveViteRoot(viteConfig) {
@@ -356,15 +362,15 @@ function resolveViteRoot(viteConfig) {
356
362
  }
357
363
 
358
364
  /**
359
- * @param {import('../types/options.d.ts').PreResolvedOptions} options
360
- * @param {import('vite').UserConfig} config
361
- * @returns {Promise<Partial<import('vite').UserConfig>>}
365
+ * @param {PreResolvedOptions} options
366
+ * @param {UserConfig} config
367
+ * @returns {Promise<Partial<UserConfig>>}
362
368
  */
363
369
  export async function buildExtraViteConfig(options, config) {
364
- /** @type {Partial<import('vite').UserConfig>} */
370
+ /** @type {Partial<UserConfig>} */
365
371
  const extraViteConfig = {
366
372
  resolve: {
367
- dedupe: [...SVELTE_IMPORTS]
373
+ dedupe: [...SVELTE_DEDUPED_IMPORTS]
368
374
  }
369
375
  // this option is still awaiting a PR in vite to be supported
370
376
  // see https://github.com/sveltejs/vite-plugin-svelte/issues/60
@@ -414,9 +420,9 @@ export async function buildExtraViteConfig(options, config) {
414
420
  }
415
421
 
416
422
  /**
417
- * @param {Partial<import('vite').UserConfig>} extraViteConfig
418
- * @param {import('vite').UserConfig} config
419
- * @param {import('../types/options.d.ts').PreResolvedOptions} options
423
+ * @param {Partial<UserConfig>} extraViteConfig
424
+ * @param {UserConfig} config
425
+ * @param {PreResolvedOptions} options
420
426
  */
421
427
  function validateViteConfig(extraViteConfig, config, options) {
422
428
  const { prebundleSvelteLibraries, isBuild } = options;
@@ -465,9 +471,9 @@ function validateViteConfig(extraViteConfig, config, options) {
465
471
  }
466
472
 
467
473
  /**
468
- * @param {import('../types/options.d.ts').PreResolvedOptions} options
469
- * @param {import('vite').UserConfig} config
470
- * @returns {Promise<import('vitefu').CrawlFrameworkPkgsResult>}
474
+ * @param {PreResolvedOptions} options
475
+ * @param {UserConfig} config
476
+ * @returns {Promise<CrawlFrameworkPkgsResult>}
471
477
  */
472
478
  async function buildExtraConfigForDependencies(options, config) {
473
479
  // extra handling for svelte dependencies in the project
@@ -557,8 +563,8 @@ async function buildExtraConfigForDependencies(options, config) {
557
563
  }
558
564
 
559
565
  /**
560
- * @param {import('vite').UserConfig} config
561
- * @returns {import('vite').UserConfig & { optimizeDeps: { include: string[], exclude:string[] }, ssr: { noExternal:(string|RegExp)[], external: string[] } } }
566
+ * @param {UserConfig} config
567
+ * @returns {UserConfig & { optimizeDeps: { include: string[], exclude:string[] }, ssr: { noExternal:(string|RegExp)[], external: string[] } } }
562
568
  */
563
569
  function buildExtraConfigForSvelte(config) {
564
570
  // include svelte imports for optimization unless explicitly excluded
@@ -567,9 +573,7 @@ function buildExtraConfigForSvelte(config) {
567
573
  /** @type {string[]} */
568
574
  const exclude = [];
569
575
  if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
570
- const svelteImportsToInclude = SVELTE_IMPORTS.filter(
571
- (si) => !(si.endsWith('/server') || si.includes('/server/'))
572
- );
576
+ const svelteImportsToInclude = [...SVELTE_CLIENT_IMPORTS];
573
577
  svelteImportsToInclude.push(...SVELTE_RUNTIME_DEPENDENCIES.map((dep) => `svelte > ${dep}`));
574
578
  log.debug(
575
579
  `adding bare svelte packages and runtime dependencies to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `,
@@ -604,7 +608,7 @@ function buildExtraConfigForSvelte(config) {
604
608
  /**
605
609
  * Mutates `config` to ensure `resolve.mainFields` is set. If unset, it emulates Vite's default fallback.
606
610
  * @param {string} name
607
- * @param {import('vite').EnvironmentOptions} config
611
+ * @param {EnvironmentOptions} config
608
612
  * @param {{ isSsrTargetWebworker?: boolean }} opts
609
613
  */
610
614
  export function ensureConfigEnvironmentMainFields(name, config, opts) {
@@ -622,7 +626,7 @@ export function ensureConfigEnvironmentMainFields(name, config, opts) {
622
626
  /**
623
627
  * Mutates `config` to ensure `resolve.conditions` is set. If unset, it emulates Vite's default fallback.
624
628
  * @param {string} name
625
- * @param {import('vite').EnvironmentOptions} config
629
+ * @param {EnvironmentOptions} config
626
630
  * @param {{ isSsrTargetWebworker?: boolean }} opts
627
631
  */
628
632
  export function ensureConfigEnvironmentConditions(name, config, opts) {
@@ -1,3 +1,5 @@
1
+ /** @import { CollectionOptions, PackageStats, Stat, StatCollection } from '../types/vite-plugin-svelte-stats.js' */
2
+
1
3
  import { log } from './log.js';
2
4
  import { performance } from 'node:perf_hooks';
3
5
  import { normalizePath } from 'vite';
@@ -5,7 +7,7 @@ import { findClosestPkgJsonPath } from 'vitefu';
5
7
  import { readFileSync } from 'node:fs';
6
8
  import { dirname } from 'node:path';
7
9
 
8
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').CollectionOptions} */
10
+ /** @type {CollectionOptions} */
9
11
  const defaultCollectionOptions = {
10
12
  // log after 500ms and more than one file processed
11
13
  logInProgress: (c, now) => now - c.collectionStart > 500 && c.stats.length > 1,
@@ -23,7 +25,7 @@ function humanDuration(n) {
23
25
  }
24
26
 
25
27
  /**
26
- * @param {import('../types/vite-plugin-svelte-stats.d.ts').PackageStats[]} pkgStats
28
+ * @param {PackageStats[]} pkgStats
27
29
  * @returns {string}
28
30
  */
29
31
  function formatPackageStats(pkgStats) {
@@ -69,26 +71,26 @@ export class VitePluginSvelteStats {
69
71
  /** @type {PackageInfo[]} */
70
72
  #packageInfos = [];
71
73
 
72
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection[]} */
74
+ /** @type {StatCollection[]} */
73
75
  #collections = [];
74
76
 
75
77
  /**
76
78
  * @param {string} name
77
- * @param {Partial<import('../types/vite-plugin-svelte-stats.d.ts').CollectionOptions>} [opts]
78
- * @returns {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection}
79
+ * @param {Partial<CollectionOptions>} [opts]
80
+ * @returns {StatCollection}
79
81
  */
80
82
  startCollection(name, opts) {
81
83
  const options = {
82
84
  ...defaultCollectionOptions,
83
85
  ...opts
84
86
  };
85
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').Stat[]} */
87
+ /** @type {Stat[]} */
86
88
  const stats = [];
87
89
  const collectionStart = performance.now();
88
90
 
89
91
  const _this = this;
90
92
  let hasLoggedProgress = false;
91
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection} */
93
+ /** @type {StatCollection} */
92
94
  const collection = {
93
95
  name,
94
96
  options,
@@ -101,7 +103,7 @@ export class VitePluginSvelteStats {
101
103
  }
102
104
  file = normalizePath(file);
103
105
  const start = performance.now();
104
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').Stat} */
106
+ /** @type {Stat} */
105
107
  const stat = { file, start, end: start };
106
108
  return () => {
107
109
  const now = performance.now();
@@ -126,7 +128,7 @@ export class VitePluginSvelteStats {
126
128
  }
127
129
 
128
130
  /**
129
- * @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection} collection
131
+ * @param {StatCollection} collection
130
132
  */
131
133
  async #finish(collection) {
132
134
  try {
@@ -138,9 +140,7 @@ export class VitePluginSvelteStats {
138
140
  await this.#aggregateStatsResult(collection);
139
141
  log.debug(
140
142
  `${collection.name} done.\n${formatPackageStats(
141
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').PackageStats[]}*/ (
142
- collection.packageStats
143
- )
143
+ /** @type {PackageStats[]}*/ (collection.packageStats)
144
144
  )}`,
145
145
  undefined,
146
146
  'stats'
@@ -164,7 +164,7 @@ export class VitePluginSvelteStats {
164
164
  }
165
165
 
166
166
  /**
167
- * @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection} collection
167
+ * @param {StatCollection} collection
168
168
  */
169
169
  async #aggregateStatsResult(collection) {
170
170
  const stats = collection.stats;
@@ -173,7 +173,7 @@ export class VitePluginSvelteStats {
173
173
  }
174
174
 
175
175
  // group stats
176
- /** @type {Record<string, import('../types/vite-plugin-svelte-stats.d.ts').PackageStats>} */
176
+ /** @type {Record<string, PackageStats>} */
177
177
  const grouped = {};
178
178
  stats.forEach((stat) => {
179
179
  const pkg = /** @type {string} */ (stat.pkg);
@@ -1,10 +1,13 @@
1
+ /** @import { ResolvedOptions } from '../types/options.js' */
2
+ /** @import { FSWatcher } from 'vite' */
3
+
1
4
  import fs from 'node:fs';
2
5
  import { log } from './log.js';
3
6
  import { knownSvelteConfigNames } from './load-svelte-config.js';
4
7
  import path from 'node:path';
5
8
 
6
9
  /**
7
- * @param {import('../types/options.d.ts').ResolvedOptions} options
10
+ * @param {ResolvedOptions} options
8
11
  * @returns {void}
9
12
  */
10
13
  export function setupWatchers(options) {
@@ -75,7 +78,7 @@ export function setupWatchers(options) {
75
78
 
76
79
  /**
77
80
  * taken from vite utils
78
- * @param {import('vite').FSWatcher} watcher
81
+ * @param {FSWatcher} watcher
79
82
  * @param {string | null} file
80
83
  * @param {string} root
81
84
  * @returns {void}
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module '@sveltejs/vite-plugin-svelte' {
2
- import type { InlineConfig, ResolvedConfig } from 'vite';
2
+ import type { InlineConfig, ResolvedConfig, Plugin, UserConfig } from 'vite';
3
3
  import type { CompileOptions, Warning, PreprocessorGroup } from 'svelte/compiler';
4
4
  export type Options = Omit<SvelteConfig, 'vitePlugin'> & PluginOptionsInline;
5
5
 
@@ -270,9 +270,9 @@ declare module '@sveltejs/vite-plugin-svelte' {
270
270
  * plugins are named `vite-plugin-svelte:<task>`
271
271
  *
272
272
  * */
273
- export function svelte(inlineOptions?: Partial<Options>): import("vite").Plugin[];
274
- export function vitePreprocess(opts?: VitePreprocessOptions): import("svelte/compiler").PreprocessorGroup;
275
- export function loadSvelteConfig(viteConfig?: import("vite").UserConfig, inlineOptions?: Partial<Options>): Promise<Partial<SvelteConfig> | undefined>;
273
+ export function svelte(inlineOptions?: Partial<Options>): Plugin[];
274
+ export function vitePreprocess(opts?: VitePreprocessOptions): PreprocessorGroup;
275
+ export function loadSvelteConfig(viteConfig?: UserConfig, inlineOptions?: Partial<Options>): Promise<Partial<SvelteConfig> | undefined>;
276
276
 
277
277
  export {};
278
278
  }
@@ -27,6 +27,6 @@
27
27
  null,
28
28
  null
29
29
  ],
30
- "mappings": ";;;aAGYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgFbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA;;;;;;;;;;;;;;kBAcrBC,gBAAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC7KjBC,MAAMA;iBCGNC,cAAcA;iBCCRC,gBAAgBA",
30
+ "mappings": ";;;aAGYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgFbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA;;;;;;;;;;;;;;kBAcrBC,gBAAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCzKjBC,MAAMA;iBCGNC,cAAcA;iBCARC,gBAAgBA",
31
31
  "ignoreList": []
32
32
  }